123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- #pragma warn -aus
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/io.h>
- #include <string.h>
- #include <ctype.h>
- #ifdef LINUX
- #include <X11/Xos.h>
- #define _strnicmp strncasecmp
- #endif
- #include "profile.h"
- #define False 0
- #define True 1
- static char *titlePos( char *buf, int *len )
- {
- char *p = buf, *q;
- while( *p && isspace(*p) ) p++;
- if( *p != '[' )
- return 0;
- q = p+1;
- while( *q && *q != ']' ) q++;
- if( *q != ']' )
- return 0;
- if( len )
- *len = (int)(q - p - 1);
- return p+1;
- }
- static int isTitleLine( char *bufPtr )
- {
- return titlePos( bufPtr, 0 ) != 0;
- }
- static int containTitle( char *buf, const char *section )
- {
- int len = 0;
- char *p = titlePos(buf, &len);
- if (p)
- {
- if( strlen( section ) == len && _strnicmp( section, p, len ) == 0 )
- return True;
- }
- return False;
- }
- static int gotoSection( FILE *is, const char *section )
- {
- char line[256] = {0};
- while( fgets(line, 256, is) != NULL)
- if( containTitle( line, section ) )
- return True;
- return False;
- }
- static char *textPos( char *buf, const char *entry )
- {
- if( buf[0] == ';' )
- return 0;
- char *p = strchr( buf, '=' );
- if (!p)
- return 0;
- int len = (int)(p - buf);
- if( strlen(entry) == len && _strnicmp( buf, entry, len ) == 0 )
- return p+1;
- return 0;
- }
- static void stripQuotationChar( char *buf )
- {
- char *p = buf;
- while( *p && isspace(*p) ) p++;
- if( !(*p == '\"' || *p == '\'') )
- return;
- char *q = p+strlen(p);
- while( *q != *p && q > p ) q--;
- if( q == p )
- return;
- int len = (int)(q - p - 1);
- memmove( buf, p+1, len );
- buf[len] = 0;
- }
- static int readEntry( FILE *is, const char *entry, char *buf, int bufSize,
- int strip )
- {
- char lineBuf[256] = {0};
- char *cur = buf;
- *cur = '\0';
- int len = -1;
- while( fgets(lineBuf, 256, is) != NULL)
- {
- if (lineBuf[strlen(lineBuf)-1] == '\n')
- lineBuf[strlen(lineBuf)-1] = 0;
- if( isTitleLine( lineBuf ) )
- break;
- char *p = textPos( lineBuf, entry );
- if( p == 0 )
- continue;
- if( strip )
- stripQuotationChar( p );
- len = strlen(p);
- if( bufSize-1 < len )
- len = bufSize-1;
- strncpy( cur, p, len );
- cur[len] = 0;
- break;
- }
- return len;
- }
- int GetPrivateProfileString( const char *section,
- const char *entry,
- const char *defaultString,
- char *buffer,
- int bufLen,
- const char *fileName )
- {
- FILE *is;
- int len = -1;
- is = fopen(fileName, "rt");
- if( is && gotoSection( is, section ) )
- len = readEntry(is, entry, buffer, bufLen, True);
- if (len < 0)
- {
- strncpy( buffer, defaultString, bufLen-1 );
- buffer[bufLen-1] = 0;
- len = strlen(buffer);
- }
- if (is) fclose(is);
- return len;
- }
- long GetPrivateProfileInt( const char *section,
- const char *entry,
- long defaultInt,
- const char *fileName )
- {
- char buf[256];
- char iBuf[34];
- sprintf(iBuf, "%d", defaultInt);
- GetPrivateProfileString( section, entry, iBuf, buf, 256, fileName );
- return atol( buf );
- }
- static void writeEntry( FILE *os, const char *entry, const char *string )
- {
- fprintf(os, "%s=%s\n", entry, string);
- }
- int WritePrivateProfileString( const char *section,
- const char *entry,
- const char *string,
- const char *fileName )
- {
- char path [8192] = {0};
- char drive[256] = {0};
- char dir [256] = {0};
- char file [256] = {0};
- char ext [256] = {0};
- char buf [256] = {0};
- int titleFound;
-
- _splitpath( path, drive, dir, file, ext );
- _makepath( path, drive, dir, mkstemp("iniXXXXXX"), "" );
- FILE *is = fopen(fileName, "rt");
- FILE *os = fopen(path, "wt");
- if(!is || !os || entry == 0)
- {
- if (is) fclose(is);
- if (os) fclose(os);
- return 0;
- }
- titleFound = False;
- if (is)
- {
- while (fgets(buf, 256, is) != NULL)
- {
- fputs(buf, os);
- if( containTitle(buf, section) )
- {
- titleFound = True;
- break;
- }
- }
- }
- if (!titleFound)
- {
- fprintf(os, "[%s]\n", section);
- writeEntry( os, entry, string );
- }
- else
- {
- while (fgets(buf, 256, is) != NULL)
- {
- if (isTitleLine(buf))
- break;
- if (textPos(buf, entry))
- {
- break;
- }
- fputs(buf, os);
- }
- writeEntry(os, entry, string);
- if (is)
- {
- while(fgets(buf, 256, is) != NULL)
- fputs(buf, os);
- }
- }
- if (is) fclose(is);
- if (os) fclose(os);
- unlink(fileName);
- rename(path, fileName);
- return strlen(string);
- }
- #pragma warn .aus
|