File: mfile.cpp

package info (click to toggle)
boinc 7.14.2+dfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 111,132 kB
  • sloc: cpp: 163,589; php: 113,173; ansic: 49,284; pascal: 35,620; xml: 17,864; java: 13,521; python: 6,551; sh: 4,082; perl: 1,843; makefile: 1,796; objc: 1,543; sql: 959; csh: 126; lisp: 47
file content (191 lines) | stat: -rw-r--r-- 4,473 bytes parent folder | download | duplicates (5)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// 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 <cstdio>
#include <cstdlib>
#include <cstdarg>
#include <cstring>
#include <string>
#include <cerrno>
#include <unistd.h>
#endif

#include "error_numbers.h"
#include "filesys.h"

#include "mfile.h"

MFILE::MFILE() {
    buf = (char*)malloc(64*1024);
    len = 0;
    f = NULL;
}

MFILE::~MFILE() {
    close();
}

int MFILE::open(const char* path, const char* mode) {
    f = boinc_fopen(path, mode);
    if (!f) return ERR_FOPEN;
    if (!buf) buf = (char*)malloc(64*1024);
    return 0;
}

// seems like Win's realloc is stupid,  Make it smart.
//
static inline char* realloc_aux(char* p, size_t len) {
    if (!p) {
        return (char*)malloc(64*1024);
    }
#ifdef _WIN32
    if (_msize(p) >= (unsigned int)len) return p;
    return (char*) realloc(p, len*2);
#else
    return (char*) realloc(p, len);
#endif
}

#define BUFSIZE 100000

int MFILE::vprintf(const char* format, va_list ap) {
    char buf2[BUFSIZE];
    int n, k;

    k = vsnprintf(buf2, BUFSIZE, format, ap);
    if (k<=-1 || k>=BUFSIZE) {
        fprintf(stderr, "ERROR: buffer too small in MFILE::vprintf()\n");
        fprintf(stderr, "ERROR: format: %s\n", format);
        fprintf(stderr, "ERROR: k=%d, BUFSIZE=%d\n", k, BUFSIZE);
        return -1;
    }
    n = (int)strlen(buf2);
    buf = (char*)realloc_aux(buf, len+n+1);
    if (!buf) {
        fprintf(stderr,
            "ERROR: realloc() failed in MFILE::vprintf(); len %d n %d\n",
            len, n
        );
        exit(1);
    }
    strncpy(buf+len, buf2, n);
    len += n;
    buf[len] = 0;
    return k;
}

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;
}

size_t MFILE::write(const void *ptr, size_t size, size_t nitems) {
    buf = (char *)realloc_aux( buf, len+(size*nitems)+1 );
    if (!buf) {
        fprintf(stderr,
            "ERROR: realloc() failed in MFILE::write(); len %d size %lu nitems %lu\n",
            len, size, nitems
        );
        exit(1);
    }
    memcpy( buf+len, ptr, size*nitems );
    len += (int)size*(int)nitems;
    buf[len] = 0;
    return nitems;
}

int MFILE::_putchar(char c) {
    buf = (char*)realloc_aux(buf, len+1+1);
    if (!buf) {
        fprintf(stderr,
            "ERROR: realloc() failed in MFILE::_putchar(); len %d\n", len
        );
        exit(1);
    }
    buf[len] = c;
    len++;
    buf[len] = 0;
    return c;
}

int MFILE::puts(const char* p) {
    int n = (int)strlen(p);
    buf = (char*)realloc_aux(buf, len+n+1);
    if (!buf) {
        fprintf(stderr,
            "ERROR: realloc() failed in MFILE::puts() len %d n %d\n", len, n
        );
        exit(1);
    }
    strncpy(buf+len, p, n);
    len += n;
    buf[len] = 0;
    return n;
}

// This is closing the file even if it couldn't flush it.
// The caller needs to check the return value and open the file again before the next retry.
//
int MFILE::close() {
    int retval = 0;
    if (f) {
        retval = flush();
        fclose(f);
        f = NULL;
    }
    if (buf) {
        free(buf);
        buf = NULL;
    }
    return retval;
}

int MFILE::flush() {
    int n, old_len = len;

    n = (int)fwrite(buf, 1, len, f);
    len = 0;
    if (n != old_len) return ERR_FWRITE;
    if (fflush(f)) return ERR_FFLUSH;
#ifndef _WIN32
    if (fsync(fileno(f)) < 0) return ERR_FSYNC;
#endif
    return 0;
}

long MFILE::tell() const {
    return f ? ftell(f) : -1;
}

void MFILE::get_buf(char*& b, int& l) {
    b = buf;
    l = len;
    buf = 0;
    len = 0;
}