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
|
//========================================================================
//
// ocfile.cpp
//
// Drop-in replacement for C <stdio.h> FILE
//
//========================================================================
#include "ocfile.h"
#include <qfile.h>
#include <qstring.h>
#include <list>
#include <cassert>
// --------------------------------------------------------------------
// This function is from Xpdf.
// It is here since the file that contained it originally is gone.
char *getLine(char *buf, int size, OCFILE *f)
{
int c, i;
i = 0;
while (i < size - 1) {
if ((c = fgetc(f)) == EOF) {
break;
}
buf[i++] = (char)c;
if (c == '\x0a') {
break;
}
if (c == '\x0d') {
c = fgetc(f);
if (c == '\x0a' && i < size - 1) {
buf[i++] = (char)c;
} else if (c != EOF) {
ungetc(c, f);
}
break;
}
}
buf[i] = '\0';
if (i == 0) {
return NULL;
}
return buf;
}
// --------------------------------------------------------------------
/* All files ever opened are kept here. */
class OcFileStore {
public:
OCFILE *Open(const char *name, const char *mode);
OCFILE *Open(QIODevice *device);
~OcFileStore();
private:
std::list<OCFILE> iFiles;
};
static OcFileStore store;
OcFileStore::~OcFileStore()
{
for (std::list<OCFILE>::iterator it = iFiles.begin();
it != iFiles.end(); ++it) {
if (it->iDevice) {
delete it->iDevice;
it->iDevice = 0;
}
}
}
OCFILE *OcFileStore::Open(const char *name, const char *mode)
{
QFile *file = new QFile(QString::fromUtf8(name));
int m = (mode[0] == 'w') ? IO_WriteOnly : IO_ReadOnly;
if (!file->open(m)) {
delete file;
return 0;
}
iFiles.push_back(OCFILE());
iFiles.back().iDevice = file;
return &iFiles.back();
}
OCFILE *OcFileStore::Open(QIODevice *device)
{
iFiles.push_back(OCFILE());
iFiles.back().iDevice = device;
return &iFiles.back();
}
// --------------------------------------------------------------------
extern OCFILE *ocfopen(const char *name, const char *mode)
{
return store.Open(name, mode);
}
extern OCFILE *ocfopen(QIODevice *device)
{
return store.Open(device);
}
extern void fclose(OCFILE *f)
{
f->iDevice->close();
f->iDevice = 0;
}
extern size_t fwrite(const void *buf, size_t size, size_t count, OCFILE *f)
{
assert(size == 1);
return f->iDevice->writeBlock((const char *) buf, count);
}
extern size_t fread(void *buf, size_t size, size_t count, OCFILE *f)
{
assert(size == 1);
return f->iDevice->readBlock((char *) buf, count);
}
extern int fseek(OCFILE *f, long offset, int whence)
{
if (!f->iDevice)
return -1;
bool res;
if (whence == SEEK_SET)
res = f->iDevice->at(offset);
else if (whence == SEEK_END)
res = f->iDevice->at(offset + f->iDevice->size());
else
assert(false);
return res ? 0 : -1;
}
extern long ftell(OCFILE *f)
{
return f->iDevice->at();
}
extern int fgetc(OCFILE *f)
{
return f->iDevice->getch();
}
extern int fputc(int ch, OCFILE *f)
{
return f->iDevice->putch(ch);
}
extern void ungetc(int ch, OCFILE *f)
{
f->iDevice->ungetch(ch);
}
extern void fputs(const char *s, OCFILE *f)
{
f->iDevice->writeBlock(s, strlen(s));
}
extern void fprintf(OCFILE *f, const char *fmt, int a1, int a2)
{
char buf[30];
sprintf(buf, fmt, a1, a2);
f->iDevice->writeBlock(buf, strlen(buf));
}
extern void fprintf(OCFILE *f, const char *fmt, const char *s)
{
assert(strlen(fmt) + strlen(s) < 200);
char buf[200];
sprintf(buf, fmt, s);
f->iDevice->writeBlock(buf, strlen(buf));
}
// --------------------------------------------------------------------
|