1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
#include <string.h>
#include <sys/times.h>
#include "ssctab.h"
#ifndef NULL
#define NULL ((void POINTER_ATTR*)0)
#endif
long cacheTTL;
ssctab TableCache[20];
void SSCT_removeAll()
{
int i = 0;
for (i = 0; i < 20; i++)
{
TableCache[i].mBuff[0] = '\0';
TableCache[i].mCode[0] = '\0';
TableCache[i].mTime0 = 0;
}
}
//Ajout d'une donnée s'il y a de la place ou deja existante
BOOL SSCT_add( char* pCode, char* newDatum)
{
int i;
int indexT; //index du tableau du code trouvé
int indexV; //index du tableau d'un emplacement vide
char T; //Code trouvé
char V; //Emplacement vide trouvé
time_t curTime;
curTime = time(NULL);
for (i=0; i< 20; i++)
{
if (strcmp( TableCache[i].mCode,pCode) == 0){
T = 1;
indexT = i;
break;
}
if (strlen(TableCache[i].mCode) == 0){
V = 1;
indexV = i;
}
}
if ((T == 1) | ( V == 1))
{
if (T ==1){
strcpy(TableCache[indexT].mBuff, newDatum);
TableCache[indexT].mTime0 = curTime;
}else{
strcpy(TableCache[indexV].mCode, pCode);
strcpy(TableCache[indexV].mBuff, newDatum);
TableCache[indexV].mTime0 = curTime;
}
return TRUE;
}else{
return FALSE;
}
}
//Supprime une donnee si elle est presente
BOOL SSCT_remove(char* pCode)
{
int i = 0;
for (i = 0; i < 20; i++)
{
if (strcmp( TableCache[i].mCode,pCode) == 0)
{
TableCache[i].mBuff[0] = '\0';
TableCache[i].mCode[0] = '\0';
TableCache[i].mTime0 = 0;
return TRUE;
}
}
return FALSE;
}
//récuperation d'une donnée si elle est pas trop ancienne
BOOL SSCT_get(char* pCode,char* newDatum)
{
int i = 0;
time_t curTime;
curTime = time(NULL);
for (i = 0; i < 20; i++)
{
if (strcmp( TableCache[i].mCode,pCode) == 0)
{
if (TableCache[i].mTime0 + cacheTTL < curTime )
{
strcpy(newDatum,TableCache[i].mBuff);
return TRUE;
}else{
return FALSE;
}
}
}
return FALSE;
}
//Suppression des données anciennes
void SSCT_removeOld()
{
int i = 0;
time_t curTime;
curTime = time(NULL);
for (i = 0; i < 20; i++)
{
if (TableCache[i].mTime0 + cacheTTL < curTime )
{
printf("SSCT_removeOld Buff - %s\n",TableCache[i].mBuff);
TableCache[i].mBuff[0] = '\0';
printf("SSCT_removeOld Key - %s\n",TableCache[i].mCode);
TableCache[i].mCode[0] = '\0';
TableCache[i].mTime0 = curTime;
printf("SSCT_removeOld - %d\n",i);
}
}
}
//Constructeur
void SSCT_Construct(long _cacheTTL)
{
cacheTTL = _cacheTTL;
SSCT_removeAll;
} |
Partager