/* *%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% * SCR_File.c * Managment of files. * * Scripts, Beaudet Christophe *%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ /* *================================================================================================= * Includes. *================================================================================================= */ #include #include #include #include #include "SCR.h" /* *================================================================================================= * Global variables. *================================================================================================= */ /* * Array of all open files. */ SCR_tdst_DyAr_Description SCR_g_st_File_ArrayOpen; SCR_tda_st_Hash_Table SCR_g_a_st_File_HashTableOpen; /* * Array of all paths. */ SCR_tdst_DyAr_Description SCR_g_st_File_ArrayPaths; /* * Priority path. */ char SCR_g_a_sz_File_PriorityPath[_MAX_PATH]; int SCR_g_iParseHeader=0; /* *================================================================================================= * Functions. *================================================================================================= */ /* *------------------------------------------------------------------------------------------------- * Init. *------------------------------------------------------------------------------------------------- */ void fn_v_File_InitModule(void) { fn_v_DyAr_InitArray(&SCR_g_st_File_ArrayOpen); fn_v_Hash_InitTable(SCR_g_a_st_File_HashTableOpen); fn_v_DyAr_InitArray(&SCR_g_st_File_ArrayPaths); *SCR_g_a_sz_File_PriorityPath = '\0'; } /* *------------------------------------------------------------------------------------------------- * Close. *------------------------------------------------------------------------------------------------- */ void fn_v_File_CloseModule(void) { fn_v_File_CloseAllHandles(); SCR_M_DyAr_DeleteAllElements ( SCR_tdst_File_Open, &SCR_g_st_File_ArrayOpen, fn_v_File_DeleteInsideOpen((SCR_tdst_File_Open *) _p_stElement_->d_vElement); ); SCR_M_DyAr_DeleteAllElements ( SCR_tdst_File_Path, &SCR_g_st_File_ArrayPaths, ; ); fn_v_Hash_CloseTable(SCR_g_a_st_File_HashTableOpen); } /* *------------------------------------------------------------------------------------------------- * To init a structure of a parse file. Page are all NULL. * _p_stFile : File to init. * _p_szFileName : Name of file. * _p_xHandle : Handle of file. *------------------------------------------------------------------------------------------------- */ void SCR_fn_v_File_InitFileDes ( SCR_tdst_File_Description *_p_stFile, char *_p_szFileName, SCR_FILE xHandle ) { /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ SCR_M_Dbg_Assert_P(_p_stFile != NULL); SCR_M_Dbg_Assert_P(_p_szFileName != NULL); /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ fn_v_Mem_SetMode(0); SCR_M_Mem_Alloc(SCR_tdst_File_Handle, _p_stFile->p_stHandle, 1); _p_stFile->p_stHandle->xHandle = xHandle; _p_stFile->p_stHandle->p_cBase = _p_stFile->p_stHandle->p_cCurrent = NULL; _p_stFile->p_stHandle->iCnt = 0; strcpy(_p_stFile->a_szFileName, _p_szFileName); strcpy(_p_stFile->a_szOpenFileName, _p_szFileName); _p_stFile->d_stFirstPage = _p_stFile->d_stLastPage = _p_stFile->d_stCurPage = NULL; _p_stFile->iCurPosInPage = 0; SCR_M_Mem_Alloc( SCR_tdst_Refs, _p_stFile->p_stRefs, 1 ); /*_p_stFile->p_stRefs=NULL;*/ _p_stFile->p_stRefs->a_szRefName=NULL; _p_stFile->p_stRefs->sRefCount=0; _p_stFile->bBinaryMode = 0; } /* *------------------------------------------------------------------------------------------------- * To close a structure of a parse file. * _p_stFile : File to close. *------------------------------------------------------------------------------------------------- */ void SCR_fn_v_File_CloseFileDes(SCR_tdst_File_Description *_p_stFile) { int i; if( _p_stFile->p_stRefs ) { if( _p_stFile->p_stRefs->a_szRefName ) { for( i=0; i<_p_stFile->p_stRefs->sRefCount; i++ ) SCR_M_Mem_Free( _p_stFile->p_stRefs->a_szRefName[i] ); SCR_M_Mem_Free( _p_stFile->p_stRefs->a_szRefName ); _p_stFile->p_stRefs->a_szRefName=NULL; _p_stFile->p_stRefs->sRefCount=0; } SCR_M_Mem_Free( _p_stFile->p_stRefs ); _p_stFile->p_stRefs=NULL; } if(_p_stFile->p_stHandle) { SCR_M_File_Close(_p_stFile); fn_v_Mem_SetMode(0); SCR_M_Mem_Free(_p_stFile->p_stHandle); } } /*===============================================================================================*/ /* *------------------------------------------------------------------------------------------------- * To add one open file. * _p_szFileName : Name of file to add. It's the user name without additional path. * _xHashKey : Hash key of file name. * Returns allocated structure. *------------------------------------------------------------------------------------------------- */ SCR_tdst_File_Open *fnp_st_File_AddOpen(char *_p_szFileName, SCR_tdx_Hash_Key _xHashKey) { /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ unsigned int uiPos; SCR_tdst_File_Open *p_stFile; unsigned int uiIndexOfHashValue; /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ SCR_M_Dbg_Assert_P(_p_szFileName != NULL); /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ /* * Add element. */ SCR_M_DyAr_AddElement(SCR_tdst_File_Open, uiPos, p_stFile, SCR_g_st_File_ArrayOpen); fnp_st_Hash_AddValue ( _xHashKey, SCR_g_a_st_File_HashTableOpen, (unsigned long) p_stFile, &uiIndexOfHashValue ); /* * Initialize file. */ SCR_fn_v_File_InitFileDes(&p_stFile->stFile, _p_szFileName, 0); fn_v_Hash_InitTable(p_stFile->a_stHashSectionsDes); fn_v_DyAr_InitArray(&p_stFile->stSectionsDes); memset(&p_stFile->stParsingInfos, 0, sizeof(SCR_tdst_Pars_Infos)); memset(&p_stFile->stFileValues, 0, sizeof(SCR_tdst_Cxt_Values)); p_stFile->xHashKey = _xHashKey; p_stFile->uiIndexOfHashValue = uiIndexOfHashValue; p_stFile->lSeekWhenClosed = 0; /* Return structure */ return p_stFile; } /* *------------------------------------------------------------------------------------------------- * To delete inside an open file. * _p_stOpenFile : File to delete. *------------------------------------------------------------------------------------------------- */ void fn_v_File_DeleteInsideOpen(SCR_tdst_File_Open *_p_stOpenFile) { SCR_M_DyAr_DeleteAllElements ( SCR_tdst_Anl_SectionDes, &_p_stOpenFile->stSectionsDes, { fn_v_Mem_SetMode(_p_stPointer_->stHeader.ucMemLevel); SCR_M_Cxt_FreeDynBuffers(&_p_stPointer_->stBuffers); } ); fn_v_Hash_CloseTable(_p_stOpenFile->a_stHashSectionsDes); fn_v_Page_DeleteAll(&_p_stOpenFile->stFile); SCR_fn_v_File_CloseFileDes(&_p_stOpenFile->stFile); } /* *------------------------------------------------------------------------------------------------- * To delete an open file. * _p_stFile : File to delete. *------------------------------------------------------------------------------------------------- */ void fn_v_File_DeleteOpen(SCR_tdst_File_Open *_p_stFile) { /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ SCR_M_Dbg_AssertStruct_P(SCR_tdst_File_Open, _p_stFile); /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ /* * Close file ? */ SCR_M_File_Close(&_p_stFile->stFile); /* Close file description */ SCR_fn_v_File_CloseFileDes(&_p_stFile->stFile); /* * Delete element in hash table. */ SCR_M_DyAr_DeleteElement ( SCR_tdst_Hash_Value, _p_stFile->uiIndexOfHashValue, &SCR_g_a_st_File_HashTableOpen[_p_stFile->xHashKey], ; ); /* * Delete structure. */ SCR_M_DyAr_DeleteElement ( SCR_tdst_File_Open, _p_stFile->stHeader.uiIndexInArray, &SCR_g_st_File_ArrayOpen, fn_v_File_DeleteInsideOpen(_p_stFile); ); } /* *------------------------------------------------------------------------------------------------- * To search an open file. * _p_szFileName : Name of file. * _xHashKey : Hash key of file name. * Returns address of allocated structure. *------------------------------------------------------------------------------------------------- */ SCR_tdst_File_Open *fnp_st_File_SearchOpen(char *_p_szFileName, SCR_tdx_Hash_Key _xHashKey) { /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ unsigned int uiPos; SCR_tdst_Hash_Value *p_stValue = NULL; /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ SCR_M_Dbg_Assert_P(_p_szFileName != NULL); /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ /* * Search file. */ SCR_M_DyAr_SearchElement ( SCR_tdst_Hash_Value, uiPos, p_stValue, SCR_g_a_st_File_HashTableOpen[_xHashKey], ( (!strcmpi ( ((SCR_tdst_File_Open *) (p_stValue->ulValue))->stFile.a_szOpenFileName, _p_szFileName ) ) ) ); /* Return address of structure */ if(p_stValue) return (SCR_tdst_File_Open *) p_stValue->ulValue; else return NULL; } /* *------------------------------------------------------------------------------------------------- * To search an open file. Registered path are eventually tested. * _p_szFileName : Name of file. * _xHashKey : Hash key of file name. * Returns address of allocated structure. *------------------------------------------------------------------------------------------------- */ SCR_tdst_File_Open *fnp_st_File_SearchOpenWithPath ( char *_p_szFileName, SCR_tdx_Hash_Key _xHashKey ) { /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ unsigned int uiPos; SCR_tdst_File_Path *p_stPath = NULL; SCR_tdst_File_Open *p_stFile; char a_szCompleteName[_MAX_PATH]; /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ SCR_M_Dbg_Assert_P(_p_szFileName != NULL); /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ /* * We first search with the priority path. */ if(*SCR_g_a_sz_File_PriorityPath != '\0') { strcpy(a_szCompleteName, SCR_g_a_sz_File_PriorityPath); strcat(a_szCompleteName, _p_szFileName); if((p_stFile = fnp_st_File_SearchOpen(a_szCompleteName, _xHashKey)) != NULL) return p_stFile; } /* * We search the file without adding a path. */ strcpy(a_szCompleteName, _p_szFileName); if((p_stFile = fnp_st_File_SearchOpen(a_szCompleteName, _xHashKey)) != NULL) return p_stFile; /* * We we have not found file, we search it in all registered paths. */ uiPos = 0; SCR_M_DyAr_GetNextElement(SCR_tdst_File_Path, uiPos, p_stPath, SCR_g_st_File_ArrayPaths); while(p_stPath) { /* Compute path + parameter file name */ strcpy(a_szCompleteName, p_stPath->a_szPathName); strcat(a_szCompleteName, _p_szFileName); if((p_stFile = fnp_st_File_SearchOpen(a_szCompleteName, _xHashKey)) != NULL) break; uiPos++; SCR_M_DyAr_GetNextElement(SCR_tdst_File_Path, uiPos, p_stPath, SCR_g_st_File_ArrayPaths); } return p_stFile; } /*===============================================================================================*/ /* *------------------------------------------------------------------------------------------------- * To clear a path name. * _p_szPathName : Name of path to clear. * _p_szDest : Destination string. * Returns address of new beginning of path. *------------------------------------------------------------------------------------------------- */ void fn_v_File_ClearPath(char *_p_szPathName, char *_p_szDest) { /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ int iLen; char *p_szTemp; /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ SCR_M_Dbg_Assert_P(_p_szPathName != NULL); SCR_M_Dbg_Assert_P(_p_szDest != NULL); /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ /* * Swap spaces before the path. */ while(isspace(*_p_szPathName)) _p_szPathName++; /* Check for an empty path */ SCR_M_Dbg_Assert_D(*_p_szPathName); strcpy(_p_szDest, _p_szPathName); /* * Swap spaces at the end of the path. */ p_szTemp = _p_szDest + strlen(_p_szDest) - 1; while(isspace(*p_szTemp) && (p_szTemp != _p_szDest)) { *p_szTemp = '\0'; p_szTemp--; } SCR_M_Dbg_Assert_P(!isspace(*_p_szDest)); /* * Force last character to be a '\'. */ iLen = strlen(_p_szDest) - 1; if(_p_szDest[iLen] != '\\') { _p_szDest[iLen + 1] = '\\'; _p_szDest[iLen + 2] = '\0'; } } /* *------------------------------------------------------------------------------------------------- * To add one single path. * _p_szPathName : Name of path to add. * Returns address of allocated structure. *------------------------------------------------------------------------------------------------- */ SCR_tdst_File_Path *fnp_st_File_AddPath(char *_p_szPathName) { /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ unsigned int uiPos; SCR_tdst_File_Path *p_stPath; /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ SCR_M_Dbg_Assert_P(_p_szPathName != NULL); /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ /* * If path already here, exit. */ p_stPath = fnp_st_File_SearchPath(_p_szPathName); if(p_stPath) return p_stPath; /* * Add element. */ SCR_M_DyAr_AddElement(SCR_tdst_File_Path, uiPos, p_stPath, SCR_g_st_File_ArrayPaths); /* * Initialize structure. */ strcpy(p_stPath->a_szPathName, _p_szPathName); /* Return structure */ return p_stPath; } /* *------------------------------------------------------------------------------------------------- * To delete a path. * _p_stPath : Path to delete. *------------------------------------------------------------------------------------------------- */ void fn_v_File_DeletePath(SCR_tdst_File_Path *_p_stPath) { /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ SCR_M_Dbg_AssertStruct_P(SCR_tdst_File_Path, _p_stPath); /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ SCR_M_DyAr_DeleteElement ( SCR_tdst_File_Path, _p_stPath->stHeader.uiIndexInArray, &SCR_g_st_File_ArrayPaths, ; ); } /* *------------------------------------------------------------------------------------------------- * To search a path. * _p_szPathName : Name of path. * Returns address of allocated structure. *------------------------------------------------------------------------------------------------- */ SCR_tdst_File_Path *fnp_st_File_SearchPath(char *_p_szPathName) { /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ unsigned int uiPos; SCR_tdst_File_Path *p_stPointer = NULL; /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ SCR_M_Dbg_Assert_P(_p_szPathName != NULL); /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ SCR_M_DyAr_SearchElement ( SCR_tdst_File_Path, uiPos, p_stPointer, SCR_g_st_File_ArrayPaths, (!strcmpi(p_stPointer->a_szPathName, _p_szPathName)) ); return p_stPointer; } /*===============================================================================================*/ /* *------------------------------------------------------------------------------------------------- * Check the validity of a file name. * _p_szFileName : Name of file to check. *------------------------------------------------------------------------------------------------- */ void fn_vCheckFileNameValidity(char *_p_szFileName) { while(*_p_szFileName) { if ( !isalpha(*_p_szFileName) && !isdigit(*_p_szFileName) && *_p_szFileName != '\\' && *_p_szFileName != '/' && *_p_szFileName != ':' && *_p_szFileName != '_' && *_p_szFileName != '.' && *_p_szFileName != '-' && *_p_szFileName != ' ' ) { sprintf(g_st_Err_GlobalError.a_szBufferTmp1, "File is \"%s\".", _p_szFileName); *g_st_Err_GlobalError.a_szBufferTmp2 = '\0'; SCR_M_Err_RaiseWarning(SCR_EI_Err_BadFileName); return; } _p_szFileName++; } } /* *------------------------------------------------------------------------------------------------- * Try to open a file. * _p_stFile : File to open. * Returns <> 0 if ok, 0 if not open. *------------------------------------------------------------------------------------------------- */ char fn_c_File_TryToOpen(SCR_tdst_File_Open *_p_stFile) { /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ unsigned int uiPos; char cMode; char buffer[10]; SCR_tdst_File_Path *p_stPath = NULL; char szFileName[255]; /*ANNECY CB*/ char cIni = 0; char *psz_Name; /*END ANNECY*/ /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ SCR_M_Dbg_AssertStruct_P(SCR_tdst_File_Open, _p_stFile); /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ /* * Open the file and seek to "lSeekWhenClosed" (0 the first time). * If file can't be open, we try the registered paths. * a_szOpenFileName must contain the sucessful name of opened file. */ uiPos = 0; cMode = 0; while(1) { switch(cMode) { case 0: /* The file with the priority path */ cMode = 1; if(*SCR_g_a_sz_File_PriorityPath != '\0') { strcpy(_p_stFile->stFile.a_szOpenFileName, SCR_g_a_sz_File_PriorityPath); strcat(_p_stFile->stFile.a_szOpenFileName, _p_stFile->stFile.a_szFileName); } else continue; break; case 1: /* The file itself without path */ strcpy(_p_stFile->stFile.a_szOpenFileName, _p_stFile->stFile.a_szFileName); cMode = 2; break; } /* MP: BINARY test */ { /* struct _stat _stat1, _stat2;*/ /* char *pch;*/ strcpy( szFileName, _p_stFile->stFile.a_szOpenFileName ); /* pch=strrchr( szFileName, '.' ); strcat( szFileName, ".xkz" ); if( _stat( _p_stFile->stFile.a_szOpenFileName, &_stat1 )!=-1 ) { if( (_stat( szFileName, &_stat2 )==-1) || ( _stat1.st_mtime>=_stat2.st_mtime )) strcpy( szFileName, _p_stFile->stFile.a_szOpenFileName ); } else if(_stat( szFileName, &_stat2 )==-1) strcpy( szFileName, _p_stFile->stFile.a_szOpenFileName );*/ } /* MP: BINARY end */ /*ANNECY CB*/ psz_Name = szFileName; if(cIni == 1) { char asz_TempName[_MAX_PATH]; strcpy(asz_TempName, gsz_Version); if(strchr(psz_Name, '\\')) strcat(asz_TempName, strchr(psz_Name, '\\')); else { strcat(asz_TempName, "\\"); strcat(asz_TempName, psz_Name); } strcpy(szFileName, asz_TempName); } if(cIni == 2) { char asz_TempName[_MAX_PATH]; strcpy(asz_TempName, gsz_Version1); if(strchr(psz_Name, '\\')) strcat(asz_TempName, strchr(psz_Name, '\\')); else { strcat(asz_TempName, "\\"); strcat(asz_TempName, psz_Name); } strcpy(szFileName, asz_TempName); } /*END ANNECY*/ if((_p_stFile->stFile.p_stHandle->xHandle = SCR_M_p_x_File_OpenRead(/*_p_stFile->stFile.a_szOpenFileName*/ szFileName)) != 0) { /* * Check file name validity if necessary. * Seek to last position if necessary. */ if(_p_stFile->lSeekWhenClosed == 0) { fn_vCheckFileNameValidity(_p_stFile->stFile.a_szFileName); SCR_M_setvbuf ( &_p_stFile->stFile, _p_stFile->a_szBuffer, _IOFBF, SCR_CV_ui_Cfg_SizeBufferFile ); /* MP: BINARY start */ fn_v_Bin_Read( &_p_stFile->stFile, 6, (void *)buffer ); _p_stFile->stFile.bBinaryMode=(strncmp( buffer, "BINARY", 6 )==0); if( !_p_stFile->stFile.bBinaryMode ) { SCR_M_File_Seek(&_p_stFile->stFile, 0, SEEK_SET); if( _p_stFile->stFile.p_stRefs ) { SCR_M_Mem_Free( _p_stFile->stFile.p_stRefs ); _p_stFile->stFile.p_stRefs=NULL; } } else { /* get strings*/ unsigned short usCnt, i; if( (_p_stFile->stParsingInfos.stPosition.lAfterSeekInFile==0)||SCR_g_iParseHeader ) { usCnt=SCR_fn_us_Bin_GetUSHORT( &_p_stFile->stFile ); _p_stFile->stFile.lChangeInPos=8; SCR_M_Mem_Alloc( char *, _p_stFile->stFile.p_stRefs->a_szRefName, usCnt ); _p_stFile->stFile.p_stRefs->sRefCount=usCnt; for( i=0; istFile.p_stRefs->a_szRefName[i]=SCR_fn_sz_Bin_GetSTRING( &_p_stFile->stFile ); _p_stFile->stParsingInfos.stPosition.lAfterSeekInFile=_p_stFile->stFile.lChangeInPos; } SCR_g_iParseHeader=0; } /* MP: BINARY end */ } else SCR_M_File_Seek(&_p_stFile->stFile, _p_stFile->lSeekWhenClosed, SEEK_SET); return 1; } /* * Compute file name with the path. */ if(cMode == 2) { SCR_M_DyAr_GetNextElement(SCR_tdst_File_Path, uiPos, p_stPath, SCR_g_st_File_ArrayPaths); if(p_stPath) { strcpy(_p_stFile->stFile.a_szOpenFileName, p_stPath->a_szPathName); strcat(_p_stFile->stFile.a_szOpenFileName, _p_stFile->stFile.a_szFileName); uiPos++; } else /*ANNECY CB*/ { if((cIni == 0) && (*gsz_Version)) { cIni = 1; cMode = 0; uiPos = 0; } else if((cIni == 1) && (*gsz_Version1)) { cIni = 2; cMode = 0; uiPos = 0; } else /*END ANNECY*/ break; /*ANNECY CB*/ } /*END*/ } } /* * File can't be open. */ return 0; } /* *------------------------------------------------------------------------------------------------- * Try to open a file. Error if not open. * _p_stFile : File to open. *------------------------------------------------------------------------------------------------- */ void fn_v_File_ReallyOpen(SCR_tdst_File_Open *_p_stFile) { do { if(fn_c_File_TryToOpen(_p_stFile)) return; /* Error */ sprintf(g_st_Err_GlobalError.a_szBufferTmp1, "File is \"%s\".", _p_stFile->stFile.a_szFileName); *g_st_Err_GlobalError.a_szBufferTmp2 = '\0'; SCR_M_Err_RaiseError(SCR_EI_Err_OpenFile); } while(g_i_Err_GlobalReturn == IDRETRY); } /* *------------------------------------------------------------------------------------------------- * Force a file to be open. An old one can be closed. * _p_stFile : File to open. *------------------------------------------------------------------------------------------------- */ void fn_v_File_ForceOpenFile(SCR_tdst_File_Open *_p_stFile) { /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ unsigned int uiToClose, uiNumOpen; SCR_tdst_File_Open *p_stCloseFile = NULL; /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ SCR_M_Dbg_AssertStruct_P(SCR_tdst_File_Open, _p_stFile); /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ /* * Already open. We exit. */ if(_p_stFile->stFile.p_stHandle->xHandle != (SCR_FILE) 0) return; /* * Really open the file. */ fn_v_File_ReallyOpen(_p_stFile); /* * Close the oldest file if we are above SCR_C_uiMaxOpenFiles. */ uiToClose = 0; uiNumOpen = 0; SCR_M_DyAr_GetNextElement(SCR_tdst_File_Open, uiToClose, p_stCloseFile, SCR_g_st_File_ArrayOpen); while(p_stCloseFile) { if(p_stCloseFile->stFile.p_stHandle->xHandle) uiNumOpen++; uiToClose++; SCR_M_DyAr_GetNextElement(SCR_tdst_File_Open, uiToClose, p_stCloseFile, SCR_g_st_File_ArrayOpen); } if(uiNumOpen >= SCR_CV_ui_Cfg_MaxOpenFiles) { /* Compute oldest file to close */ uiToClose = 0; SCR_M_DyAr_GetNextElement(SCR_tdst_File_Open, uiToClose, p_stCloseFile, SCR_g_st_File_ArrayOpen); while ( ( (p_stCloseFile) && (p_stCloseFile->stFile.p_stHandle->xHandle == (SCR_FILE) 0) ) || (p_stCloseFile == _p_stFile) ) { uiToClose++; SCR_M_DyAr_GetNextElement(SCR_tdst_File_Open, uiToClose, p_stCloseFile, SCR_g_st_File_ArrayOpen); } SCR_M_Dbg_Assert_P(p_stCloseFile != NULL); /* Remember current position of file before closing */ if((p_stCloseFile->lSeekWhenClosed = SCR_M_ftell(&p_stCloseFile->stFile)) == -1) { sprintf(g_st_Err_GlobalError.a_szBufferTmp1, "File is \"%s\".", _p_stFile->stFile.a_szFileName); *g_st_Err_GlobalError.a_szBufferTmp2 = '\0'; SCR_M_Err_RaiseError(SCR_EI_Err_ReadFile); } /* Close the file */ SCR_M_File_Close(&p_stCloseFile->stFile); } } /* *------------------------------------------------------------------------------------------------- * To open a file. * _p_szFileName : User name of file. * Returns address of allocated structure. *------------------------------------------------------------------------------------------------- */ SCR_tdst_File_Open *fnp_st_File_OpenFile(char *_p_szFileName) { /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ SCR_tdst_File_Open *p_stFile; SCR_tdx_Hash_Key xHashKey; /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ SCR_M_Dbg_Assert_P(_p_szFileName != NULL); /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ /* * Compute hash key for file name. */ xHashKey = SCR_fn_x_Hash_ComputeHashKeyForString(_p_szFileName); /* * We try to search the file in array of already open ones. * If file is not present, we add it. */ p_stFile = fnp_st_File_SearchOpenWithPath(_p_szFileName, xHashKey); if(p_stFile == NULL) { /* We add a new open file */ p_stFile = fnp_st_File_AddOpen(_p_szFileName, xHashKey); SCR_M_Dbg_Assert_P(p_stFile != NULL); } /* * We really open file if its handle is NULL. */ SCR_M_Dbg_Assert_P(p_stFile != NULL); if(p_stFile->stFile.p_stHandle->xHandle == (SCR_FILE) 0) { fn_v_File_ForceOpenFile(p_stFile); } return p_stFile; } /* *------------------------------------------------------------------------------------------------- * To close all the handles of the open files *------------------------------------------------------------------------------------------------- */ void fn_v_File_CloseAllHandles(void) { /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ unsigned int uiToClose; SCR_tdst_File_Open *p_stCloseFile = NULL; /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ uiToClose = 0; SCR_M_DyAr_GetNextElement(SCR_tdst_File_Open, uiToClose, p_stCloseFile, SCR_g_st_File_ArrayOpen); while(p_stCloseFile) { SCR_M_File_Close(&p_stCloseFile->stFile); uiToClose++; SCR_M_DyAr_GetNextElement(SCR_tdst_File_Open, uiToClose, p_stCloseFile, SCR_g_st_File_ArrayOpen); } } /*===============================================================================================*/ /* *------------------------------------------------------------------------------------------------- * To transform a filename depending on previous context. * Name = "*" : We take current file name (previous context). * Name = "*.ext" : We take current file name with extension ".ext" (previous context). * _p_szFileName : File name to transform. *------------------------------------------------------------------------------------------------- */ void fn_v_File_TransformFileName(char *_p_szFileName) { /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ char *p_szTemp; SCR_tdst_Cxt_Description *p_stContext; char a_szTmpBuf[_MAX_PATH]; char a_szPath[_MAX_PATH]; char a_szName[_MAX_FNAME]; char a_szExt[_MAX_EXT]; /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ SCR_M_Dbg_Assert_P(_p_szFileName != NULL); /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ /* * Split file name. */ _splitpath ( _p_szFileName, a_szTmpBuf, a_szPath, a_szName, a_szExt ); /* * File name must began with '*'. */ if(*a_szName != '*') return; /* * Get current context (to get current file). Error if it not exists. */ p_stContext = fnp_st_Cxt_Compute(0); /* * Convertion. */ strcpy(_p_szFileName, p_stContext->p_stOpenFile->stFile.a_szFileName); /* Replace with previous context file name */ if(!strcmpi(a_szName, "*")) return; /* Replace with previous context file name, but with a new extension */ else if(a_szExt[0]) { p_szTemp = strrchr(_p_szFileName, '.'); if(!p_szTemp) p_szTemp = _p_szFileName + strlen(_p_szFileName); strcpy(p_szTemp, a_szExt); return; } /* * Here, we have a name that began with '*' but that is not valid. * We keep it without modify. */ } /* *------------------------------------------------------------------------------------------------- * To split a reference section name. * _p_szInputName : Input name of section. * _p_szFileName : To receive file name alone. * _p_szSectionName : To receive complete section name (atomic file name + section) *------------------------------------------------------------------------------------------------- */ void fn_v_File_ComputeFileSectionName ( char *_p_szInputName, char *_p_szFileName, char *_p_szSectionName ) { /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ char *p_szSecond; /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ SCR_M_Dbg_Assert_P(_p_szInputName != NULL); SCR_M_Dbg_Assert_P(_p_szFileName != NULL); SCR_M_Dbg_Assert_P(_p_szSectionName != NULL); /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ /* * File name */ strcpy(_p_szFileName, _p_szInputName); p_szSecond = strchr(_p_szFileName, SCR_CC_c_Cfg_NameSeparator); if(p_szSecond) *p_szSecond = '\0'; fn_v_File_TransformFileName(_p_szFileName); /* * Section name */ *_p_szSectionName = '\0'; if(p_szSecond) { strcpy(_p_szSectionName, _p_szFileName); strcat(_p_szSectionName, strchr(_p_szInputName, SCR_CC_c_Cfg_NameSeparator)); } } /* ************************************************************************************************** ************************************************************************************************** ************************************************************************************************** ************************************************************************************************** */ /* *------------------------------------------------------------------------------------------------- * To reduce memory size. *------------------------------------------------------------------------------------------------- */ void fn_v_File_ReduceMemory(void) { /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ unsigned int uiPos; SCR_tdst_File_Open *p_stPointer; /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ /* Pack array of open files */ SCR_M_DyAr_PackArray(SCR_tdst_File_Open, SCR_g_st_File_ArrayOpen, ;); /* Pack open files hash table */ SCR_M_Hash_PackArray(SCR_g_a_st_File_HashTableOpen, SCR_tdst_File_Open, SCR_g_st_File_ArrayOpen, xHashKey, uiIndexOfHashValue); /* Pack array of sections descriptions */ for(uiPos = 0; uiPos < SCR_g_st_File_ArrayOpen.uiNumValues; uiPos++) { SCR_M_DyAr_GetElement(SCR_tdst_File_Open, uiPos, p_stPointer, SCR_g_st_File_ArrayOpen); SCR_M_DyAr_SimplePackArray(SCR_tdst_Anl_SectionDescription, p_stPointer->stSectionsDes); SCR_M_Hash_SimplePackArray(p_stPointer->a_stHashSectionsDes); } /* Pack array of paths */ SCR_M_DyAr_PackArray(SCR_tdst_File_Path, SCR_g_st_File_ArrayPaths, ;); } /* *------------------------------------------------------------------------------------------------- *------------------------------------------------------------------------------------------------- */ void fn_v_File_DeleteWithMemLevel(unsigned char _ucMin, unsigned char _ucMax) { SCR_M_DyAr_DeleteElementWithMemLevel ( SCR_tdst_File_Open, SCR_g_st_File_ArrayOpen, { SCR_M_File_Close(&_p_stPointer_->stFile); SCR_fn_v_File_CloseFileDes(&_p_stPointer_->stFile); fn_v_File_DeleteInsideOpen(_p_stPointer_); }, _ucMin, _ucMax ); SCR_M_Hash_DeleteWithMemLevel ( SCR_g_a_st_File_HashTableOpen, _ucMin, _ucMax ); SCR_M_DyAr_DeleteElementWithMemLevel ( SCR_tdst_File_Path, SCR_g_st_File_ArrayPaths, ;, _ucMin, _ucMax ); }