//@@Module User FileEx ******************************************************
// Name:
// FileEx
//
// Author:
// Gordon Tackett
//
// Date:
// 04/01/2012
//
// Copyright:
// copyright 2012 by Westmarch Studios. All rights reserved.
//
// Description:
// This module is an extension of the file_ functions. If the file exists
// the regular file functions are used. If however the file is not found
// the extension is changed to "PAK
//
// History:
// 04/01/2012 first release.
//
//@@End**********************************************************************
#ifndef fileex_c
#define fileex_c
#include <acknex.h>
#include <stdio.h>
#define PRAGMA_POINTER
//@@type internal FILEEX ------------------------------------------------------
//
// Name:
// FILEEX
//
// Description:
// an opake type that is used to hold information about where the file is
// and what the read position is.
//
// Usage:
// use internally to file_XXX_ex functions only
//
//@@End-----------------------------------------------------------------------
typedef struct {
char* buffer;
int len;
int pos;
var handle;
int fileOrResource;
int lastreadsize;
} FILEEX;
//@function API file_open_read_ex --------------------------------------------
//
// Name:
// file_open_read_ex
//
// Description:
// This allocates a FILEEX structure on the heap then tries to open the
// requested file with file_open_read. If this fails it changes the
// extension of the file to "pak" then uses file_load to see if it can
// be loaded from a resource file. If this fails then NULL is returned.
// if either of the other methods work then a pointer to the allocated
// memory is returned.
//
// Usage:
// file_open_read_ex("filename.ext");
//
// Parameters:
// # filename - STRING pointer.
//
// Output:
// void * - NULL for file not open, non-null for open
//
//*@@End----------------------------------------------------------------------
void* file_open_read_ex(STRING* filename)
{
FILEEX* file;
STRING* tmpstr = "#128";
int index;
int size;
file = malloc(sizeof(FILEEX));
if (file != NULL) {
file->handle = file_open_read(filename);
file->fileOrResource = 0;
if (file->handle == NULL) {
file->fileOrResource = 1;
str_cpy(tmpstr,filename);
index = str_stri(tmpstr,".");
if (index != 0) {
str_trunc(tmpstr,str_len(tmpstr) - index);
}
str_cat(tmpstr,"pak");
size = 0;
file->buffer = file_load(_chr(tmpstr),NULL,&size);
if (size > 0) {
file->len = size;
file->pos = 0;
file->lastreadsize = 0;
} else {
free(file);
file = NULL;
}
}
}
return(file);
}
//@function API file_close_ex ------------------------------------------------
//
// Name:
// file_close_ex
//
// Description:
// closes the file and deallocates any memory allocated.
//
// Usage:
// file_close_ex(handle);
//
// Parameters:
// # handle - void* to a FILEEX structure
//
// Output:
// No return value.
//
//*@@End----------------------------------------------------------------------
void file_close_ex(void* handle)
{
FILEEX* file;
file = handle;
if (file == NULL) {
return;
}
if (file->fileOrResource) {
file_load(NULL,file->buffer,NULL);
free(file);
return;
} else {
file_close(file->handle);
free(file);
}
}
//@function API file_str_read_ex ---------------------------------------------
//
// Name:
// file_str_read_ex
//
// Description:
// This function works just like file_str_read but if it is reading from
// resource file it will copy to a buffer in the function then str_cpy
// the buffer to lite-c string. line size is limited to 256 bytes.
//
// Usage:
// file_str_read_ex(handle, str);
//
// Parameters:
// # handle - void * pointer pointing to a FILEEX structure;
//
// Output:
// int - length of line.
//
//*@@End----------------------------------------------------------------------
int file_str_read_ex(void* handle, STRING* str)
{
FILEEX* file;
int len;
char tmpstr[257];
file = handle;
if (file == NULL) {
return(-1);
}
if (!file->fileOrResource) {
file->lastreadsize = file_str_read(file->handle,str);
return(file->lastreadsize);
}
if (file->pos >= file->len) {
str_cpy(str,"");
file->lastreadsize = -1;
return(file->lastreadsize);
}
len = 0;
while (len < 256 && (file->buffer)[file->pos] >= 32 && file->pos < file->len) {
tmpstr[len] = (file->buffer)[file->pos];
len++;
file->pos++;
}
tmpstr[len] = 0;
while((file->buffer)[file->pos] < 32 && file->pos < file->len) {
file->pos++;
}
str_cpy(str, _str(tmpstr));
file->lastreadsize = len;
return(file->lastreadsize);
}
//@function API file_eof --------------------------------------------
//
// Name:
// file_eof
//
// Description:
// simple extention function to check for end of file.
//
// Usage:
// file_eof(handle);
//
// Parameters:
// # handle - void* to FILEEX structure
//
// Output:
// true for end of file false otherwise.
//
//*@@End----------------------------------------------------------------------
int file_eof(void* handle)
{
FILEEX* file;
file = handle;
return(file->lastreadsize == -1);
}
#endif