2008-08-06 18:36:30 +00:00
|
|
|
// This file is part of BOINC.
|
2005-01-20 23:22:22 +00:00
|
|
|
// http://boinc.berkeley.edu
|
2008-08-06 18:36:30 +00:00
|
|
|
// Copyright (C) 2008 University of California
|
2004-11-14 08:29:32 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC 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 3 of the License, or (at your option) any later version.
|
2003-08-15 00:50:58 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
2005-01-20 23:22:22 +00:00
|
|
|
// 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
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
|
2002-09-26 18:11:06 +00:00
|
|
|
|
2010-05-11 19:10:29 +00:00
|
|
|
#if defined(_WIN32) && !defined(__STDWX_H__)
|
2004-06-16 23:16:08 +00:00
|
|
|
#include "boinc_win.h"
|
2010-05-11 19:10:29 +00:00
|
|
|
#elif defined(_WIN32) && defined(__STDWX_H__)
|
|
|
|
#include "stdwx.h"
|
|
|
|
#else
|
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>
|
2005-12-22 00:55:15 +00:00
|
|
|
#include <unistd.h>
|
2004-03-06 09:45:25 +00:00
|
|
|
#endif
|
2004-03-05 04:37:53 +00:00
|
|
|
|
2004-01-04 06:48:40 +00:00
|
|
|
#include "error_numbers.h"
|
2012-08-01 20:04:05 +00:00
|
|
|
#include "filesys.h"
|
2004-06-12 04:45:36 +00:00
|
|
|
|
2012-08-11 05:47:18 +00:00
|
|
|
#include "mfile.h"
|
|
|
|
|
2004-06-12 04:45:36 +00:00
|
|
|
MFILE::MFILE() {
|
2010-03-18 17:01:11 +00:00
|
|
|
buf = (char*)malloc(64*1024);
|
2002-08-05 00:29:34 +00:00
|
|
|
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;
|
2010-08-11 20:23:49 +00:00
|
|
|
if (!buf) buf = (char*)malloc(64*1024);
|
2002-08-05 00:29:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-03-18 17:01:11 +00:00
|
|
|
// seems like Win's realloc is stupid, Make it smart.
|
|
|
|
//
|
2010-03-23 00:01:25 +00:00
|
|
|
static inline char* realloc_aux(char* p, size_t len) {
|
2011-09-18 21:28:31 +00:00
|
|
|
if (!p) {
|
|
|
|
return (char*)malloc(64*1024);
|
|
|
|
}
|
2010-03-18 19:56:20 +00:00
|
|
|
#ifdef _WIN32
|
2010-03-23 00:01:25 +00:00
|
|
|
if (_msize(p) >= (unsigned int)len) return p;
|
2010-03-18 17:01:11 +00:00
|
|
|
return (char*) realloc(p, len*2);
|
2010-03-18 19:56:20 +00:00
|
|
|
#else
|
|
|
|
return (char*) realloc(p, len);
|
|
|
|
#endif
|
2010-03-18 17:01:11 +00:00
|
|
|
}
|
|
|
|
|
2006-12-07 23:39:23 +00:00
|
|
|
#define BUFSIZE 100000
|
|
|
|
|
2004-06-12 04:45:36 +00:00
|
|
|
int MFILE::vprintf(const char* format, va_list ap) {
|
2006-12-07 23:39:23 +00:00
|
|
|
char buf2[BUFSIZE];
|
2002-08-05 00:29:34 +00:00
|
|
|
int n, k;
|
|
|
|
|
2006-12-07 23:39:23 +00:00
|
|
|
k = vsnprintf(buf2, BUFSIZE, format, ap);
|
2006-12-12 23:32:25 +00:00
|
|
|
if (k<=-1 || k>=BUFSIZE) {
|
2006-12-07 23:39:23 +00:00
|
|
|
fprintf(stderr, "ERROR: buffer too small in MFILE::vprintf()\n");
|
2007-01-19 22:50:32 +00:00
|
|
|
fprintf(stderr, "ERROR: format: %s\n", format);
|
|
|
|
fprintf(stderr, "ERROR: k=%d, BUFSIZE=%d\n", k, BUFSIZE);
|
2006-12-07 23:39:23 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2004-03-06 09:45:25 +00:00
|
|
|
n = (int)strlen(buf2);
|
2010-03-18 17:01:11 +00:00
|
|
|
buf = (char*)realloc_aux(buf, len+n+1);
|
2004-01-04 06:48:40 +00:00
|
|
|
if (!buf) {
|
2012-12-05 21:06:16 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: realloc() failed in MFILE::vprintf(); len %d n %d\n",
|
|
|
|
len, n
|
|
|
|
);
|
2009-07-10 22:59:45 +00:00
|
|
|
exit(1);
|
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) {
|
2010-03-18 17:01:11 +00:00
|
|
|
buf = (char *)realloc_aux( buf, len+(size*nitems)+1 );
|
2004-01-04 06:48:40 +00:00
|
|
|
if (!buf) {
|
2012-12-05 21:06:16 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: realloc() failed in MFILE::write(); len %d size %lu nitems %lu\n",
|
|
|
|
len, size, nitems
|
|
|
|
);
|
2009-07-10 22:59:45 +00:00
|
|
|
exit(1);
|
2004-01-04 06:48:40 +00:00
|
|
|
}
|
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) {
|
2010-03-18 17:01:11 +00:00
|
|
|
buf = (char*)realloc_aux(buf, len+1+1);
|
2004-01-04 06:48:40 +00:00
|
|
|
if (!buf) {
|
2012-12-05 21:06:16 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: realloc() failed in MFILE::_putchar(); len %d\n", len
|
|
|
|
);
|
2009-07-10 22:59:45 +00:00
|
|
|
exit(1);
|
2004-01-04 06:48:40 +00:00
|
|
|
}
|
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);
|
2010-03-18 17:01:11 +00:00
|
|
|
buf = (char*)realloc_aux(buf, len+n+1);
|
2004-01-04 06:48:40 +00:00
|
|
|
if (!buf) {
|
2012-12-05 21:06:16 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: realloc() failed in MFILE::puts() len %d n %d\n", len, n
|
|
|
|
);
|
2009-07-10 22:59:45 +00:00
|
|
|
exit(1);
|
2004-01-04 06:48:40 +00:00
|
|
|
}
|
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() {
|
2010-08-11 20:23:49 +00:00
|
|
|
int retval = 0;
|
|
|
|
if (f) {
|
|
|
|
flush();
|
|
|
|
fclose(f);
|
|
|
|
f = NULL;
|
|
|
|
}
|
|
|
|
if (buf) {
|
|
|
|
free(buf);
|
|
|
|
buf = NULL;
|
|
|
|
}
|
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
|
|
|
|