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.
|
2004-07-13 13:54:09 +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.
|
2004-06-13 23:17:33 +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/>.
|
2004-06-13 23:17:33 +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-06-13 23:17:33 +00:00
|
|
|
#include <string>
|
2004-07-13 13:54:09 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include <cstdarg>
|
2004-06-13 23:17:33 +00:00
|
|
|
#endif
|
2004-06-12 04:45:36 +00:00
|
|
|
|
|
|
|
#include "error_numbers.h"
|
|
|
|
|
2012-08-11 05:47:18 +00:00
|
|
|
#include "miofile.h"
|
|
|
|
|
2004-06-30 22:16:26 +00:00
|
|
|
using std::string;
|
|
|
|
|
2004-06-12 04:45:36 +00:00
|
|
|
MIOFILE::MIOFILE() {
|
|
|
|
mf = 0;
|
|
|
|
f = 0;
|
|
|
|
buf = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
MIOFILE::~MIOFILE() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void MIOFILE::init_mfile(MFILE* _mf) {
|
|
|
|
mf = _mf;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MIOFILE::init_file(FILE* _f) {
|
|
|
|
f = _f;
|
|
|
|
}
|
|
|
|
|
2006-11-07 17:40:55 +00:00
|
|
|
void MIOFILE::init_buf_read(const char* _buf) {
|
2004-06-12 04:45:36 +00:00
|
|
|
buf = _buf;
|
|
|
|
}
|
|
|
|
|
2007-03-13 19:33:27 +00:00
|
|
|
void MIOFILE::init_buf_write(char* _buf, int _len) {
|
2006-11-07 00:38:08 +00:00
|
|
|
wbuf = _buf;
|
2007-03-13 19:33:27 +00:00
|
|
|
len = _len;
|
2013-04-03 00:23:37 +00:00
|
|
|
wbuf[0] = 0;
|
2006-11-07 00:38:08 +00:00
|
|
|
}
|
|
|
|
|
2009-06-13 21:03:45 +00:00
|
|
|
bool MIOFILE::eof() {
|
|
|
|
if (f) {
|
|
|
|
if (!feof(f)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-01-24 20:22:54 +00:00
|
|
|
|
2004-06-12 04:45:36 +00:00
|
|
|
int MIOFILE::printf(const char* format, ...) {
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
if (mf) {
|
|
|
|
retval = mf->vprintf(format, ap);
|
2006-08-15 17:47:34 +00:00
|
|
|
} else if (f) {
|
2004-06-12 04:45:36 +00:00
|
|
|
retval = vfprintf(f, format, ap);
|
2006-08-15 17:47:34 +00:00
|
|
|
} else {
|
2008-01-31 18:34:51 +00:00
|
|
|
size_t cursize = strlen(wbuf);
|
|
|
|
size_t remaining_len = len - cursize;
|
2007-03-13 19:33:27 +00:00
|
|
|
retval = vsnprintf(wbuf+cursize, remaining_len, format, ap);
|
2004-06-12 04:45:36 +00:00
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2007-06-20 22:34:06 +00:00
|
|
|
char* MIOFILE::fgets(char* dst, int dst_len) {
|
2004-06-12 04:45:36 +00:00
|
|
|
if (f) {
|
2008-09-09 19:10:42 +00:00
|
|
|
#ifndef _USING_FCGI_
|
2007-06-20 22:34:06 +00:00
|
|
|
return ::fgets(dst, dst_len, f);
|
2008-09-09 19:10:42 +00:00
|
|
|
#else
|
|
|
|
return FCGI::fgets(dst, dst_len, f);
|
|
|
|
#endif
|
2004-06-12 04:45:36 +00:00
|
|
|
}
|
2006-11-08 00:20:16 +00:00
|
|
|
const char* q = strchr(buf, '\n');
|
2004-06-12 04:45:36 +00:00
|
|
|
if (!q) return 0;
|
|
|
|
|
|
|
|
q++;
|
2004-07-02 22:03:23 +00:00
|
|
|
int n = (int)(q - buf);
|
2007-06-20 22:34:06 +00:00
|
|
|
if (n > dst_len-1) n = dst_len-1;
|
2004-06-12 04:45:36 +00:00
|
|
|
memcpy(dst, buf, n);
|
|
|
|
dst[n] = 0;
|
|
|
|
|
|
|
|
buf = q;
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2006-09-27 16:50:47 +00:00
|
|
|
int MIOFILE::_ungetc(int c) {
|
2006-08-22 21:52:44 +00:00
|
|
|
if (f) {
|
2006-09-27 16:50:47 +00:00
|
|
|
#ifdef _USING_FCGI_
|
|
|
|
return FCGI_ungetc(c, f);
|
|
|
|
#else
|
|
|
|
return ungetc(c, f);
|
|
|
|
#endif
|
2006-11-07 17:40:55 +00:00
|
|
|
} else {
|
|
|
|
buf--;
|
|
|
|
// NOTE: we assume that the char being pushed
|
|
|
|
// is what's already there
|
|
|
|
//*buf = c;
|
2006-08-22 21:52:44 +00:00
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2004-06-12 04:45:36 +00:00
|
|
|
// copy from a file to static buffer
|
|
|
|
//
|
|
|
|
int copy_element_contents(MIOFILE& in, const char* end_tag, char* p, int len) {
|
|
|
|
char buf[256];
|
2004-09-22 21:08:26 +00:00
|
|
|
int n;
|
2004-06-12 04:45:36 +00:00
|
|
|
|
|
|
|
strcpy(p, "");
|
|
|
|
while (in.fgets(buf, 256)) {
|
|
|
|
if (strstr(buf, end_tag)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2005-07-23 07:24:51 +00:00
|
|
|
n = (int)strlen(buf);
|
2004-09-22 21:08:26 +00:00
|
|
|
if (n >= len-1) return ERR_XML_PARSE;
|
2004-06-12 04:45:36 +00:00
|
|
|
strcat(p, buf);
|
2004-09-22 21:08:26 +00:00
|
|
|
len -= n;
|
2004-06-12 04:45:36 +00:00
|
|
|
}
|
|
|
|
return ERR_XML_PARSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int copy_element_contents(MIOFILE& in, const char* end_tag, string& str) {
|
|
|
|
char buf[256];
|
|
|
|
|
|
|
|
str = "";
|
|
|
|
while (in.fgets(buf, 256)) {
|
|
|
|
if (strstr(buf, end_tag)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
str += buf;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "copy_element_contents(): no end tag\n");
|
|
|
|
return ERR_XML_PARSE;
|
|
|
|
}
|
2004-12-08 00:40:19 +00:00
|
|
|
|