lib/tdl-util.c
/**
 * Creates a new empty tdl file. This will setup 
 *
 * Proper header: 219  84  68  76  13  10  26  10
 * ASCII  header: INV   T   D   L  \r  \n \032 \n
 * We use this combination of invalid ascii and different returns to make
 * the file not openable with text editors since it is a binary file.
 *
 * @param char* path - the path to the file and the file name. 
 * 				       ".tdl" will be appended to the file name if
 *					   it is not specified in the path.
 *		  char* name - the name of the object that the file contains.
 *					   There is a limit of 32 chars for the object name,
 *					   anything longer will be truncated.
 *
 * @return FILE* - a file descripter pointing to the newly created file.
 */
Creates a new empty tdl file. This will setup
Proper header: 219  84  68  76  13  10  26  10
ASCII  header: INV   T   D   L  \r  \n \032 \n
We use this combination of invalid ascii and different returns to make
the file not openable with text editors since it is a binary file.

@param char* path - the path to the file and the file name.
".tdl" will be appended to the file name if
it is not specified in the path.
char* name - the name of the object that the file contains.
There is a limit of 32 chars for the object name,
anything longer will be truncated.

@return FILE* - a file descripter pointing to the newly created file.
Creates a new empty tdl file . This will setup Proper header : 219 84 68 76 13 10 26 10 ASCII header : INV T D L \ r \ n \ 032 \ n We use this combination of invalid ascii and different returns to make the file not openable with text editors since it is a binary file . @param char * path - the path to the file and the file name . " . tdl " will be appended to the file name if it is not specified in the path . char * name - the name of the object that the file contains . There is a limit of 32 chars for the object name anything longer will be truncated . @return FILE * - a file descripter pointing to the newly created file .
FILE* tdl_create(const char* path, const char* name)
{
#ifdef _WIN32
	msg(MSG_ERROR, "This function is not defined on Windows.");
	return NULL;
#else
	int pathLen = strlen(path);
	int nameLen = strlen(name);
	nameLen = nameLen > 32 ? 32 : nameLen;
	char buff[1024];
	if(pathLen < 4 || strncmp(path + pathLen - 4, ".tdl", 4) != 0)
	{
		int retVal = snprintf(buff, 1024, "%s.tdl", path);
		if(retVal >= 1024)
			msg(MSG_WARNING, "Path provided to tdl_create() was too long: %s", path);
		path = buff;
		pathLen += 4;
	}
	FILE *f = fopen(path, "w");
	if(f == NULL)
	{
		msg(MSG_ERROR, "Failed to create file %s", path);
		return f;
	}
	unsigned char header[9];
	header[0] = 219;
	header[1] = 84;
	header[2] = 68;
	header[3] = 76;
	header[4] = 13;
	header[5] = 10;
	header[6] = 26;
	header[7] = 10;
	header[8] = 0;
	if(fwrite(header, 9, 1, f) != 1)
	{
		msg(MSG_ERROR, "Failed to write header to %s", path);
		return f;
	}
	if(fwrite(name, nameLen+1, 1, f) != 1)
	{
		msg(MSG_ERROR, "Failed to write object name to %s", path);
		return f;
	}
	return f;
#endif
}
FILE * tdl_create ( const char * path , const char * name ) { #ifdef _WIN32 msg ( MSG_ERROR , " " ) ; return NULL ; #else int pathLen = strlen ( path ) ; int nameLen = strlen ( name ) ; nameLen = nameLen > 32 ? 32 : nameLen ; char buff [ 1024 ] ; if ( pathLen < 4 || strncmp ( path + pathLen - 4 , " " , 4 ) != 0 ) { int retVal = snprintf ( buff , 1024 , " " , path ) ; if ( retVal >= 1024 ) msg ( MSG_WARNING , " " , path ) ; path = buff ; pathLen += 4 ; } FILE * f = fopen ( path , " " ) ; if ( f == NULL ) { msg ( MSG_ERROR , " " , path ) ; return f ; } unsigned char header [ 9 ] ; header [ 0 ] = 219 ; header [ 1 ] = 84 ; header [ 2 ] = 68 ; header [ 3 ] = 76 ; header [ 4 ] = 13 ; header [ 5 ] = 10 ; header [ 6 ] = 26 ; header [ 7 ] = 10 ; header [ 8 ] = 0 ; if ( fwrite ( header , 9 , 1 , f ) != 1 ) { msg ( MSG_ERROR , " " , path ) ; return f ; } if ( fwrite ( name , nameLen + 1 , 1 , f ) != 1 ) { msg ( MSG_ERROR , " " , path ) ; return f ; } return f ; #endif }
Creates a new empty tdl file.
Creates a new empty tdl file .
// Add .tdl to the path if it was missing from the path provided // by the caller. //INV ascii char for identifier //T //D //L //\r //\n //\032 //\n //NULL term //We +1 to the name length so that the term char is also written.