Gamestudio Links
Zorro Links
Newest Posts
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
3 registered members (vicknick, dr_panther, VoroneTZ), 1,255 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
firatv, wandaluciaia, Mega_Rod, EternallyCurious, howardR
19050 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
file extentions... #398486
04/02/12 17:17
04/02/12 17:17
Joined: Aug 2003
Posts: 902
Van Buren, Ar
Gordon Offline OP
User
Gordon  Offline OP
User

Joined: Aug 2003
Posts: 902
Van Buren, Ar
I needed a simple file extension for reading config files from published games (pro).. would like to add the rest of the file_xxx_read functions but no time right now... maybe someone else can take what I did and add to it ;-)

header
Code:
//@@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_h
#define fileex_h

void* file_open_read_ex(STRING* filename);
void file_close_ex(void* handle);
int file_str_read_ex(void* handle, STRING* str);
int file_eof(void* handle);

#endif



the code
Code:
//@@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



enjoy cool


Our new web site:Westmarch Studios
Re: file extentions... [Re: Gordon] #400468
05/01/12 16:02
05/01/12 16:02
Joined: Jan 2012
Posts: 39
B
BySharDe Offline
Newbie
BySharDe  Offline
Newbie
B

Joined: Jan 2012
Posts: 39
file IO is always useful.
thanks.

Re: file extentions... [Re: BySharDe] #400536
05/02/12 23:50
05/02/12 23:50
Joined: Aug 2003
Posts: 902
Van Buren, Ar
Gordon Offline OP
User
Gordon  Offline OP
User

Joined: Aug 2003
Posts: 902
Van Buren, Ar
You welcome


Our new web site:Westmarch Studios
Re: file extentions... [Re: Gordon] #400769
05/07/12 16:36
05/07/12 16:36
Joined: Apr 2012
Posts: 62
wrekWIRED Offline
Junior Member
wrekWIRED  Offline
Junior Member

Joined: Apr 2012
Posts: 62
but do you know how to encrypt a file and only the game engine can read it?

Re: file extentions... [Re: wrekWIRED] #400870
05/08/12 22:59
05/08/12 22:59
Joined: Aug 2003
Posts: 902
Van Buren, Ar
Gordon Offline OP
User
Gordon  Offline OP
User

Joined: Aug 2003
Posts: 902
Van Buren, Ar
You can use any standard encryption. You would need to build this in lite-c and compile to exe so source and key would be hidden. It could even be something simple like xor with pie starting at a location based on a hash of the file name. 2048 bits of pie fraction should be enough to keep most people out of a text file. It could even be built into these routines.


Our new web site:Westmarch Studios
Re: file extentions... [Re: Gordon] #400876
05/09/12 07:10
05/09/12 07:10
Joined: Mar 2011
Posts: 3,150
Budapest
sivan Offline
Expert
sivan  Offline
Expert

Joined: Mar 2011
Posts: 3,150
Budapest
there is an AUM with a simple encryption example using ascii file writing/reading.


Free world editor for 3D Gamestudio: MapBuilder Editor

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1