- Adapted indenation to new agreed upon system
- Added support for second generation scriptcontext based counter
This commit is contained in:
348
ifile.c
348
ifile.c
@ -47,204 +47,196 @@
|
||||
|
||||
/*====================== Locals ============================================*/
|
||||
|
||||
static IPair *CreateNewEntry(char *name, char *val, IPair *pN)
|
||||
{
|
||||
IPair *pRes = NULL;
|
||||
|
||||
pRes = (IPair *)malloc(sizeof(IPair));
|
||||
if(!pRes)
|
||||
return NULL;
|
||||
memset(pRes,0,sizeof(IPair));
|
||||
if(name)
|
||||
pRes->name = strdup(name);
|
||||
if(val)
|
||||
pRes->value = strdup(val);
|
||||
pRes->pNext = pN;
|
||||
return pRes;
|
||||
}
|
||||
static IPair *CreateNewEntry(char *name, char *val, IPair * pN)
|
||||
{
|
||||
IPair *pRes = NULL;
|
||||
|
||||
pRes = (IPair *) malloc(sizeof(IPair));
|
||||
if (!pRes)
|
||||
return NULL;
|
||||
memset(pRes, 0, sizeof(IPair));
|
||||
if (name)
|
||||
pRes->name = strdup(name);
|
||||
if (val)
|
||||
pRes->value = strdup(val);
|
||||
pRes->pNext = pN;
|
||||
return pRes;
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
static void RemoveWhiteSpace(char *pText)
|
||||
{
|
||||
int i, ii, i3;
|
||||
char *pPtr;
|
||||
|
||||
assert(pText);
|
||||
|
||||
/* find start */
|
||||
i = 0;
|
||||
while(isspace(pText[i]))
|
||||
{
|
||||
i++;
|
||||
}
|
||||
|
||||
/* find end */
|
||||
ii = strlen(pText);
|
||||
static void RemoveWhiteSpace(char *pText)
|
||||
{
|
||||
int i, ii, i3;
|
||||
char *pPtr;
|
||||
|
||||
assert(pText);
|
||||
|
||||
/* find start */
|
||||
i = 0;
|
||||
while (isspace(pText[i])) {
|
||||
i++;
|
||||
}
|
||||
|
||||
/* find end */
|
||||
ii = strlen(pText);
|
||||
ii--;
|
||||
while ((isspace(pText[ii])) || (pText[ii] == '\n')) {
|
||||
ii--;
|
||||
while( (isspace(pText[ii])) || (pText[ii] == '\n') )
|
||||
{
|
||||
ii--;
|
||||
}
|
||||
|
||||
/* copy it */
|
||||
pPtr = pText;
|
||||
for(i3 = i; i3 < (ii+1); i3++)
|
||||
{
|
||||
*pPtr = pText[i3];
|
||||
pPtr++;
|
||||
}
|
||||
*pPtr = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
/* copy it */
|
||||
pPtr = pText;
|
||||
for (i3 = i; i3 < (ii + 1); i3++) {
|
||||
*pPtr = pText[i3];
|
||||
pPtr++;
|
||||
}
|
||||
*pPtr = '\0';
|
||||
}
|
||||
|
||||
/*===========================================================================*/
|
||||
IPair *IFReadConfigFile(FILE *fd)
|
||||
{
|
||||
IPair *pList = NULL;
|
||||
char pBueffel[256];
|
||||
char pName[132];
|
||||
char pValue[132];
|
||||
char *pPos;
|
||||
int iLen;
|
||||
|
||||
assert(fd);
|
||||
|
||||
while(!feof(fd))
|
||||
{
|
||||
fgets(pBueffel,255,fd);
|
||||
if(feof(fd))
|
||||
continue;
|
||||
IPair *IFReadConfigFile(FILE * fd)
|
||||
{
|
||||
IPair *pList = NULL;
|
||||
char pBueffel[256];
|
||||
char pName[132];
|
||||
char pValue[132];
|
||||
char *pPos;
|
||||
int iLen;
|
||||
|
||||
pPos = strchr(pBueffel,'=');
|
||||
if(!pPos)
|
||||
continue;
|
||||
if(pBueffel[0] == '#')
|
||||
continue;
|
||||
|
||||
iLen = pPos - pBueffel;
|
||||
strncpy(pName,pBueffel,iLen);
|
||||
pName[iLen] = '\0';
|
||||
strcpy(pValue,(pPos+1));
|
||||
RemoveWhiteSpace(pName);
|
||||
RemoveWhiteSpace(pValue);
|
||||
pList = CreateNewEntry(pName,pValue,pList);
|
||||
if(!pList)
|
||||
return NULL;
|
||||
}
|
||||
return pList;
|
||||
}
|
||||
/*--------------------------------------------------------------------------*/
|
||||
char *IFindOption(IPair *pList, char *name)
|
||||
{
|
||||
IPair *pCurrent;
|
||||
|
||||
if(!pList)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
pCurrent = pList;
|
||||
while(pCurrent)
|
||||
{
|
||||
if(strcmp(name,pCurrent->name) == 0)
|
||||
return pCurrent->value;
|
||||
pCurrent = pCurrent->pNext;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
/*--------------------------------------------------------------------------*/
|
||||
IPair *IFAddOption(IPair *pList,char *name, char *value)
|
||||
{
|
||||
return CreateNewEntry(name,value,pList);
|
||||
assert(fd);
|
||||
|
||||
while (!feof(fd)) {
|
||||
fgets(pBueffel, 255, fd);
|
||||
if (feof(fd))
|
||||
continue;
|
||||
|
||||
pPos = strchr(pBueffel, '=');
|
||||
if (!pPos)
|
||||
continue;
|
||||
if (pBueffel[0] == '#')
|
||||
continue;
|
||||
|
||||
iLen = pPos - pBueffel;
|
||||
strncpy(pName, pBueffel, iLen);
|
||||
pName[iLen] = '\0';
|
||||
strcpy(pValue, (pPos + 1));
|
||||
RemoveWhiteSpace(pName);
|
||||
RemoveWhiteSpace(pValue);
|
||||
pList = CreateNewEntry(pName, pValue, pList);
|
||||
if (!pList)
|
||||
return NULL;
|
||||
}
|
||||
return pList;
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
IPair *IFSetOption(IPair *pList,char *name, char *value)
|
||||
{
|
||||
IPair *pCurrent;
|
||||
if(NULL!=pList)
|
||||
{
|
||||
pCurrent = pList;
|
||||
while((NULL!=pCurrent) && (0!=strcmp(name,pCurrent->name)))
|
||||
{
|
||||
pCurrent = pCurrent->pNext;
|
||||
}
|
||||
if(NULL!=pCurrent)
|
||||
{ /* replace value */
|
||||
free(pCurrent->value);
|
||||
pCurrent->value = strdup(value);
|
||||
return pCurrent;
|
||||
}
|
||||
char *IFindOption(IPair * pList, char *name)
|
||||
{
|
||||
IPair *pCurrent;
|
||||
|
||||
if (!pList) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
pCurrent = pList;
|
||||
while (pCurrent) {
|
||||
if (strcmp(name, pCurrent->name) == 0)
|
||||
return pCurrent->value;
|
||||
pCurrent = pCurrent->pNext;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
IPair *IFAddOption(IPair * pList, char *name, char *value)
|
||||
{
|
||||
return CreateNewEntry(name, value, pList);
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
IPair *IFSetOption(IPair * pList, char *name, char *value)
|
||||
{
|
||||
IPair *pCurrent;
|
||||
if (NULL != pList) {
|
||||
pCurrent = pList;
|
||||
while ((NULL != pCurrent) && (0 != strcmp(name, pCurrent->name))) {
|
||||
pCurrent = pCurrent->pNext;
|
||||
}
|
||||
if (NULL != pCurrent) { /* replace value */
|
||||
free(pCurrent->value);
|
||||
pCurrent->value = strdup(value);
|
||||
return pCurrent;
|
||||
}
|
||||
return CreateNewEntry(name,value,pList);
|
||||
}
|
||||
return CreateNewEntry(name, value, pList);
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*/
|
||||
|
||||
int IFSaveOptions(IPair *pList,FILE *fd)
|
||||
{
|
||||
IPair *pCurrent;
|
||||
|
||||
assert(fd);
|
||||
assert(pList);
|
||||
|
||||
pCurrent = pList;
|
||||
while(pCurrent)
|
||||
{
|
||||
fprintf(fd,"%s = %s \n", pCurrent->name, pCurrent->value);
|
||||
pCurrent = pCurrent->pNext;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
int IFSaveOptions(IPair * pList, FILE * fd)
|
||||
{
|
||||
IPair *pCurrent;
|
||||
|
||||
assert(fd);
|
||||
assert(pList);
|
||||
|
||||
pCurrent = pList;
|
||||
while (pCurrent) {
|
||||
fprintf(fd, "%s = %s \n", pCurrent->name, pCurrent->value);
|
||||
pCurrent = pCurrent->pNext;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
void IFDeleteOptions(IPair *pList)
|
||||
{
|
||||
IPair *pCurrent, *pTemp;
|
||||
|
||||
pCurrent = pList;
|
||||
while(pCurrent)
|
||||
{
|
||||
pTemp = pCurrent->pNext;
|
||||
if(pCurrent->name)
|
||||
{
|
||||
free(pCurrent->name);
|
||||
}
|
||||
if(pCurrent->value)
|
||||
{
|
||||
free(pCurrent->value);
|
||||
}
|
||||
free(pCurrent);
|
||||
pCurrent = pTemp;
|
||||
void IFDeleteOptions(IPair * pList)
|
||||
{
|
||||
IPair *pCurrent, *pTemp;
|
||||
|
||||
pCurrent = pList;
|
||||
while (pCurrent) {
|
||||
pTemp = pCurrent->pNext;
|
||||
if (pCurrent->name) {
|
||||
free(pCurrent->name);
|
||||
}
|
||||
if (pCurrent->value) {
|
||||
free(pCurrent->value);
|
||||
}
|
||||
free(pCurrent);
|
||||
pCurrent = pTemp;
|
||||
}
|
||||
}
|
||||
|
||||
/*===========================================================================
|
||||
Testcode: Define MAIN to activate it. Needs a file test.txt
|
||||
as input
|
||||
*/
|
||||
|
||||
#ifdef MAIN
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE *fd;
|
||||
IPair *pList = NULL;
|
||||
char *pPos = NULL;
|
||||
|
||||
fd = fopen("test.txt","r");
|
||||
if(!fd)
|
||||
exit(2);
|
||||
|
||||
|
||||
pList = IFReadConfigFile(fd);
|
||||
if(!pList)
|
||||
exit(2);
|
||||
fclose(fd);
|
||||
|
||||
pPos = IFindOption(pList,"Gabi");
|
||||
if(pPos)
|
||||
puts(pPos);
|
||||
|
||||
fd = fopen("lala.txt","w");
|
||||
IFSaveOptions(pList,fd);
|
||||
IFDeleteOptions(pList);
|
||||
|
||||
}
|
||||
#endif
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE *fd;
|
||||
IPair *pList = NULL;
|
||||
char *pPos = NULL;
|
||||
|
||||
fd = fopen("test.txt", "r");
|
||||
if (!fd)
|
||||
exit(2);
|
||||
|
||||
|
||||
pList = IFReadConfigFile(fd);
|
||||
if (!pList)
|
||||
exit(2);
|
||||
fclose(fd);
|
||||
|
||||
pPos = IFindOption(pList, "Gabi");
|
||||
if (pPos)
|
||||
puts(pPos);
|
||||
|
||||
fd = fopen("lala.txt", "w");
|
||||
IFSaveOptions(pList, fd);
|
||||
IFDeleteOptions(pList);
|
||||
|
||||
}
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user