#include "int_range.h"
static long file_size(const char * const pFname){
FileSizeGetterContext ctx = {{pFname, "r", size_reader}, 0};
if(!access_file(&ctx.base)){
return -1;
}
return ctx.size + 1; //需要为内存多分配一个字节
}
static void size_reader(FileAcessorContext *p, FILE *fp){
FileSizeGetterContext *pThis = (FileSizeGetterContext *)p;
pThis->size = -1;
if(fseek(fp, 0, SEEK_END) == 0)
pThis->size = ftell(fp);
}
static long buffer_allocate(long size, Context *pAppCtx){
MyBufferContext bufCtx = {{NULL, size, do_with_buffer}, pAppCtx};
if(!buffer(&bufCtx.base)){
return -1;
}
return size;
}
IntRangeError int_range(const char * const pFname){
Context ctx = {pFname, ERR_CAT_OK};
long size = file_size(pFname);
if(size == -1){
file_error(&ctx);
return ctx.errorCatgory;
}
long buffer_size = buffer_allocate(size, &ctx);
if(buffer_size == -1){
buffer_error(&ctx);
return ctx.errorCatgory;
}
}
static void file_error(Context *pCtx){
printf("Failed to open File: %s\n", pCtx->pFname);
pCtx->errorCatgory = ERR_CAT_FILE;
}
static void buffer_error(Context *pCtx){
printf("Failed to malloc memory\n");
pCtx->errorCatgory = ERR_CAT_MEMORY;
}
static void do_with_buffer(BufferContext *p) {
MyBufferContext *pBufCtx = (MyBufferContext *)p;
MyFileAccessorContext readFileCtx = {{pBufCtx->pAppCtx->pFname, "rb", reader}, pBufCtx};
if(!access_file(&readFileCtx.base)){
file_error(pBufCtx->pAppCtx);
return;
}
int value = range_processor(p->pBuf, p->size);
printf("value is %d\n", value);
MyFileAccessorContext writeFileCtx = {{pBufCtx->pAppCtx->pFname, "wb", writer}, pBufCtx};
if(!access_file(&readFileCtx.base)){
file_error(pBufCtx->pAppCtx);
return;
}
}
static void reader(FileAcessorContext *p, FILE *fp){
MyFileAccessorContext *pFileCtx = (MyFileAccessorContext *)p;
MyBufferContext *pBufCtx = pFileCtx->pBufCtx;
if(fgets(pBufCtx->base.pBuf, pBufCtx->base.size, fp) == NULL){
file_error(pBufCtx->pAppCtx);
}
}
static void writer(FileAcessorContext *p, FILE *fp){
MyFileAccessorContext *pFileCtx = (MyFileAccessorContext *)p;
MyBufferContext *pBufCtx = pFileCtx->pBufCtx;
if(fputs(pBufCtx->base.pBuf, fp)){
file_error(pBufCtx->pAppCtx);
}
}
static int range_processor(char *buf, size_t size){
int min = INT_MAX;
int max = INT_MIN;
char *temp = strtok(buf, " ");
while(temp){
int value = atoi(temp);
min = min > value ? value : min;
max = max < value ? value : max;
temp = strtok(NULL, " ");
}
return max - min;
}