static void writesection(TCHAR *LocalBuffer, const TCHAR *Section, INI_FILETYPE *fp) { TCHAR *p; if (Section != NULL && _tcslen(Section) > 0) { LocalBuffer[0] = '['; save_strncpy(LocalBuffer + 1, Section, INI_BUFFERSIZE - 4, QUOTE_NONE); /* -1 for '[', -1 for ']', -2 for '\r\n' */ p = _tcsrchr(LocalBuffer, '\0'); assert(p != NULL); *p++ = ']'; _tcscpy(p, INI_LINETERM); /* copy line terminator (typically "\n") */ (void) ini_write(LocalBuffer, fp); } /* if */ }
/** ini_gets() * \param Section the name of the section to search for * \param Key the name of the entry to find the value of * \param DefValue default string in the event of a failed read * \param Buffer a pointer to the buffer to copy into * \param BufferSize the maximum number of characters to copy * \param Filename the name and full path of the .ini file to read from * * \return the number of characters copied into the supplied buffer */ int ini_gets(const TCHAR *Section, const TCHAR *Key, const TCHAR *DefValue, TCHAR *Buffer, int BufferSize, const TCHAR *Filename) { INI_FILETYPE fp; int ok = 0; if (Buffer == NULL || BufferSize <= 0 || Key == NULL) return 0; if (ini_openread(Filename, &fp)) { ok = getkeystring(&fp, Section, Key, -1, -1, Buffer, BufferSize); (void)ini_close(&fp); } /* if */ if (!ok) save_strncpy(Buffer, DefValue, BufferSize, QUOTE_NONE); return _tcslen(Buffer); }
/** Profile_GetString() * \param Section the name of the section to search for * \param Key the name of the entry to find the value of * \param DefValue default string in the event of a failed read * \param Buffer a pointer to the buffer to copy into * \param BufferSize the maximum number of characters to copy * * \return the number of characters copied into the supplied buffer */ int Profile_GetString(const char *Section, const char *Key, const char *DefValue, char *Buffer, int BufferSize) { int ok = 0; if (Buffer == NULL || BufferSize <= 0 || Key == NULL) return 0; if (g_BufferAddr != NULL) { ok = getkeystring(Section, Key, -1, -1, Buffer, BufferSize); Profile_seek(0); } /* if */ if (!ok) save_strncpy(Buffer, DefValue, BufferSize, QUOTE_NONE); return _tcslen(Buffer); }
int ini_gets_OpenedFile( const TCHAR *Section, const TCHAR *Key, const TCHAR *DefValue, TCHAR *Buffer, int BufferSize, INI_FILETYPE* pFile ) { int ok = 0; int pos = 0; if (Buffer == NULL || BufferSize <= 0 || Key == NULL) return 0; // (void) ini_seek(pFile, &pos); ok = getkeystring(pFile, Section, Key, -1, -1, Buffer, BufferSize); if (!ok) save_strncpy(Buffer, DefValue, BufferSize, QUOTE_NONE); return _tcslen(Buffer); }
/** ini_gets() * \param Section the name of the section to search for * \param Key the name of the entry to find the value of * \param DefValue default string in the event of a failed read * \param Buffer a pointer to the buffer to copy into * \param BufferSize the maximum number of characters to copy * \param Filename the name and full path of the .ini file to read from * * \return the number of characters copied into the supplied buffer */ int ini_gets(const TCHAR *Section, const TCHAR *Key, const TCHAR *DefValue, TCHAR *Buffer, int BufferSize, const TCHAR *Filename) { INI_FILETYPE fp; //FILE* fp = fopen("./config.txt","rb"); int ok = 0; /*int i; printf("file pointer, %p\n", fp); u_char* data; data = (u_char*) &fp; printf("fopen:::"); for (i=0; i<=(20); i++) //-1 to account for not starting at 1 { printf("%s:", data[i]); } printf("\n"); */ if (Buffer == NULL || BufferSize <= 0 || Key == NULL) return 0; if (ini_openread(Filename, &fp)) { ok = getkeystring(&fp, Section, Key, -1, -1, Buffer, BufferSize); (void)ini_close(&fp); } /* if */ if (!ok) save_strncpy(Buffer, DefValue, BufferSize, QUOTE_NONE); return _tcslen(Buffer); }
static int getkeystring(INI_FILETYPE *fp, const TCHAR *Section, const TCHAR *Key, int idxSection, int idxKey, TCHAR *Buffer, int BufferSize) { TCHAR *sp, *ep; int len, idx; enum quote_option quotes; TCHAR LocalBuffer[INI_BUFFERSIZE]; assert(fp != NULL); /* Move through file 1 line at a time until a section is matched or EOF. If * parameter Section is NULL, only look at keys above the first section. If * idxSection is postive, copy the relevant section name. */ len = (Section != NULL) ? _tcslen(Section) : 0; if (len > 0 || idxSection >= 0) { idx = -1; do { if (!ini_read(LocalBuffer, INI_BUFFERSIZE, fp)) return 0; sp = skipleading(LocalBuffer); ep = _tcschr(sp, ']'); } while (*sp != '[' || ep == NULL || (((int)(ep-sp-1) != len || _tcsnicmp(sp+1,Section,len) != 0) && ++idx != idxSection)); if (idxSection >= 0) { if (idx == idxSection) { assert(ep != NULL); assert(*ep == ']'); *ep = '\0'; save_strncpy(Buffer, sp + 1, BufferSize, QUOTE_NONE); return 1; } /* if */ return 0; /* no more section found */ } /* if */ } /* if */ /* Now that the section has been found, find the entry. * Stop searching upon leaving the section's area. */ assert(Key != NULL || idxKey >= 0); len = (Key != NULL) ? (int)_tcslen(Key) : 0; idx = -1; do { if (!ini_read(LocalBuffer,INI_BUFFERSIZE,fp) || *(sp = skipleading(LocalBuffer)) == '[') return 0; sp = skipleading(LocalBuffer); ep = _tcschr(sp, '='); /* Parse out the equal sign */ if (ep == NULL) ep = _tcschr(sp, ':'); } while (*sp == ';' || *sp == '#' || ep == NULL || (((int)(skiptrailing(ep,sp)-sp) != len || _tcsnicmp(sp,Key,len) != 0) && ++idx != idxKey)); if (idxKey >= 0) { if (idx == idxKey) { assert(ep != NULL); assert(*ep == '=' || *ep == ':'); *ep = '\0'; striptrailing(sp); save_strncpy(Buffer, sp, BufferSize, QUOTE_NONE); return 1; } /* if */ return 0; /* no more key found (in this section) */ } /* if */ /* Copy up to BufferSize chars to buffer */ assert(ep != NULL); assert(*ep == '=' || *ep == ':'); sp = skipleading(ep + 1); sp = cleanstring(sp, "es); /* Remove a trailing comment */ save_strncpy(Buffer, sp, BufferSize, quotes); return 1; }
static int getkeystring(INI_FILETYPE *fp, const TCHAR *Section, const TCHAR *Key, int idxSection, int idxKey, TCHAR *Buffer, int BufferSize) { TCHAR *sp, *ep; int len, idx, isstring; enum quote_option quotes; TCHAR LocalBuffer[INI_BUFFERSIZE]; assert(fp != NULL); /* Move through file 1 line at a time until a section is matched or EOF. If * parameter Section is NULL, only look at keys above the first section. If * idxSection is postive, copy the relevant section name. */ len = (Section != NULL) ? _tcslen(Section) : 0; if (len > 0 || idxSection >= 0) { idx = -1; do { if (!ini_read(LocalBuffer, INI_BUFFERSIZE, fp)) return 0; sp = skipleading(LocalBuffer); ep = _tcschr(sp, ']'); } while (*sp != '[' || ep == NULL || (((int)(ep-sp-1) != len || _tcsnicmp(sp+1,Section,len) != 0) && ++idx != idxSection)); if (idxSection >= 0) { if (idx == idxSection) { assert(ep != NULL); assert(*ep == ']'); *ep = '\0'; save_strncpy(Buffer, sp + 1, BufferSize, QUOTE_NONE); return 1; } /* if */ return 0; /* no more section found */ } /* if */ } /* if */ /* Now that the section has been found, find the entry. * Stop searching upon leaving the section's area. */ assert(Key != NULL || idxKey >= 0); len = (Key != NULL) ? (int)_tcslen(Key) : 0; idx = -1; do { if (!ini_read(LocalBuffer,INI_BUFFERSIZE,fp) || *(sp = skipleading(LocalBuffer)) == '[') return 0; sp = skipleading(LocalBuffer); ep = _tcschr(sp, '='); /* Parse out the equal sign */ if (ep == NULL) ep = _tcschr(sp, ':'); } while (*sp == ';' || *sp == '#' || ep == NULL || (((int)(skiptrailing(ep,sp)-sp) != len || _tcsnicmp(sp,Key,len) != 0) && ++idx != idxKey)); if (idxKey >= 0) { if (idx == idxKey) { assert(ep != NULL); assert(*ep == '=' || *ep == ':'); *ep = '\0'; striptrailing(sp); save_strncpy(Buffer, sp, BufferSize, QUOTE_NONE); return 1; } /* if */ return 0; /* no more key found (in this section) */ } /* if */ /* Copy up to BufferSize chars to buffer */ assert(ep != NULL); assert(*ep == '=' || *ep == ':'); sp = skipleading(ep + 1); /* Remove a trailing comment */ isstring = 0; for (ep = sp; *ep != '\0' && ((*ep != ';' && *ep != '#') || isstring); ep++) { if (*ep == '"') { if (*(ep + 1) == '"') ep++; /* skip "" (both quotes) */ else isstring = !isstring; /* single quote, toggle isstring */ } else if (*ep == '\\' && *(ep + 1) == '"') { ep++; /* skip \" (both quotes */ } /* if */ } /* for */ assert(ep != NULL && (*ep == '\0' || *ep == ';' || *ep == '#')); *ep = '\0'; /* terminate at a comment */ striptrailing(sp); /* Remove double quotes surrounding a value */ quotes = QUOTE_NONE; if (*sp == '"' && (ep = _tcschr(sp, '\0')) != NULL && *(ep - 1) == '"') { sp++; *--ep = '\0'; quotes = QUOTE_DEQUOTE; /* this is a string, so remove escaped characters */ } /* if */ save_strncpy(Buffer, sp, BufferSize, quotes); return 1; }