2005-01-20 23:22:22 +00:00
|
|
|
// Berkeley Open Infrastructure for Network Computing
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2005 University of California
|
2004-11-14 08:29:32 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// This is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation;
|
|
|
|
// either version 2.1 of the License, or (at your option) any later version.
|
2003-08-15 00:50:58 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// This software is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
// See the GNU Lesser General Public License for more details.
|
2002-09-26 18:11:06 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// To view the GNU Lesser General Public License visit
|
|
|
|
// http://www.gnu.org/copyleft/lesser.html
|
|
|
|
// or write to the Free Software Foundation, Inc.,
|
|
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2002-09-26 18:11:06 +00:00
|
|
|
|
2005-07-14 16:46:38 +00:00
|
|
|
#if defined(_WIN32) && !defined(__STDWX_H__) && !defined(_BOINC_WIN_) && !defined(_AFX_STDAFX_H_)
|
2004-06-16 23:16:08 +00:00
|
|
|
#include "boinc_win.h"
|
2004-03-06 09:45:25 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
2005-11-21 18:34:44 +00:00
|
|
|
#include "config.h"
|
2004-07-13 13:54:09 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstdarg>
|
|
|
|
#include <cstring>
|
2004-03-05 04:37:53 +00:00
|
|
|
#include <string>
|
2004-07-13 13:54:09 +00:00
|
|
|
#include <cerrno>
|
2002-09-04 19:55:19 +00:00
|
|
|
#if HAVE_MALLOC_H
|
2002-08-05 00:29:34 +00:00
|
|
|
#include <malloc.h>
|
2002-09-04 19:55:19 +00:00
|
|
|
#endif
|
2005-12-22 00:55:15 +00:00
|
|
|
#include <unistd.h>
|
2002-08-05 00:29:34 +00:00
|
|
|
|
2004-03-05 04:37:53 +00:00
|
|
|
using namespace std;
|
2004-03-06 09:45:25 +00:00
|
|
|
#endif
|
2004-03-05 04:37:53 +00:00
|
|
|
|
2003-12-24 00:50:51 +00:00
|
|
|
#include "filesys.h"
|
2004-01-04 06:48:40 +00:00
|
|
|
#include "error_numbers.h"
|
2004-04-15 00:21:29 +00:00
|
|
|
#include "mfile.h"
|
2002-08-05 00:29:34 +00:00
|
|
|
|
2004-06-12 04:45:36 +00:00
|
|
|
|
|
|
|
MFILE::MFILE() {
|
2002-08-05 00:29:34 +00:00
|
|
|
buf = 0;
|
|
|
|
len = 0;
|
2004-06-12 04:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MFILE::~MFILE() {
|
|
|
|
if (buf) free(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
int MFILE::open(const char* path, const char* mode) {
|
2003-12-24 00:50:51 +00:00
|
|
|
f = boinc_fopen(path, mode);
|
2005-04-28 23:19:58 +00:00
|
|
|
if (!f) return ERR_FOPEN;
|
2002-08-05 00:29:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-06-12 04:45:36 +00:00
|
|
|
int MFILE::vprintf(const char* format, va_list ap) {
|
2005-08-06 21:13:21 +00:00
|
|
|
char buf2[65536];
|
2002-08-05 00:29:34 +00:00
|
|
|
int n, k;
|
|
|
|
|
|
|
|
k = vsprintf(buf2, format, ap);
|
2004-03-06 09:45:25 +00:00
|
|
|
n = (int)strlen(buf2);
|
2005-02-01 21:11:08 +00:00
|
|
|
buf = (char*)realloc(buf, len+n+1);
|
2004-01-04 06:48:40 +00:00
|
|
|
if (!buf) {
|
|
|
|
errno = ERR_MALLOC;
|
2005-04-28 23:19:58 +00:00
|
|
|
return ERR_MALLOC;
|
2004-01-04 06:48:40 +00:00
|
|
|
}
|
2002-08-05 00:29:34 +00:00
|
|
|
strncpy(buf+len, buf2, n);
|
|
|
|
len += n;
|
2005-02-01 21:11:08 +00:00
|
|
|
buf[len] = 0;
|
2002-08-05 00:29:34 +00:00
|
|
|
return k;
|
|
|
|
}
|
|
|
|
|
2004-06-12 04:45:36 +00:00
|
|
|
int MFILE::printf(const char* format, ...) {
|
|
|
|
int n;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
n = MFILE::vprintf(format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2002-08-05 00:29:34 +00:00
|
|
|
size_t MFILE::write(const void *ptr, size_t size, size_t nitems) {
|
2005-02-01 21:11:08 +00:00
|
|
|
buf = (char *)realloc( buf, len+(size*nitems)+1 );
|
2004-01-04 06:48:40 +00:00
|
|
|
if (!buf) {
|
|
|
|
errno = ERR_MALLOC;
|
|
|
|
return 0;
|
|
|
|
}
|
2002-08-05 00:29:34 +00:00
|
|
|
memcpy( buf+len, ptr, size*nitems );
|
2004-03-06 09:45:25 +00:00
|
|
|
len += (int)size*(int)nitems;
|
2005-02-01 21:11:08 +00:00
|
|
|
buf[len] = 0;
|
2002-08-05 00:29:34 +00:00
|
|
|
return nitems;
|
|
|
|
}
|
|
|
|
|
|
|
|
int MFILE::_putchar(char c) {
|
2005-02-01 21:11:08 +00:00
|
|
|
buf = (char*)realloc(buf, len+1+1);
|
2004-01-04 06:48:40 +00:00
|
|
|
if (!buf) {
|
|
|
|
errno = ERR_MALLOC;
|
|
|
|
return EOF;
|
|
|
|
}
|
2002-08-05 00:29:34 +00:00
|
|
|
buf[len] = c;
|
|
|
|
len++;
|
2005-02-01 21:11:08 +00:00
|
|
|
buf[len] = 0;
|
2002-08-05 00:29:34 +00:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2003-10-29 11:05:39 +00:00
|
|
|
int MFILE::puts(const char* p) {
|
2004-03-06 09:45:25 +00:00
|
|
|
int n = (int)strlen(p);
|
2005-02-01 21:11:08 +00:00
|
|
|
buf = (char*)realloc(buf, len+n+1);
|
2004-01-04 06:48:40 +00:00
|
|
|
if (!buf) {
|
|
|
|
errno = ERR_MALLOC;
|
|
|
|
return EOF;
|
|
|
|
}
|
2002-08-05 00:29:34 +00:00
|
|
|
strncpy(buf+len, p, n);
|
|
|
|
len += n;
|
2005-02-01 21:11:08 +00:00
|
|
|
buf[len] = 0;
|
2003-12-30 19:39:25 +00:00
|
|
|
return n;
|
2002-08-05 00:29:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int MFILE::close() {
|
2005-12-22 00:55:15 +00:00
|
|
|
int retval = flush();
|
|
|
|
fclose(f);
|
2002-08-05 00:29:34 +00:00
|
|
|
free(buf);
|
|
|
|
buf = 0;
|
2005-12-22 00:55:15 +00:00
|
|
|
return retval;
|
2002-08-05 00:29:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int MFILE::flush() {
|
2005-12-22 00:55:15 +00:00
|
|
|
int n, old_len = len;
|
|
|
|
|
2005-12-22 01:02:13 +00:00
|
|
|
n = (int)fwrite(buf, 1, len, f);
|
2002-08-05 00:29:34 +00:00
|
|
|
len = 0;
|
2005-12-22 00:55:15 +00:00
|
|
|
if (n != old_len) return ERR_FWRITE;
|
|
|
|
if (fflush(f)) return ERR_FFLUSH;
|
2005-12-22 01:02:13 +00:00
|
|
|
#ifndef _WIN32
|
2005-12-22 00:55:15 +00:00
|
|
|
if (fsync(fileno(f)) < 0) return ERR_FSYNC;
|
2005-12-22 01:02:13 +00:00
|
|
|
#endif
|
2005-12-22 00:55:15 +00:00
|
|
|
return 0;
|
2002-08-05 00:29:34 +00:00
|
|
|
}
|
2003-08-15 00:50:58 +00:00
|
|
|
|
2004-01-04 06:48:40 +00:00
|
|
|
long MFILE::tell() const {
|
2003-08-15 00:50:58 +00:00
|
|
|
return f ? ftell(f) : -1;
|
|
|
|
}
|
2004-06-12 04:45:36 +00:00
|
|
|
|
|
|
|
void MFILE::get_buf(char*& b, int& l) {
|
|
|
|
b = buf;
|
|
|
|
l = len;
|
|
|
|
buf = 0;
|
|
|
|
len = 0;
|
|
|
|
}
|
2004-12-08 00:40:19 +00:00
|
|
|
|
2005-01-02 18:29:53 +00:00
|
|
|
const char *BOINC_RCSID_8de9facdd7 = "$Id$";
|