1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
10 #include "readstrings.h"
14 # define NS_tfopen _wfopen
15 # define OPEN_MODE L"rb"
17 # define NS_tfopen fopen
18 # define OPEN_MODE "r"
21 // stack based FILE wrapper to ensure that fclose is called.
25 explicit AutoFILE(FILE *fp
) : fp_(fp
) {}
41 explicit AutoCharArray(size_t len
)
57 static const char kNL
[] = "\r\n";
58 static const char kEquals
[] = "=";
59 static const char kWhitespace
[] = " \t";
60 static const char kRBracket
[] = "]";
63 NS_strspnp(const char *delims
, const char *str
)
68 for (d
= delims
; *d
!= '\0'; ++d
)
83 NS_strtok(const char *delims
, char **str
)
88 char *ret
= (char*) NS_strspnp(delims
, *str
);
99 for (const char *d
= delims
; *d
!= '\0'; ++d
)
117 * Find a key in a keyList containing zero-delimited keys ending with "\0\0".
118 * Returns a zero-based index of the key in the list, or -1 if the key is not found.
121 find_key(const char *keyList
, char* key
)
127 const char *p
= keyList
;
130 if (strcmp(key
, p
) == 0)
137 // The key was not found if we came here
142 * A very basic parser for updater.ini taken mostly from nsINIParser.cpp
143 * that can be used by standalone apps.
145 * @param path Path to the .ini file to read
146 * @param keyList List of zero-delimited keys ending with two zero characters
147 * @param numStrings Number of strings to read into results buffer - must be equal to the number of keys
148 * @param results Two-dimensional array of strings to be filled in the same order as the keys provided
149 * @param section Optional name of the section to read; defaults to "Strings"
152 ReadStrings(const NS_tchar
*path
,
154 unsigned int numStrings
,
155 char results
[][MAX_TEXT_LEN
],
158 AutoFILE
fp(NS_tfopen(path
, OPEN_MODE
));
164 if (fseek(fp
, 0, SEEK_END
) != 0)
167 long len
= ftell(fp
);
171 size_t flen
= size_t(len
);
172 AutoCharArray
fileContents(flen
+ 1);
174 return READ_STRINGS_MEM_ERROR
;
176 /* read the file in one swoop */
177 if (fseek(fp
, 0, SEEK_SET
) != 0)
180 size_t rd
= fread(fileContents
, sizeof(char), flen
, fp
);
184 fileContents
[flen
] = '\0';
186 char *buffer
= fileContents
;
187 bool inStringsSection
= false;
189 unsigned int read
= 0;
191 while (char *token
= NS_strtok(kNL
, &buffer
))
193 if (token
[0] == '#' || token
[0] == ';') // it's a comment
196 token
= (char*) NS_strspnp(kWhitespace
, token
);
197 if (!*token
) // empty line
200 if (token
[0] == '[') // section header!
203 char const * currSection
= token
;
205 char *rb
= NS_strtok(kRBracket
, &token
);
206 if (!rb
|| NS_strtok(kWhitespace
, &token
))
208 // there's either an unclosed [Section or a [Section]Moretext!
209 // we could frankly decide that this INI file is malformed right
210 // here and stop, but we won't... keep going, looking for
211 // a well-formed [section] to continue working with
212 inStringsSection
= false;
217 inStringsSection
= strcmp(currSection
, section
) == 0;
219 inStringsSection
= strcmp(currSection
, "Strings") == 0;
225 if (!inStringsSection
)
227 // If we haven't found a section header (or we found a malformed
228 // section header), or this isn't the [Strings] section don't bother
229 // parsing this line.
234 char *e
= NS_strtok(kEquals
, &token
);
238 int keyIndex
= find_key(keyList
, key
);
239 if (keyIndex
>= 0 && (unsigned int)keyIndex
< numStrings
)
241 strncpy(results
[keyIndex
], token
, MAX_TEXT_LEN
- 1);
242 results
[keyIndex
][MAX_TEXT_LEN
- 1] = '\0';
247 return (read
== numStrings
) ? OK
: PARSE_ERROR
;
250 // A wrapper function to read strings for the updater.
251 // Added for compatibility with the original code.
253 ReadStrings(const NS_tchar
*path
, StringTable
*results
)
255 const unsigned int kNumStrings
= 2;
256 const char *kUpdaterKeys
= "Title\0Info\0";
257 char updater_strings
[kNumStrings
][MAX_TEXT_LEN
];
259 int result
= ReadStrings(path
, kUpdaterKeys
, kNumStrings
, updater_strings
);
261 strncpy(results
->title
, updater_strings
[0], MAX_TEXT_LEN
- 1);
262 results
->title
[MAX_TEXT_LEN
- 1] = '\0';
263 strncpy(results
->info
, updater_strings
[1], MAX_TEXT_LEN
- 1);
264 results
->info
[MAX_TEXT_LEN
- 1] = '\0';