Skip to content

Commit 7790836

Browse files
committed
Removed "const" on user data in callbacks (issue #6).
Fixed assert in save_strncpy() (issue #7); also renamed the function to ini_strncpy().
1 parent d4d2a05 commit 7790836

File tree

3 files changed

+35
-34
lines changed

3 files changed

+35
-34
lines changed

dev/minIni.c

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* These routines are in part based on the article "Multiplatform .INI Files"
44
* by Joseph J. Graf in the March 1994 issue of Dr. Dobb's Journal.
55
*
6-
* Copyright (c) CompuPhase, 2008-2015
6+
* Copyright (c) CompuPhase, 2008-2017
77
*
88
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
99
* use this file except in compliance with the License. You may obtain a copy
@@ -103,9 +103,8 @@ enum quote_option {
103103
#if defined PORTABLE_STRNICMP
104104
int strnicmp(const TCHAR *s1, const TCHAR *s2, size_t n)
105105
{
106-
register int c1, c2;
107-
108106
while (n-- != 0 && (*s1 || *s2)) {
107+
register int c1, c2;
109108
c1 = *s1++;
110109
if ('a' <= c1 && c1 <= 'z')
111110
c1 += ('A' - 'a');
@@ -144,12 +143,13 @@ static TCHAR *striptrailing(TCHAR *str)
144143
return str;
145144
}
146145

147-
static TCHAR *save_strncpy(TCHAR *dest, const TCHAR *source, size_t maxlen, enum quote_option option)
146+
static TCHAR *ini_strncpy(TCHAR *dest, const TCHAR *source, size_t maxlen, enum quote_option option)
148147
{
149148
size_t d, s;
150149

151150
assert(maxlen>0);
152-
assert(dest <= source || dest >= source + maxlen);
151+
assert(source != NULL && dest != NULL);
152+
assert((dest < source || (dest == source && option != QUOTE_ENQUOTE)) || dest > source + strlen(source));
153153
if (option == QUOTE_ENQUOTE && maxlen < 3)
154154
option = QUOTE_NONE; /* cannot store two quotes and a terminating zero in less than 3 characters */
155155

@@ -234,23 +234,25 @@ static int getkeystring(INI_FILETYPE *fp, const TCHAR *Section, const TCHAR *Key
234234
assert(fp != NULL);
235235
/* Move through file 1 line at a time until a section is matched or EOF. If
236236
* parameter Section is NULL, only look at keys above the first section. If
237-
* idxSection is positive, copy the relevant section name.
237+
* idxSection is postive, copy the relevant section name.
238238
*/
239239
len = (Section != NULL) ? (int)_tcslen(Section) : 0;
240240
if (len > 0 || idxSection >= 0) {
241+
assert(idxSection >= 0 || Section != NULL);
241242
idx = -1;
242243
do {
243244
if (!ini_read(LocalBuffer, INI_BUFFERSIZE, fp))
244245
return 0;
245246
sp = skipleading(LocalBuffer);
246-
ep = _tcschr(sp, ']');
247-
} while (*sp != '[' || ep == NULL || (((int)(ep-sp-1) != len || _tcsnicmp(sp+1,Section,len) != 0) && ++idx != idxSection));
247+
ep = _tcsrchr(sp, ']');
248+
} while (*sp != '[' || ep == NULL ||
249+
(((int)(ep-sp-1) != len || Section == NULL || _tcsnicmp(sp+1,Section,len) != 0) && ++idx != idxSection));
248250
if (idxSection >= 0) {
249251
if (idx == idxSection) {
250252
assert(ep != NULL);
251253
assert(*ep == ']');
252254
*ep = '\0';
253-
save_strncpy(Buffer, sp + 1, BufferSize, QUOTE_NONE);
255+
ini_strncpy(Buffer, sp + 1, BufferSize, QUOTE_NONE);
254256
return 1;
255257
} /* if */
256258
return 0; /* no more section found */
@@ -280,7 +282,7 @@ static int getkeystring(INI_FILETYPE *fp, const TCHAR *Section, const TCHAR *Key
280282
assert(*ep == '=' || *ep == ':');
281283
*ep = '\0';
282284
striptrailing(sp);
283-
save_strncpy(Buffer, sp, BufferSize, QUOTE_NONE);
285+
ini_strncpy(Buffer, sp, BufferSize, QUOTE_NONE);
284286
return 1;
285287
} /* if */
286288
return 0; /* no more key found (in this section) */
@@ -291,7 +293,7 @@ static int getkeystring(INI_FILETYPE *fp, const TCHAR *Section, const TCHAR *Key
291293
assert(*ep == '=' || *ep == ':');
292294
sp = skipleading(ep + 1);
293295
sp = cleanstring(sp, &quotes); /* Remove a trailing comment */
294-
save_strncpy(Buffer, sp, BufferSize, quotes);
296+
ini_strncpy(Buffer, sp, BufferSize, quotes);
295297
return 1;
296298
}
297299

@@ -318,7 +320,7 @@ int ini_gets(const TCHAR *Section, const TCHAR *Key, const TCHAR *DefValue,
318320
(void)ini_close(&fp);
319321
} /* if */
320322
if (!ok)
321-
save_strncpy(Buffer, (DefValue != NULL) ? DefValue : __T(""), BufferSize, QUOTE_NONE);
323+
ini_strncpy(Buffer, (DefValue != NULL) ? DefValue : __T(""), BufferSize, QUOTE_NONE);
322324
return (int)_tcslen(Buffer);
323325
}
324326

@@ -447,7 +449,7 @@ int ini_getkey(const TCHAR *Section, int idx, TCHAR *Buffer, int BufferSize, co
447449
/** ini_browse()
448450
* \param Callback a pointer to a function that will be called for every
449451
* setting in the INI file.
450-
* \param UserData arbitrary data, which the function passes on the
452+
* \param UserData arbitrary data, which the function passes on the the
451453
* \c Callback function
452454
* \param Filename the name and full path of the .ini file to read from
453455
*
@@ -458,7 +460,7 @@ int ini_getkey(const TCHAR *Section, int idx, TCHAR *Buffer, int BufferSize, co
458460
* callback stops the browsing, this function will return 1
459461
* (for success).
460462
*/
461-
int ini_browse(INI_CALLBACK Callback, const void *UserData, const TCHAR *Filename)
463+
int ini_browse(INI_CALLBACK Callback, void *UserData, const TCHAR *Filename)
462464
{
463465
TCHAR LocalBuffer[INI_BUFFERSIZE];
464466
int lenSec, lenKey;
@@ -481,10 +483,10 @@ int ini_browse(INI_CALLBACK Callback, const void *UserData, const TCHAR *Filena
481483
if (*sp == '\0' || *sp == ';' || *sp == '#')
482484
continue;
483485
/* see whether we reached a new section */
484-
ep = _tcschr(sp, ']');
486+
ep = _tcsrchr(sp, ']');
485487
if (*sp == '[' && ep != NULL) {
486488
*ep = '\0';
487-
save_strncpy(LocalBuffer, sp + 1, INI_BUFFERSIZE, QUOTE_NONE);
489+
ini_strncpy(LocalBuffer, sp + 1, INI_BUFFERSIZE, QUOTE_NONE);
488490
lenSec = (int)_tcslen(LocalBuffer) + 1;
489491
continue;
490492
} /* if */
@@ -496,12 +498,12 @@ int ini_browse(INI_CALLBACK Callback, const void *UserData, const TCHAR *Filena
496498
continue; /* invalid line, ignore */
497499
*ep++ = '\0'; /* split the key from the value */
498500
striptrailing(sp);
499-
save_strncpy(LocalBuffer + lenSec, sp, INI_BUFFERSIZE - lenSec, QUOTE_NONE);
501+
ini_strncpy(LocalBuffer + lenSec, sp, INI_BUFFERSIZE - lenSec, QUOTE_NONE);
500502
lenKey = (int)_tcslen(LocalBuffer + lenSec) + 1;
501503
/* clean up the value */
502504
sp = skipleading(ep);
503505
sp = cleanstring(sp, &quotes); /* Remove a trailing comment */
504-
save_strncpy(LocalBuffer + lenSec + lenKey, sp, INI_BUFFERSIZE - lenSec - lenKey, quotes);
506+
ini_strncpy(LocalBuffer + lenSec + lenKey, sp, INI_BUFFERSIZE - lenSec - lenKey, quotes);
505507
/* call the callback */
506508
if (!Callback(LocalBuffer, LocalBuffer + lenSec, LocalBuffer + lenSec + lenKey, UserData))
507509
break;
@@ -517,7 +519,7 @@ static void ini_tempname(TCHAR *dest, const TCHAR *source, int maxlength)
517519
{
518520
TCHAR *p;
519521

520-
save_strncpy(dest, source, maxlength, QUOTE_NONE);
522+
ini_strncpy(dest, source, maxlength, QUOTE_NONE);
521523
p = _tcsrchr(dest, '\0');
522524
assert(p != NULL);
523525
*(p - 1) = '~';
@@ -541,7 +543,7 @@ static void writesection(TCHAR *LocalBuffer, const TCHAR *Section, INI_FILETYPE
541543
if (Section != NULL && _tcslen(Section) > 0) {
542544
TCHAR *p;
543545
LocalBuffer[0] = '[';
544-
save_strncpy(LocalBuffer + 1, Section, INI_BUFFERSIZE - 4, QUOTE_NONE); /* -1 for '[', -1 for ']', -2 for '\r\n' */
546+
ini_strncpy(LocalBuffer + 1, Section, INI_BUFFERSIZE - 4, QUOTE_NONE); /* -1 for '[', -1 for ']', -2 for '\r\n' */
545547
p = _tcsrchr(LocalBuffer, '\0');
546548
assert(p != NULL);
547549
*p++ = ']';
@@ -555,11 +557,11 @@ static void writekey(TCHAR *LocalBuffer, const TCHAR *Key, const TCHAR *Value, I
555557
{
556558
TCHAR *p;
557559
enum quote_option option = check_enquote(Value);
558-
save_strncpy(LocalBuffer, Key, INI_BUFFERSIZE - 3, QUOTE_NONE); /* -1 for '=', -2 for '\r\n' */
560+
ini_strncpy(LocalBuffer, Key, INI_BUFFERSIZE - 3, QUOTE_NONE); /* -1 for '=', -2 for '\r\n' */
559561
p = _tcsrchr(LocalBuffer, '\0');
560562
assert(p != NULL);
561563
*p++ = '=';
562-
save_strncpy(p, Value, INI_BUFFERSIZE - (p - LocalBuffer) - 2, option); /* -2 for '\r\n' */
564+
ini_strncpy(p, Value, INI_BUFFERSIZE - (p - LocalBuffer) - 2, option); /* -2 for '\r\n' */
563565
p = _tcsrchr(LocalBuffer, '\0');
564566
assert(p != NULL);
565567
_tcscpy(p, INI_LINETERM); /* copy line terminator (typically "\n") */
@@ -697,7 +699,7 @@ int ini_puts(const TCHAR *Section, const TCHAR *Key, const TCHAR *Value, const T
697699
(void)ini_close(&rfp);
698700
return 0;
699701
} /* if */
700-
ini_tell(&rfp, &mark);
702+
(void)ini_tell(&rfp, &mark);
701703
cachelen = 0;
702704

703705
/* Move through the file one line at a time until a section is
@@ -721,7 +723,7 @@ int ini_puts(const TCHAR *Section, const TCHAR *Key, const TCHAR *Value, const T
721723
* we are looking for and this section must be removed
722724
*/
723725
sp = skipleading(LocalBuffer);
724-
ep = _tcschr(sp, ']');
726+
ep = _tcsrchr(sp, ']');
725727
match = (*sp == '[' && ep != NULL && (int)(ep-sp-1) == len && _tcsnicmp(sp + 1,Section,len) == 0);
726728
if (!match || Key != NULL) {
727729
if (!cache_accum(LocalBuffer, &cachelen, INI_BUFFERSIZE)) {
@@ -740,7 +742,7 @@ int ini_puts(const TCHAR *Section, const TCHAR *Key, const TCHAR *Value, const T
740742
*/
741743
if (Key == NULL) {
742744
(void)ini_read(LocalBuffer, INI_BUFFERSIZE, &rfp);
743-
ini_tell(&rfp, &mark);
745+
(void)ini_tell(&rfp, &mark);
744746
} /* if */
745747

746748
/* Now that the section has been found, find the entry. Stop searching
@@ -768,7 +770,7 @@ int ini_puts(const TCHAR *Section, const TCHAR *Key, const TCHAR *Value, const T
768770
break; /* found the key, or found a new section */
769771
/* copy other keys in the section */
770772
if (Key == NULL) {
771-
ini_tell(&rfp, &mark); /* we are deleting the entire section, so update the read position */
773+
(void)ini_tell(&rfp, &mark); /* we are deleting the entire section, so update the read position */
772774
} else {
773775
if (!cache_accum(LocalBuffer, &cachelen, INI_BUFFERSIZE)) {
774776
cache_flush(LocalBuffer, &cachelen, &rfp, &wfp, &mark);
@@ -796,7 +798,7 @@ int ini_puts(const TCHAR *Section, const TCHAR *Key, const TCHAR *Value, const T
796798
cache_accum(LocalBuffer, &cachelen, INI_BUFFERSIZE);
797799
} else {
798800
/* forget the old key line */
799-
ini_tell(&rfp, &mark);
801+
(void)ini_tell(&rfp, &mark);
800802
} /* if */
801803
/* Copy the rest of the INI file */
802804
while (ini_read(LocalBuffer, INI_BUFFERSIZE, &rfp)) {
@@ -827,11 +829,10 @@ static void long2str(long value, TCHAR *str)
827829
{
828830
int i = 0;
829831
long sign = value;
830-
int n;
831832

832833
/* generate digits in reverse order */
833834
do {
834-
n = (int)(value % 10); /* get next lowest digit */
835+
int n = (int)(value % 10); /* get next lowest digit */
835836
str[i++] = (TCHAR)(ABS(n) + '0'); /* handle case of negative digit */
836837
} while (value /= 10); /* delete the lowest digit */
837838
if (sign < 0)

dev/minIni.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* minIni - Multi-Platform INI file parser, suitable for embedded systems
22
*
3-
* Copyright (c) CompuPhase, 2008-2015
3+
* Copyright (c) CompuPhase, 2008-2017
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
66
* use this file except in compliance with the License. You may obtain a copy
@@ -56,8 +56,8 @@ int ini_putf(const mTCHAR *Section, const mTCHAR *Key, INI_REAL Value, const m
5656
#endif /* INI_READONLY */
5757

5858
#if !defined INI_NOBROWSE
59-
typedef int (*INI_CALLBACK)(const mTCHAR *Section, const mTCHAR *Key, const mTCHAR *Value, const void *UserData);
60-
int ini_browse(INI_CALLBACK Callback, const void *UserData, const mTCHAR *Filename);
59+
typedef int (*INI_CALLBACK)(const mTCHAR *Section, const mTCHAR *Key, const mTCHAR *Value, void *UserData);
60+
int ini_browse(INI_CALLBACK Callback, void *UserData, const mTCHAR *Filename);
6161
#endif /* INI_NOBROWSE */
6262

6363
#if defined __cplusplus

dev/test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
const char inifile[] = "test.ini";
1313
const char inifile2[] = "testplain.ini";
1414

15-
int Callback(const char *section, const char *key, const char *value, const void *userdata)
15+
int Callback(const char *section, const char *key, const char *value, void *userdata)
1616
{
1717
(void)userdata; /* this parameter is not used in this example */
1818
printf(" [%s]\t%s=%s\n", section, key, value);
@@ -88,7 +88,7 @@ int main(void)
8888
printf("3. String writing tests passed\n");
8989

9090
/* section/key enumeration */
91-
printf("4. Section/key enumeration, file structure follows\n");
91+
printf("4. Section/key enumertion, file structure follows\n");
9292
for (s = 0; ini_getsection(s, section, sizearray(section), inifile) > 0; s++) {
9393
printf(" [%s]\n", section);
9494
for (k = 0; ini_getkey(section, k, str, sizearray(str), inifile) > 0; k++) {

0 commit comments

Comments
 (0)