pspfile.c
来自「db.* (pronounced dee-be star) is an adva」· C语言 代码 · 共 954 行 · 第 1/2 页
C
954 行
return bytes;}/* ========================================================================== Read from a file*/int psp_fileRead( PSP_FH handle, void *buf, size_t size){ register PSPFTAB *p = (PSPFTAB *) handle; int bytes; if (p->f_xflags & PSP_FLAG_SYNC) { /* TBD: Log out warning about accessing file needing synchronization */ vtprintf(DB_TEXT("Invalid file mode, file was opened with PSP_FLAG_SYNC\n")); } if (p->f_stream) { /* TBD: Log out warning about accessing stream file */ vtprintf(DB_TEXT("Invalid file mode, file is a stream\n")); } if (size == 0) return 0; BeginFileAccess(p); if ((bytes = psp_flRead(p->f_desc, buf, size)) == -1) { /* TBD: Log out error about write failure */ vtprintf(DB_TEXT("Read failed, errno = %d\n"), errno); } EndFileAccess(p); return bytes;}/* ========================================================================== Write to a specified location in a file*/int psp_fileSeekWrite( PSP_FH handle, size_t addr, const void *buf, size_t size){ register PSPFTAB *p = (PSPFTAB *) handle; int bytes; if (!(p->f_xflags & PSP_FLAG_SYNC)) { /* TBD: Log out warning about accessing a non-synchronized file */ vtprintf(DB_TEXT("Invalid file mode, file must be opened with PSP_FLAG_SYNC\n")); } if (p->f_stream) { /* TBD: Log out warning about accessing stream file */ vtprintf(DB_TEXT("Invalid file mode, file is a stream\n")); } if (size == 0) return 0; BeginFileAccess(p); bytes = psp_flSeekWrite(p->f_desc, addr, buf, size); EndFileAccess(p); if (bytes == -1) { /* TBD: Log out error on failure */ vtprintf(DB_TEXT("Failed to seek and write, errno = %d\n"), errno); } return bytes;}/* ========================================================================== Read from a specified location in a file*/int psp_fileSeekRead( PSP_FH handle, size_t addr, void *buf, size_t size){ register PSPFTAB *p = (PSPFTAB *) handle; int bytes; if (!(p->f_xflags & PSP_FLAG_SYNC)) { /* TBD: Log out warning about accessing a non-synchronized file */ vtprintf(DB_TEXT("Invalid file mode, file must be opened with PSP_FLAG_SYNC\n")); } if (p->f_stream) { /* TBD: Log out warning about accessing stream file */ vtprintf(DB_TEXT("Invalid file mode, file is a stream\n")); } if (size == 0) return 0; BeginFileAccess(p); bytes = psp_flSeekRead(p->f_desc, addr, buf, size); EndFileAccess(p); if (bytes == -1) { /* TBD: Log out error on failure */ vtprintf(DB_TEXT("Failed to seek and read, errno = %d\n"), errno); } return bytes;}/* ========================================================================== See if we need to read any more data from the file*/static int check_block( PSP_FDESC desc, PSPSTREAM *stream){ int bytes; if (stream->offset == PSP_BLKSIZE) { bytes = psp_flRead(desc, stream->block, PSP_BLKSIZE); if (bytes == 0) return 0; if (bytes != PSP_BLKSIZE) stream->block[bytes] = '\0'; stream->offset = 0; } return 1;}/* ========================================================================== Return the next string from a file*/char *psp_fileGets( PSP_FH handle, char *buf, size_t size){ register PSPFTAB *p = (PSPFTAB *) handle; register PSPSTREAM *stream; char *cur; char *cp = buf; char *pp; size_t len = 0; if (!p->f_stream) { /* TBD: Log out warning about invalid file open mode */ vtprintf(DB_TEXT("Invalid file mode, file is not a stream\n")); } BeginFileAccess(p); stream = p->f_stream; buf[0] = '\0'; do { if (!check_block(p->f_desc, stream)) break; cur = stream->block + stream->offset; if ((pp = strchr(cur, '\n')) != NULL) len = pp - cur + 1; else len = PSP_BLKSIZE; if (len > size - 1) len = size - 1; strncpy(cp, cur, len); cp[len] = '\0'; len = strlen(cp); cp += len; size -= len; stream->offset += len; } while (!pp && len && stream->offset == PSP_BLKSIZE); EndFileAccess(p); if (!len) return NULL; return buf;}/* ========================================================================== Return the next character from a file.*/char psp_fileGetc( PSP_FH handle){ register PSPFTAB *p = (PSPFTAB *) handle; register PSPSTREAM *stream; char ch = (char) -1; if (!p->f_stream) { /* TBD: Log out warning about invalid file open mode */ vtprintf(DB_TEXT("Invalid file mode, file is not a stream\n")); } BeginFileAccess(p); stream = p->f_stream; if (check_block(p->f_desc, stream)) { ch = stream->block[stream->offset]; if (ch) stream->offset++; } EndFileAccess(p); return ch;}/* ========================================================================== Return the length of a file.*/size_t psp_fileLength( PSP_FH handle){ register PSPFTAB *p = (PSPFTAB *) handle; size_t len; BeginFileAccess(p); len = psp_flSize(p->f_desc); EndFileAccess(p); return len;}/* ========================================================================== Set the current size of the file*/int psp_fileSetSize( PSP_FH handle, size_t size){ register PSPFTAB *p = (PSPFTAB *) handle; int ret; BeginFileAccess(p); ret = psp_flSetSize(p->f_desc, size); EndFileAccess(p); return ret;}/* ========================================================================== Lock a file*/short psp_fileLock( PSP_FH handle){ short ret; register PSPFTAB *p = (PSPFTAB *) handle; BeginFileAccess(p); if (p->f_xflags & PSP_FLAG_SYNC) psp_flSeek(p->f_desc, 0, SEEK_SET); ret = psp_flLock(p->f_desc); EndFileAccess(p); return ret;}/* ========================================================================== Unlock a file*/void psp_fileUnlock( PSP_FH handle){ register PSPFTAB *p = (PSPFTAB *) handle; BeginFileAccess(p); if (p->f_xflags & PSP_FLAG_SYNC) psp_flSeek(p->f_desc, 0, SEEK_SET); psp_flUnlock(p->f_desc); EndFileAccess(p);}/* ========================================================================== Synchronize writes to a file*/void psp_fileSync( PSP_FH handle){ register PSPFTAB *p = (PSPFTAB *) handle; BeginFileAccess(p); psp_flSync(p->f_desc); EndFileAccess(p);}/* ========================================================================== Return the last access time of a file*/long psp_fileLastAccess( PSP_FH handle){ register PSPFTAB *p = (PSPFTAB *) handle; long ret; BeginFileAccess(p); ret = psp_flLastAccess(p->f_desc); EndFileAccess(p); return ret;}/* ========================================================================== Return last modified time*/long psp_fileModTime( PSP_FH handle){ register PSPFTAB *p = (PSPFTAB *) handle; long ret; BeginFileAccess(p); ret = psp_flModTime(p->f_desc); EndFileAccess(p); return ret;} /* ========================================================================== Copy the contents of a file to a second file*/int psp_fileCopy( const DB_TCHAR *src, const DB_TCHAR *dest){ PSP_FH hSrc; PSP_FH hDest; char *buf; int bytes; if (vtstrcmp(src, dest) == 0) return PSP_OKAY; psp_fileRemove(dest); if ((hSrc = psp_fileOpen(src, O_RDONLY, 0)) == NULL) return PSP_FAILED; if ((hDest = psp_fileOpen(dest, O_CREAT|O_RDWR|O_EXCL, 0)) == NULL) { psp_fileClose(hSrc); return PSP_FAILED; } buf = psp_getMemory(PSP_BLKSIZE, 0); while ((bytes = psp_fileRead(hSrc, buf, PSP_BLKSIZE)) > 0) psp_fileWrite(hDest, buf, PSP_BLKSIZE); psp_fileClose(hSrc); psp_fileClose(hDest); psp_freeMemory(buf, 0); return PSP_OKAY;}/* ========================================================================== Move the contents of a file to a second file*/int psp_fileMove( const DB_TCHAR *src, const DB_TCHAR *dest){ int stat = PSP_OKAY; if (vtstrcmp(src, dest) == 0) return PSP_OKAY; psp_fileRemove(dest); if (psp_fileRename(src, dest)) { if ((stat = psp_fileCopy(src, dest)) == PSP_OKAY) psp_fileRemove(src); } return stat;}/* ========================================================================== Return the current file handle limit*/long psp_fileHandleLimit( void){ return handleLimit;}/* ========================================================================== Set the file handle limit*/void psp_fileSetHandleLimit( long limit){ handleLimit = limit;}/* =========================================================================== Get file extension if any*/DB_TCHAR * psp_getExt(const DB_TCHAR * path){ DB_TCHAR *d, *e; d = strrchr(path, DIRCHAR); e = strrchr(path, '.'); if (e == NULL || d != NULL && d > e) return NULL; else return (DB_TCHAR*)e;}/* =========================================================================== Strip extension form file name*/int psp_stripExt(DB_TCHAR * path, DB_TCHAR ** ext, const DB_TCHAR * old_ext){ DB_TCHAR *e; e = psp_getExt(path); if (e == NULL || (old_ext && vtstrcmp(old_ext, e) != 0)) return -1; *e = '\0'; if (ext) *ext = ++e; return 0;}/* =========================================================================== Replace file extension if it matches given, or append it*/int psp_replaceExt( DB_TCHAR * path, const DB_TCHAR * new_ext, const DB_TCHAR * old_ext, int max_size){ DB_TCHAR * e = psp_getExt(path); if (e != NULL && (old_ext == NULL || vtstrcmp(e, old_ext) == 0)) vtstrncpy(e, new_ext, max_size - (e - path)); else vtstrncat(path, new_ext, max_size - vtstrlen(path)); return 0;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?