nss: upgrade to release 3.73
[LibreOffice.git] / onlineupdate / source / update / common / readstrings.cxx
blobe1366cdddaed4d509bf8f8a3e383befb6aa3ac62
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/. */
7 #include <limits.h>
8 #include <string.h>
9 #include <stdio.h>
10 #include "readstrings.h"
11 #include "errors.h"
13 #ifdef _WIN32
14 # define NS_tfopen _wfopen
15 # define OPEN_MODE L"rb"
16 #else
17 # define NS_tfopen fopen
18 # define OPEN_MODE "r"
19 #endif
21 // stack based FILE wrapper to ensure that fclose is called.
22 class AutoFILE
24 public:
25 explicit AutoFILE(FILE *fp) : fp_(fp) {}
26 ~AutoFILE()
28 if (fp_) fclose(fp_);
30 operator FILE *()
32 return fp_;
34 private:
35 FILE *fp_;
38 class AutoCharArray
40 public:
41 explicit AutoCharArray(size_t len)
43 ptr_ = new char[len];
45 ~AutoCharArray()
47 delete[] ptr_;
49 operator char *()
51 return ptr_;
53 private:
54 char *ptr_;
57 static const char kNL[] = "\r\n";
58 static const char kEquals[] = "=";
59 static const char kWhitespace[] = " \t";
60 static const char kRBracket[] = "]";
62 static const char*
63 NS_strspnp(const char *delims, const char *str)
65 const char *d;
68 for (d = delims; *d != '\0'; ++d)
70 if (*str == *d)
72 ++str;
73 break;
77 while (*d);
79 return str;
82 static char*
83 NS_strtok(const char *delims, char **str)
85 if (!*str)
86 return nullptr;
88 char *ret = (char*) NS_strspnp(delims, *str);
90 if (!*ret)
92 *str = ret;
93 return nullptr;
96 char *i = ret;
99 for (const char *d = delims; *d != '\0'; ++d)
101 if (*i == *d)
103 *i = '\0';
104 *str = ++i;
105 return ret;
108 ++i;
110 while (*i);
112 *str = nullptr;
113 return ret;
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.
120 static int
121 find_key(const char *keyList, char* key)
123 if (!keyList)
124 return -1;
126 int index = 0;
127 const char *p = keyList;
128 while (*p)
130 if (strcmp(key, p) == 0)
131 return index;
133 p += strlen(p) + 1;
134 index++;
137 // The key was not found if we came here
138 return -1;
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,
153 const char *keyList,
154 unsigned int numStrings,
155 char results[][MAX_TEXT_LEN],
156 const char *section)
158 AutoFILE fp(NS_tfopen(path, OPEN_MODE));
160 if (!fp)
161 return READ_ERROR;
163 /* get file size */
164 if (fseek(fp, 0, SEEK_END) != 0)
165 return READ_ERROR;
167 long len = ftell(fp);
168 if (len <= 0)
169 return READ_ERROR;
171 size_t flen = size_t(len);
172 AutoCharArray fileContents(flen + 1);
173 if (!fileContents)
174 return READ_STRINGS_MEM_ERROR;
176 /* read the file in one swoop */
177 if (fseek(fp, 0, SEEK_SET) != 0)
178 return READ_ERROR;
180 size_t rd = fread(fileContents, sizeof(char), flen, fp);
181 if (rd != flen)
182 return READ_ERROR;
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
194 continue;
196 token = (char*) NS_strspnp(kWhitespace, token);
197 if (!*token) // empty line
198 continue;
200 if (token[0] == '[') // section header!
202 ++token;
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;
214 else
216 if (section)
217 inStringsSection = strcmp(currSection, section) == 0;
218 else
219 inStringsSection = strcmp(currSection, "Strings") == 0;
222 continue;
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.
230 continue;
233 char *key = token;
234 char *e = NS_strtok(kEquals, &token);
235 if (!e)
236 continue;
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';
243 read++;
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';
266 return result;