• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
Aucun tag

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

Unix 系のプログラムで使用される設定ファイルを読み込むためのC言語ライブラリ


Commit MetaInfo

Révision036428eba572a421fe0f8dc89f5477f15badb7c3 (tree)
l'heure2014-01-22 23:39:01
Auteurtsntsumi <tsntsumi@user...>
Commitertsntsumi

Message de Log

strdupのない環境のために同等関数 RCDuplicateString() を実装した。

strdupのない環境とは、 gcc --std=c99 などとして STRICT_ANSI モードでコンパイルした場合などである。

Change Summary

Modification

--- a/src/readconf.c
+++ b/src/readconf.c
@@ -125,6 +125,21 @@ void RCFreeRisedErrors(void)
125125 RCRisedErrorArrayCapacity = 0;
126126 }
127127
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+
128143 /**
129144 * @brief 設定ファイルのストリームから設定を読み込み、指定の構造体に格納します。
130145 * @param[inout] items 読み込むべき設定項目の名前とデフォルト値を設定した
@@ -168,7 +183,7 @@ static void initialize(RCConfItem *items, size_t numItems)
168183 }
169184 else
170185 {
171- item->defaultValue.string = strdup(item->defaultAsString);
186+ item->defaultValue.string = RCDuplicateString(item->defaultAsString);
172187 if (item->defaultValue.string == NULL)
173188 {
174189 addError(RCErrorNoMemory,
@@ -176,7 +191,7 @@ static void initialize(RCConfItem *items, size_t numItems)
176191 item->name,
177192 strerror(errno));
178193 }
179- item->value.string = strdup(item->defaultValue.string);
194+ item->value.string = RCDuplicateString(item->defaultValue.string);
180195 if (item->value.string == NULL)
181196 {
182197 addError(RCErrorNoMemory,
@@ -322,7 +337,7 @@ static char *readName(FILE *conffp)
322337 }
323338 }
324339
325- char *shrinked = strdup(name);
340+ char *shrinked = RCDuplicateString(name);
326341 free(name);
327342 return shrinked;
328343 }
@@ -541,7 +556,7 @@ static char *readQuotedString(FILE *conffp)
541556 }
542557 }
543558
544- char *shrinked = strdup(string);
559+ char *shrinked = RCDuplicateString(string);
545560 free(string);
546561
547562 return shrinked;
@@ -609,7 +624,7 @@ static char *readBareString(FILE *conffp)
609624 i ++;
610625 string[i] = '\0';
611626
612- char *shrinked = strdup(string);
627+ char *shrinked = RCDuplicateString(string);
613628 free(string);
614629
615630 return shrinked;
--- a/src/readconf.h
+++ b/src/readconf.h
@@ -178,6 +178,13 @@ void RCFreeStringValueInItems(RCConfItem *items, size_t numItems);
178178 */
179179 void RCFreeRisedErrors(void);
180180
181+/**
182+ * @brief 指定された文字列を複製します。
183+ * @param str 複製する文字列。
184+ * @return 複製した文字列。
185+ */
186+char *RCDuplicateString(const char *str);
187+
181188 #ifndef ELEMENTSOF
182189 #define ELEMENTSOF(ARRAY) (sizeof(ARRAY)/sizeof(ARRAY[0]))
183190 #endif