Page principale | Hiérarchie des classes | Liste alphabétique | Liste des classes | Liste des fichiers | Membres de classe | Membres de fichier | Pages associées

Surcouche.c

Aller à la documentation de ce fichier.
00001 /*! \file Surcouche.c
00002     \brief Surcouche pour reseau, multitache, console et temp.
00003 
00004     Redéfinie tout un tas de fonctions système pour les rendre portables unix et windows.
00005 
00006     \author     aerith (contact@aerith.biz - www.aerith.biz)
00007     \version    1.0
00008     \date       02/10/2006
00009 */
00010 
00011 #include    "Surcouche.h"
00012 #include    <string.h>
00013 
00014 //****************************** Multitache ************************************
00015 int     ThreadStart(THREAD *pThread, ROUTINE Process, PARAMETRE Param)
00016 {
00017 #ifdef WIN32
00018     *pThread = (THREAD)CreateThread(NULL, 0, (ROUTINE)Process, (PARAMETRE)Param, 0, NULL);
00019     if(pThread == INVALID_HANDLE_VALUE)
00020         return 1;
00021 #else
00022     pthread_attr_t Type;
00023 
00024     if(pthread_attr_init(&Type))
00025         return(1);
00026 
00027     pthread_attr_setdetachstate(&Type, PTHREAD_CREATE_JOINABLE);
00028 
00029     if(pthread_create((THREAD *)pThread, &Type, (ROUTINE)Process, (PARAMETRE)Param))
00030         return 1;
00031 #endif
00032 
00033     return 0;
00034 }
00035 
00036 void    ThreadStop(THREAD *pThread)
00037 {
00038 #ifdef WIN32
00039     TerminateThread((THREAD)pThread, FALSE);
00040     CloseHandle((THREAD)pThread);
00041 #else
00042     pthread_cancel((THREAD)pThread);
00043 #endif
00044     *pThread = 0;
00045 }
00046 
00047 void    ThreadExit(int ExitCode)
00048 {
00049 #ifdef WIN32
00050     ExitThread(ExitCode);
00051 #else
00052     pthread_exit((void *)&ExitCode);
00053 #endif
00054 }
00055 
00056 void    ThreadWait(THREAD *pThread)
00057 {
00058 #ifdef WIN32
00059     WaitForSingleObject((THREAD)pThread, INFINITE);
00060     CloseHandle((THREAD)pThread);
00061 #else
00062     pthread_join((THREAD)pThread, NULL);
00063 #endif
00064     *pThread = 0;
00065 }
00066 
00067 void    MutexInit(MUTEX *pMutex)
00068 {
00069 #ifdef WIN32
00070     *pMutex = (MUTEX)CreateMutex(NULL, FALSE, NULL);
00071 #else
00072     pthread_mutex_init((MUTEX *)pMutex, NULL);
00073 #endif
00074 }
00075 
00076 void    MutexLock(MUTEX *pMutex)
00077 {
00078     if(!pMutex)
00079         MutexInit(pMutex);
00080 
00081 #ifdef WIN32
00082     WaitForSingleObject((MUTEX)pMutex, INFINITE);
00083 #else
00084     pthread_mutex_lock((MUTEX *)pMutex);
00085 #endif
00086 }
00087 
00088 void    MutexUnLock(MUTEX *pMutex)
00089 {
00090 #ifdef WIN32
00091     ReleaseMutex((MUTEX)pMutex);
00092 #else
00093     pthread_mutex_unlock((MUTEX *)pMutex);
00094 #endif
00095 }
00096 
00097 void    MutexStop(MUTEX *pMutex)
00098 {
00099 #ifdef WIN32
00100     ReleaseMutex((MUTEX)pMutex);
00101     CloseHandle((MUTEX)pMutex);
00102     *pMutex = NULL;
00103 #else
00104     pthread_mutex_destroy((MUTEX *)pMutex);
00105     pMutex = NULL;
00106 #endif
00107 }
00108 
00109 //******************************* Module ***************************************
00110 int     ModuleLoad(MODULE *pModule, char *pName)
00111 {
00112     *pModule = NULL;
00113 #ifdef WIN32
00114     *pModule = (MODULE)LoadLibrary(pName);
00115 #else
00116     *pModule = (MODULE)dlopen(pName, RTLD_NOW);
00117 #endif
00118 
00119     if(*pModule)
00120         return 1;
00121 
00122     return 0;
00123 }
00124 
00125 int     ModuleUnload(MODULE *pModule)
00126 {
00127     int     r = 1;
00128 #ifdef WIN32
00129     if(FreeLibrary((MODULE)*pModule))
00130         r = 0;
00131 #else
00132     if(!dlclose((MODULE)*pModule))
00133         r = 0;
00134 #endif
00135 
00136     *pModule = NULL;
00137 
00138     return r;
00139 }
00140 
00141 int     ModuleImport(MODULE Module, ROUTINE *pFonction, char *pName)
00142 {
00143     *pFonction = NULL;
00144 
00145 #ifdef WIN32
00146     *pFonction = (ROUTINE)GetProcAddress(Module, pName);
00147 #else
00148     *pFonction = (ROUTINE)dlsym(Module, pName);
00149 #endif
00150 
00151     if(*pFonction)
00152         return 1;
00153 
00154     return 0;
00155 }
00156 
00157 
00158 //******************************* Reseau ***************************************
00159 int     SocketLoad(void)
00160 {
00161 #ifdef WIN32
00162     WORD        wVersionRequested = MAKEWORD(1, 1);
00163     WSADATA     wsaData;
00164 
00165     if(WSAStartup(wVersionRequested, &wsaData))
00166     {
00167         WSACleanup();
00168         return 1;
00169     }
00170 #else
00171     signal(SIGPIPE, SIG_IGN);
00172 #endif
00173 
00174     return 0;
00175 }
00176 
00177 void    SocketUnLoad(void)
00178 {
00179 #ifdef WIN32
00180     WSACleanup();
00181 #endif
00182 }
00183 
00184 void    SocketClose(SOCKET Sock)
00185 {
00186 #ifdef WIN32
00187     closesocket(Sock);
00188 #else
00189     close(Sock);
00190 #endif
00191     Sock = 0;
00192 }
00193 
00194 
00195 //********************************** Temp **************************************
00196 void    TempWait(unsigned int Delai)
00197 {
00198 #ifdef WIN32
00199     Sleep(Delai);
00200 #else
00201     usleep(Delai * 1000);
00202 #endif
00203 }
00204 
00205 void    TempPause()
00206 {
00207 #ifdef WIN32
00208     system("PAUSE");
00209 #else
00210 
00211 #endif
00212 }
00213 
00214 //******************************** Console *************************************
00215 void    ConsoleClear()
00216 {
00217 #ifdef WIN32
00218     system("CLS");
00219 #else
00220     printf("\033[2J");
00221 #endif
00222 }
00223 
00224 void    ConsoleSetColor(unsigned int iColor)
00225 {
00226 #ifdef WIN32
00227     char    Buffer[12];
00228 
00229     sprintf(Buffer, "COLOR %X", iColor);
00230     system(Buffer);
00231 #else
00232 
00233 #endif
00234 }
00235 
00236 int     ConsoleCharColor(CONSOLE *pConsole, unsigned int iPosX, unsigned int iPosY, unsigned int iColor, int unsigned iLength)
00237 {
00238 #ifdef WIN32
00239     WORD    *Data;
00240     int     i;
00241     COORD   Pos;
00242 
00243     Pos.X = iPosX;
00244     Pos.Y = iPosY;
00245 
00246     Data = (WORD *)malloc(sizeof(WORD) * iLength);
00247     for(i = 0; i < iLength; Data[i++] = iColor);
00248 
00249     return WriteConsoleOutputAttribute(pConsole, Data, iLength, Pos, (LPDWORD)iLength);
00250 #else
00251     return 0;
00252 #endif
00253 }
00254 
00255 int     ConsoleCharWrite(CONSOLE *pConsole, unsigned int iPosX, unsigned int iPosY, char *pData, unsigned int iLength)
00256 {
00257 #ifdef WIN32
00258     COORD   Pos;
00259 
00260     Pos.X = iPosX;
00261     Pos.Y = iPosY;
00262 
00263     if(!iLength)
00264         iLength = strlen(pData);
00265 
00266     return WriteConsoleOutputCharacter(pConsole, pData, iLength, Pos, (LPDWORD)iLength);
00267 #else
00268     return 0;
00269 #endif
00270 }
00271 
00272 int     ConsoleCharRead(CONSOLE *pConsole, unsigned int iPosX, unsigned int iPosY, char *pData, unsigned int iLength)
00273 {
00274 #ifdef WIN32
00275     COORD   Pos;
00276 
00277     Pos.X = iPosX;
00278     Pos.Y = iPosY;
00279 
00280     if(!iLength)
00281         iLength = strlen(pData);
00282 
00283     return ReadConsoleOutputCharacter(pConsole, pData, iLength, Pos, (LPDWORD)iLength);
00284 #else
00285     return 0;
00286 #endif
00287 }
00288 
00289 int     ConsoleZoneWrite(CONSOLE *pConsole, unsigned int iPosX, unsigned int iPosY, unsigned int iLength, unsigned int iHeight, CHARINFO *pData)
00290 {
00291 #ifdef WIN32
00292     COORD       Pos;
00293     COORD       Size;
00294     SMALL_RECT  Zone;
00295 
00296     Pos.X       = iPosX;
00297     Pos.Y       = iPosY;
00298     Size.X      = iLength - iPosX;
00299     Size.Y      = iHeight - iPosY;
00300     Zone.Left   = Pos.X;
00301     Zone.Top    = Pos.Y;
00302     Zone.Right  = Size.X;
00303     Zone.Bottom = Size.Y;
00304 
00305     return WriteConsoleOutput(pConsole, pData, Size, Pos, &Zone);
00306 #else
00307     return 0;
00308 #endif
00309 }
00310 
00311 int     ConsoleZoneRead(CONSOLE *pConsole, unsigned int iPosX, unsigned int iPosY, unsigned int iLength, unsigned int iHeight, CHARINFO *pData)
00312 {
00313 #ifdef WIN32
00314     COORD       Pos;
00315     COORD       Size;
00316     SMALL_RECT  Zone;
00317 
00318     Pos.X       = iPosX;
00319     Pos.Y       = iPosY;
00320     Size.X      = iLength - iPosX;
00321     Size.Y      = iHeight - iPosY;
00322     Zone.Left   = Pos.X;
00323     Zone.Top    = Pos.Y;
00324     Zone.Right  = Size.X;
00325     Zone.Bottom = Size.Y;
00326 
00327     return ReadConsoleOutput(pConsole, pData, Size, Pos, &Zone);
00328 #else
00329     return 0;
00330 #endif
00331 }
00332 
00333 //******************************** Touche **************************************
00334 int     KeyGet()
00335 {
00336     fflush(stdin);
00337 
00338     return getchar();
00339 }
00340 
00341 int     KeyGet2()
00342 {
00343 #ifdef WIN32
00344     return _getch();
00345 #else
00346     return 0;
00347 #endif
00348 }
00349 
00350 int     KeyWait()
00351 {
00352 #ifdef WIN32
00353     while(!_kbhit());
00354 
00355     return _getch();
00356 #else
00357     return 0;
00358 #endif
00359 }
00360 
00361 int     KeyPressed()
00362 {
00363 #ifdef WIN32
00364     return _kbhit();
00365 #else
00366     return 0;
00367 #endif
00368 }
00369 
00370 
00371 //******************************** Fichier *************************************
00372 int     FileDelete(char *pName)
00373 {
00374 #ifdef WIN32
00375     if(DeleteFile(pName))
00376         return 0;
00377 #else
00378     if(!remove(pName))
00379         return 0;
00380 #endif
00381 
00382     return 1;
00383 }
00384 
00385 int     FileRename(char *pName, char *pNewName)
00386 {
00387 #ifdef WIN32
00388     if(MoveFile(pName, pNewName))
00389         return 0;
00390 #else
00391     if(!rename(pName, pNewName))
00392         return 0;
00393 #endif
00394 
00395     return 1;
00396 }
00397 
00398 //******************************* Reperoire ************************************
00399 DIRECTORY       DirOpen(char *pDirName, char *pFirstData, int *pIsFile)
00400 {
00401     char        *DirName;
00402     DIRECTORY   Dir;
00403 #ifdef WIN32
00404     WIN32_FIND_DATA Find;
00405 #else
00406     struct dirent   *Find;
00407     struct stat     Stat;
00408 #endif
00409 
00410     DirName = (char *)malloc(sizeof(char) * (strlen(pDirName) + 3));
00411     strcpy(DirName, pDirName);
00412     strcat(DirName, "\\");
00413     strcat(DirName, "*");
00414     *pFirstData = '\0';
00415     *pIsFile = 1;
00416 
00417 #ifdef WIN32
00418     if(Dir = FindFirstFile(DirName, &Find))
00419     {
00420         if(Find.cFileName)
00421             strcpy(pFirstData, Find.cFileName);
00422 
00423         if(Find.dwFileAttributes)
00424             if(Find.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
00425                 *pIsFile = 0;
00426     }
00427 #else
00428     Dir = opendir(DirName);
00429     if(Dir)
00430     {
00431         Find = readdir(Dir);
00432         if(Find->d_name)
00433             strcpy(pFirstData, Find->d_name);
00434 
00435         lstat(DirName, &Stat);
00436         if(S_ISDIR(Stat.st_mode))
00437             *pIsFile = 0;
00438     }
00439 #endif
00440 
00441     free(DirName);
00442 
00443     return Dir;
00444 }
00445 
00446 int     DirClose(DIRECTORY *pDir)
00447 {
00448 #ifdef WIN32
00449     if(FindClose(pDir))
00450         return 0;
00451 #else
00452     if(!closedir(*pDir))
00453         return 0;
00454 #endif
00455 
00456     return 1;
00457 }
00458 
00459 int     DirGetNext(DIRECTORY *pDir, char *pNextData, int *pIsFile)
00460 {
00461 #ifdef WIN32
00462     WIN32_FIND_DATA Find;
00463 #else
00464     struct dirent   *Find;
00465     struct stat     Stat;
00466 #endif
00467 
00468     *pNextData = '\0';
00469     *pIsFile = 1;
00470 
00471 #ifdef WIN32
00472     if(FindNextFile(pDir, &Find))
00473     {
00474         if(Find.cFileName)
00475             strcpy(pNextData, Find.cFileName);
00476 
00477         if(Find.dwFileAttributes)
00478             if(Find.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
00479                 *pIsFile = 0;
00480         return 0;
00481     }
00482 #else
00483     if(Find = readdir(*pDir))
00484     {
00485         Find = readdir(*pDir);
00486         if(Find->d_name)
00487             strcpy(pNextData, Find->d_name);
00488 
00489         lstat(pNextData, &Stat);
00490         if(S_ISDIR(Stat.st_mode))
00491             *pIsFile = 0;
00492 
00493         return 0;
00494     }
00495 #endif
00496 
00497     return 1;
00498 }
00499 
00500 int     DirCreate(char *pName)
00501 {
00502 #ifdef WIN32
00503     if(CreateDirectory(pName, NULL))
00504         return 0;
00505 #else
00506     mode_t  modes;
00507 
00508     modes = S_IRWXU | S_IXGRP | S_IXOTH;
00509     if(!mkdir(pName, modes))
00510         return 0;
00511 #endif
00512 
00513     return 1;
00514 }
00515 
00516 int     DirDelete(char *pName)
00517 {
00518 #ifdef WIN32
00519     if(RemoveDirectory(pName))
00520         return 0;
00521 #else
00522     if(!rmdir(pName))
00523         return 0;
00524 #endif
00525 
00526     return 1;
00527 }
00528 

Généré le Thu Oct 12 01:00:43 2006 pour GestionAsso par  doxygen 1.3.9.1