標準C語言中的檔案操作函式的記憶體版
阿新 • • 發佈:2019-01-29
1. 初衷
專案需要,itron作業系統中實裝freetype+harfbuzz的字型引擎,但itron系統中沒有支援檔案系統!
所以只能將依賴庫中的libdatrie和libthai中的檔案操作換成記憶體版。
2. 程式碼
廢話不說,直接上程式碼。
易知,上述API和C語言中的版本分別對應。#include "memfile.h" #include <assert.h> #include <string.h> /* Create an new mem file. */ MEMFILE* mfcreate(int size) { MEMFILE* mf = (MEMFILE*)malloc(sizeof(MEMFILE)); mf->ptr = (char*)malloc(size); mf->size = size; mf->cursor = 0; return mf; } /* Open an exist mem file. */ MEMFILE* mfopen(char* ptr, int size) { MEMFILE* mf = (MEMFILE*)malloc(sizeof(MEMFILE)); mf->ptr = ptr; mf->size = size; mf->cursor = 0; return mf; } /* Close an mem file. */ extern void mfclose(MEMFILE* mf, Bool created) { if (created) { free(mf->ptr); } free(mf); } /* Seek to a certain position on MEMFILE. */ int mfseek (MEMFILE *mf, long int offset, int whence) { int idxEnd = mf->size - 1; switch (whence) { case SEEK_SET: { if (offset >= 0 && offset <= idxEnd) { mf->cursor = offset; return 0; } } break; case SEEK_CUR: { if ((offset + mf->cursor) >= 0 && (offset + mf->cursor) <= idxEnd) { mf->cursor += offset; return 0; } } break; case SEEK_END: { if ((offset + idxEnd) >= 0 && (offset + idxEnd) <= idxEnd) { mf->cursor = idxEnd + offset + 1; return 0; } } break; default: break; } return -1; } /* Return the current position of MEMFILE. */ long int mftell (MEMFILE *mf) { return mf->cursor; } /* Rewind to the beginning of MEMFILE. */ void mfrewind (MEMFILE *mf) { mf->cursor = 0; } /* Read chunks of generic data from MEMFILE. */ size_t mfread (void *ptr, size_t size, size_t n, MEMFILE *mf) { if (size > (mf->size - mf->cursor)) { return 0; } int read_count = 0; while ((read_count < n) && (read_count*size <= (mf->size - mf->cursor))) { read_count++; } memcpy(ptr, mf->ptr + mf->cursor, read_count*size); mf->cursor += read_count*size; assert(mf->cursor <= mf->size); return read_count; } /* Write chunks of generic data to MEMFILE. */ size_t mfwrite (const void *ptr, size_t size, size_t n, MEMFILE *mf) //TODO: memfile will *NOT* malloc incrementlly! { if (size > (mf->size - mf->cursor)) { return 0; } int write_count = 0; while ((write_count < n) && (write_count*size <= (mf->size - mf->cursor))) { write_count++; } memcpy(mf->ptr + mf->cursor, ptr, write_count*size); mf->cursor += write_count*size; assert(mf->cursor <= mf->size); return write_count; }
如有需要請拿去。github地址在此處。