1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
// This file is part of BOINC.
// http://boinc.berkeley.edu
// Copyright (C) 2008 University of California
//
// 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.
//
// BOINC 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.
//
// You should have received a copy of the GNU Lesser General Public License
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
#if defined(_WIN32) && !defined(__STDWX_H__)
#include "boinc_win.h"
#elif defined(_WIN32) && defined(__STDWX_H__)
#include "stdwx.h"
#else
#include "config.h"
#include <string>
#include <cstring>
#include <cstdarg>
#endif
#include "error_numbers.h"
#include "str_replace.h"
#include "miofile.h"
using std::string;
MIOFILE::MIOFILE() {
mf = 0;
wbuf = 0;
len = 0;
f = 0;
buf = 0;
}
MIOFILE::~MIOFILE() {
}
void MIOFILE::init_mfile(MFILE* _mf) {
mf = _mf;
}
void MIOFILE::init_file(FILE* _f) {
f = _f;
}
void MIOFILE::init_buf_read(const char* _buf) {
buf = _buf;
}
void MIOFILE::init_buf_write(char* _buf, int _len) {
wbuf = _buf;
len = _len;
wbuf[0] = 0;
}
bool MIOFILE::eof() {
if (f) {
if (!feof(f)) {
return false;
}
}
return true;
}
int MIOFILE::printf(const char* format, ...) {
int retval;
va_list ap;
va_start(ap, format);
#ifndef _USING_FCGI_
if (mf) {
retval = mf->vprintf(format, ap);
} else
#endif
if (f) {
retval = vfprintf(f, format, ap);
} else {
size_t cursize = strlen(wbuf);
size_t remaining_len = len - cursize;
retval = vsnprintf(wbuf+cursize, remaining_len, format, ap);
}
va_end(ap);
return retval;
}
char* MIOFILE::fgets(char* dst, int dst_len) {
if (f) {
#ifndef _USING_FCGI_
return ::fgets(dst, dst_len, f);
#else
return FCGI::fgets(dst, dst_len, f);
#endif
}
const char* q = strchr(buf, '\n');
if (!q) return 0;
q++;
int n = (int)(q - buf);
if (n > dst_len-1) n = dst_len-1;
memcpy(dst, buf, n);
dst[n] = 0;
buf = q;
return dst;
}
int MIOFILE::_ungetc(int c) {
if (f) {
#ifdef _USING_FCGI_
return FCGI_ungetc(c, f);
#else
return ungetc(c, f);
#endif
} else {
buf--;
// NOTE: we assume that the char being pushed
// is what's already there
//*buf = c;
}
return c;
}
// copy from a file to static buffer
//
int copy_element_contents(MIOFILE& in, const char* end_tag, char* p, int len) {
char buf[256];
int n;
strlcpy(p, "", len);
while (in.fgets(buf, 256)) {
if (strstr(buf, end_tag)) {
return 0;
}
n = (int)strlen(buf);
if (n >= len-1) return ERR_XML_PARSE;
strlcat(p, buf, len);
len -= n;
}
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;
}
|