Util: Cleanup of VFileZip and VFile7z

This commit is contained in:
Jeffrey Pfau 2015-03-05 18:40:33 -08:00
parent a268d462c0
commit 9adfcad1b5
4 changed files with 21 additions and 36 deletions

View File

@ -378,3 +378,15 @@ const char* _vdeName(struct VDirEntry* vde) {
} }
return 0; return 0;
} }
ssize_t VFileReadline(struct VFile* vf, char* buffer, size_t size) {
size_t bytesRead = 0;
while (bytesRead < size - 1) {
size_t newRead = vf->read(vf, &buffer[bytesRead], 1);
bytesRead += newRead;
if (!newRead || buffer[bytesRead] == '\n') {
break;
}
}
return buffer[bytesRead] = '\0';
}

View File

@ -52,4 +52,6 @@ struct VDir* VDirOpen7z(const char* path, int flags);
struct VFile* VDirOptionalOpenFile(struct VDir* dir, const char* realPath, const char* prefix, const char* suffix, int mode); struct VFile* VDirOptionalOpenFile(struct VDir* dir, const char* realPath, const char* prefix, const char* suffix, int mode);
struct VFile* VDirOptionalOpenIncrementFile(struct VDir* dir, const char* realPath, const char* prefix, const char* infix, const char* suffix, int mode); struct VFile* VDirOptionalOpenIncrementFile(struct VDir* dir, const char* realPath, const char* prefix, const char* infix, const char* suffix, int mode);
ssize_t VFileReadline(struct VFile* vf, char* buffer, size_t size);
#endif #endif

View File

@ -51,7 +51,6 @@ struct VFile7z {
static bool _vf7zClose(struct VFile* vf); static bool _vf7zClose(struct VFile* vf);
static off_t _vf7zSeek(struct VFile* vf, off_t offset, int whence); static off_t _vf7zSeek(struct VFile* vf, off_t offset, int whence);
static ssize_t _vf7zRead(struct VFile* vf, void* buffer, size_t size); static ssize_t _vf7zRead(struct VFile* vf, void* buffer, size_t size);
static ssize_t _vf7zReadline(struct VFile* vf, char* buffer, size_t size);
static ssize_t _vf7zWrite(struct VFile* vf, const void* buffer, size_t size); static ssize_t _vf7zWrite(struct VFile* vf, const void* buffer, size_t size);
static void* _vf7zMap(struct VFile* vf, size_t size, int flags); static void* _vf7zMap(struct VFile* vf, size_t size, int flags);
static void _vf7zUnmap(struct VFile* vf, void* memory, size_t size); static void _vf7zUnmap(struct VFile* vf, void* memory, size_t size);
@ -142,16 +141,12 @@ off_t _vf7zSeek(struct VFile* vf, off_t offset, int whence) {
return -1; return -1;
} }
if (position <= vf7z->offset) { if (position > vf7z->size) {
vf7z->offset = position; return -1;
return position;
} }
if (position <= vf7z->size) { vf7z->offset = position;
return vf7z->offset; return position;
}
return -1;
} }
ssize_t _vf7zRead(struct VFile* vf, void* buffer, size_t size) { ssize_t _vf7zRead(struct VFile* vf, void* buffer, size_t size) {
@ -162,21 +157,10 @@ ssize_t _vf7zRead(struct VFile* vf, void* buffer, size_t size) {
} }
memcpy(buffer, vf7z->outBuffer + vf7z->offset + vf7z->bufferOffset, size); memcpy(buffer, vf7z->outBuffer + vf7z->offset + vf7z->bufferOffset, size);
vf7z->offset += size;
return size; return size;
} }
ssize_t _vf7zReadline(struct VFile* vf, char* buffer, size_t size) {
size_t bytesRead = 0;
while (bytesRead < size - 1) {
size_t newRead = vf->read(vf, &buffer[bytesRead], 1);
bytesRead += newRead;
if (!newRead || buffer[bytesRead] == '\n') {
break;
}
}
return buffer[bytesRead] = '\0';
}
ssize_t _vf7zWrite(struct VFile* vf, const void* buffer, size_t size) { ssize_t _vf7zWrite(struct VFile* vf, const void* buffer, size_t size) {
// TODO // TODO
UNUSED(vf); UNUSED(vf);
@ -301,7 +285,7 @@ struct VFile* _vd7zOpenFile(struct VDir* vd, const char* path, int mode) {
vf->d.close = _vf7zClose; vf->d.close = _vf7zClose;
vf->d.seek = _vf7zSeek; vf->d.seek = _vf7zSeek;
vf->d.read = _vf7zRead; vf->d.read = _vf7zRead;
vf->d.readline = _vf7zReadline; vf->d.readline = VFileReadline;
vf->d.write = _vf7zWrite; vf->d.write = _vf7zWrite;
vf->d.map = _vf7zMap; vf->d.map = _vf7zMap;
vf->d.unmap = _vf7zUnmap; vf->d.unmap = _vf7zUnmap;

View File

@ -38,7 +38,6 @@ struct VFileZip {
static bool _vfzClose(struct VFile* vf); static bool _vfzClose(struct VFile* vf);
static off_t _vfzSeek(struct VFile* vf, off_t offset, int whence); static off_t _vfzSeek(struct VFile* vf, off_t offset, int whence);
static ssize_t _vfzRead(struct VFile* vf, void* buffer, size_t size); static ssize_t _vfzRead(struct VFile* vf, void* buffer, size_t size);
static ssize_t _vfzReadline(struct VFile* vf, char* buffer, size_t size);
static ssize_t _vfzWrite(struct VFile* vf, const void* buffer, size_t size); static ssize_t _vfzWrite(struct VFile* vf, const void* buffer, size_t size);
static void* _vfzMap(struct VFile* vf, size_t size, int flags); static void* _vfzMap(struct VFile* vf, size_t size, int flags);
static void _vfzUnmap(struct VFile* vf, void* memory, size_t size); static void _vfzUnmap(struct VFile* vf, void* memory, size_t size);
@ -188,18 +187,6 @@ ssize_t _vfzRead(struct VFile* vf, void* buffer, size_t size) {
return bytesRead; return bytesRead;
} }
ssize_t _vfzReadline(struct VFile* vf, char* buffer, size_t size) {
size_t bytesRead = 0;
while (bytesRead < size - 1) {
size_t newRead = vf->read(vf, &buffer[bytesRead], 1);
bytesRead += newRead;
if (!newRead || buffer[bytesRead] == '\n') {
break;
}
}
return buffer[bytesRead] = '\0';
}
ssize_t _vfzWrite(struct VFile* vf, const void* buffer, size_t size) { ssize_t _vfzWrite(struct VFile* vf, const void* buffer, size_t size) {
// TODO // TODO
UNUSED(vf); UNUSED(vf);
@ -296,7 +283,7 @@ struct VFile* _vdzOpenFile(struct VDir* vd, const char* path, int mode) {
vfz->d.close = _vfzClose; vfz->d.close = _vfzClose;
vfz->d.seek = _vfzSeek; vfz->d.seek = _vfzSeek;
vfz->d.read = _vfzRead; vfz->d.read = _vfzRead;
vfz->d.readline = _vfzReadline; vfz->d.readline = VFileReadline;
vfz->d.write = _vfzWrite; vfz->d.write = _vfzWrite;
vfz->d.map = _vfzMap; vfz->d.map = _vfzMap;
vfz->d.unmap = _vfzUnmap; vfz->d.unmap = _vfzUnmap;