Unix 系のプログラムで使用される設定ファイルを読み込むためのC言語ライブラリ
Révision | 036428eba572a421fe0f8dc89f5477f15badb7c3 (tree) |
---|---|
l'heure | 2014-01-22 23:39:01 |
Auteur | tsntsumi <tsntsumi@user...> |
Commiter | tsntsumi |
strdupのない環境のために同等関数 RCDuplicateString() を実装した。
strdupのない環境とは、 gcc --std=c99 などとして STRICT_ANSI モードでコンパイルした場合などである。
@@ -125,6 +125,21 @@ void RCFreeRisedErrors(void) | ||
125 | 125 | RCRisedErrorArrayCapacity = 0; |
126 | 126 | } |
127 | 127 | |
128 | +char *RCDuplicateString(const char *str) | |
129 | +{ | |
130 | + if (str == NULL) | |
131 | + { | |
132 | + return NULL; | |
133 | + } | |
134 | + const size_t length = strlen(str); | |
135 | + char *dup = calloc(1, length + 1); | |
136 | + if (dup != NULL) | |
137 | + { | |
138 | + strcpy(dup, str); | |
139 | + } | |
140 | + return dup; | |
141 | +} | |
142 | + | |
128 | 143 | /** |
129 | 144 | * @brief 設定ファイルのストリームから設定を読み込み、指定の構造体に格納します。 |
130 | 145 | * @param[inout] items 読み込むべき設定項目の名前とデフォルト値を設定した |
@@ -168,7 +183,7 @@ static void initialize(RCConfItem *items, size_t numItems) | ||
168 | 183 | } |
169 | 184 | else |
170 | 185 | { |
171 | - item->defaultValue.string = strdup(item->defaultAsString); | |
186 | + item->defaultValue.string = RCDuplicateString(item->defaultAsString); | |
172 | 187 | if (item->defaultValue.string == NULL) |
173 | 188 | { |
174 | 189 | addError(RCErrorNoMemory, |
@@ -176,7 +191,7 @@ static void initialize(RCConfItem *items, size_t numItems) | ||
176 | 191 | item->name, |
177 | 192 | strerror(errno)); |
178 | 193 | } |
179 | - item->value.string = strdup(item->defaultValue.string); | |
194 | + item->value.string = RCDuplicateString(item->defaultValue.string); | |
180 | 195 | if (item->value.string == NULL) |
181 | 196 | { |
182 | 197 | addError(RCErrorNoMemory, |
@@ -322,7 +337,7 @@ static char *readName(FILE *conffp) | ||
322 | 337 | } |
323 | 338 | } |
324 | 339 | |
325 | - char *shrinked = strdup(name); | |
340 | + char *shrinked = RCDuplicateString(name); | |
326 | 341 | free(name); |
327 | 342 | return shrinked; |
328 | 343 | } |
@@ -541,7 +556,7 @@ static char *readQuotedString(FILE *conffp) | ||
541 | 556 | } |
542 | 557 | } |
543 | 558 | |
544 | - char *shrinked = strdup(string); | |
559 | + char *shrinked = RCDuplicateString(string); | |
545 | 560 | free(string); |
546 | 561 | |
547 | 562 | return shrinked; |
@@ -609,7 +624,7 @@ static char *readBareString(FILE *conffp) | ||
609 | 624 | i ++; |
610 | 625 | string[i] = '\0'; |
611 | 626 | |
612 | - char *shrinked = strdup(string); | |
627 | + char *shrinked = RCDuplicateString(string); | |
613 | 628 | free(string); |
614 | 629 | |
615 | 630 | return shrinked; |
@@ -178,6 +178,13 @@ void RCFreeStringValueInItems(RCConfItem *items, size_t numItems); | ||
178 | 178 | */ |
179 | 179 | void RCFreeRisedErrors(void); |
180 | 180 | |
181 | +/** | |
182 | + * @brief 指定された文字列を複製します。 | |
183 | + * @param str 複製する文字列。 | |
184 | + * @return 複製した文字列。 | |
185 | + */ | |
186 | +char *RCDuplicateString(const char *str); | |
187 | + | |
181 | 188 | #ifndef ELEMENTSOF |
182 | 189 | #define ELEMENTSOF(ARRAY) (sizeof(ARRAY)/sizeof(ARRAY[0])) |
183 | 190 | #endif |