2014-06-30 19:39:45 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2012-05-23 21:09:10 +04:00
|
|
|
// Moz headers (alphabetical)
|
|
|
|
#include "nsCRTGlue.h"
|
2005-08-15 22:29:55 +04:00
|
|
|
#include "nsError.h"
|
2012-06-06 06:08:30 +04:00
|
|
|
#include "nsIFile.h"
|
2012-05-23 21:09:10 +04:00
|
|
|
#include "nsINIParser.h"
|
|
|
|
#include "mozilla/FileUtils.h" // AutoFILE
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2012-05-23 21:09:10 +04:00
|
|
|
// System headers (alphabetical)
|
2005-08-15 22:29:55 +04:00
|
|
|
#include <stdio.h>
|
2012-05-23 21:09:10 +04:00
|
|
|
#include <stdlib.h>
|
2009-05-13 14:16:45 +04:00
|
|
|
#ifdef XP_WIN
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2009-05-13 14:16:45 +04:00
|
|
|
#if defined(XP_WIN)
|
|
|
|
#define READ_BINARYMODE L"rb"
|
2005-08-16 02:13:56 +04:00
|
|
|
#else
|
2009-05-13 14:16:45 +04:00
|
|
|
#define READ_BINARYMODE "r"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef XP_WIN
|
2014-06-27 05:35:39 +04:00
|
|
|
inline FILE*
|
|
|
|
TS_tfopen(const char* aPath, const wchar_t* aMode)
|
2009-05-13 14:16:45 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
wchar_t wPath[MAX_PATH];
|
|
|
|
MultiByteToWideChar(CP_UTF8, 0, aPath, -1, wPath, MAX_PATH);
|
|
|
|
return _wfopen(wPath, aMode);
|
2009-05-13 14:16:45 +04:00
|
|
|
}
|
|
|
|
#else
|
2014-06-27 05:35:39 +04:00
|
|
|
inline FILE*
|
|
|
|
TS_tfopen(const char* aPath, const char* aMode)
|
2009-05-13 14:16:45 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
return fopen(aPath, aMode);
|
2009-05-13 14:16:45 +04:00
|
|
|
}
|
2005-08-16 02:13:56 +04:00
|
|
|
#endif
|
|
|
|
|
2005-08-15 22:29:55 +04:00
|
|
|
// Stack based FILE wrapper to ensure that fclose is called, copied from
|
2010-02-06 10:25:10 +03:00
|
|
|
// toolkit/mozapps/update/updater/readstrings.cpp
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
class AutoFILE
|
|
|
|
{
|
2005-08-15 22:29:55 +04:00
|
|
|
public:
|
2014-07-30 04:43:56 +04:00
|
|
|
explicit AutoFILE(FILE* aFp = nullptr) : fp_(aFp) {}
|
2014-06-27 05:35:39 +04:00
|
|
|
~AutoFILE()
|
|
|
|
{
|
|
|
|
if (fp_) {
|
|
|
|
fclose(fp_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
operator FILE*() { return fp_; }
|
|
|
|
FILE** operator&() { return &fp_; }
|
|
|
|
void operator=(FILE* aFp) { fp_ = aFp; }
|
2005-08-15 22:29:55 +04:00
|
|
|
private:
|
2014-06-27 05:35:39 +04:00
|
|
|
FILE* fp_;
|
2005-08-15 22:29:55 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
nsresult
|
2012-06-06 06:08:30 +04:00
|
|
|
nsINIParser::Init(nsIFile* aFile)
|
2005-08-15 22:29:55 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
/* open the file. Don't use OpenANSIFileDesc, because you mustn't
|
|
|
|
pass FILE* across shared library boundaries, which may be using
|
|
|
|
different CRTs */
|
2007-05-25 19:05:11 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
AutoFILE fd;
|
2007-05-25 19:05:11 +04:00
|
|
|
|
|
|
|
#ifdef XP_WIN
|
2014-06-27 05:35:39 +04:00
|
|
|
nsAutoString path;
|
|
|
|
nsresult rv = aFile->GetPath(path);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2007-05-25 19:05:11 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
fd = _wfopen(path.get(), READ_BINARYMODE);
|
2007-05-25 19:05:11 +04:00
|
|
|
#else
|
2014-06-27 05:35:39 +04:00
|
|
|
nsAutoCString path;
|
|
|
|
aFile->GetNativePath(path);
|
2007-05-25 19:05:11 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
fd = fopen(path.get(), READ_BINARYMODE);
|
2007-05-25 19:05:11 +04:00
|
|
|
#endif
|
2009-05-13 14:16:45 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
if (!fd) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
return InitFromFILE(fd);
|
2005-08-15 22:29:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-06-27 05:35:39 +04:00
|
|
|
nsINIParser::Init(const char* aPath)
|
2005-08-15 22:29:55 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
/* open the file */
|
2014-07-30 04:43:56 +04:00
|
|
|
AutoFILE fd(TS_tfopen(aPath, READ_BINARYMODE));
|
2014-06-27 05:35:39 +04:00
|
|
|
if (!fd) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
return InitFromFILE(fd);
|
2005-08-15 22:29:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char kNL[] = "\r\n";
|
|
|
|
static const char kEquals[] = "=";
|
|
|
|
static const char kWhitespace[] = " \t";
|
|
|
|
static const char kRBracket[] = "]";
|
|
|
|
|
|
|
|
nsresult
|
2014-06-27 05:35:39 +04:00
|
|
|
nsINIParser::InitFromFILE(FILE* aFd)
|
2005-08-15 22:29:55 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
/* get file size */
|
|
|
|
if (fseek(aFd, 0, SEEK_END) != 0) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-05-23 21:09:10 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
long flen = ftell(aFd);
|
2015-04-28 19:09:31 +03:00
|
|
|
/* zero-sized file, or an error */
|
|
|
|
if (flen <= 0) {
|
2014-06-27 05:35:39 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* malloc an internal buf the size of the file */
|
|
|
|
mFileContents = new char[flen + 2];
|
|
|
|
if (!mFileContents) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read the file in one swoop */
|
|
|
|
if (fseek(aFd, 0, SEEK_SET) != 0) {
|
|
|
|
return NS_BASE_STREAM_OSERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rd = fread(mFileContents, sizeof(char), flen, aFd);
|
|
|
|
if (rd != flen) {
|
|
|
|
return NS_BASE_STREAM_OSERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We write a UTF16 null so that the file is easier to convert to UTF8
|
|
|
|
mFileContents[flen] = mFileContents[flen + 1] = '\0';
|
|
|
|
|
|
|
|
char* buffer = &mFileContents[0];
|
|
|
|
|
|
|
|
if (flen >= 3 &&
|
2015-01-16 08:34:42 +03:00
|
|
|
mFileContents[0] == '\xEF' &&
|
|
|
|
mFileContents[1] == '\xBB' &&
|
|
|
|
mFileContents[2] == '\xBF') {
|
2014-06-27 05:35:39 +04:00
|
|
|
// Someone set us up the Utf-8 BOM
|
|
|
|
// This case is easy, since we assume that BOM-less
|
|
|
|
// files are Utf-8 anyway. Just skip the BOM and process as usual.
|
|
|
|
buffer = &mFileContents[3];
|
|
|
|
}
|
2012-05-23 21:09:10 +04:00
|
|
|
|
|
|
|
#ifdef XP_WIN
|
2014-06-27 05:35:39 +04:00
|
|
|
if (flen >= 2 &&
|
2015-01-16 08:34:42 +03:00
|
|
|
mFileContents[0] == '\xFF' &&
|
|
|
|
mFileContents[1] == '\xFE') {
|
2014-06-27 05:35:39 +04:00
|
|
|
// Someone set us up the Utf-16LE BOM
|
|
|
|
buffer = &mFileContents[2];
|
|
|
|
// Get the size required for our Utf8 buffer
|
|
|
|
flen = WideCharToMultiByte(CP_UTF8,
|
|
|
|
0,
|
|
|
|
reinterpret_cast<LPWSTR>(buffer),
|
|
|
|
-1,
|
|
|
|
nullptr,
|
|
|
|
0,
|
|
|
|
nullptr,
|
|
|
|
nullptr);
|
|
|
|
if (flen == 0) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-05-23 21:09:10 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
nsAutoArrayPtr<char> utf8Buffer(new char[flen]);
|
|
|
|
if (WideCharToMultiByte(CP_UTF8, 0, reinterpret_cast<LPWSTR>(buffer), -1,
|
|
|
|
utf8Buffer, flen, nullptr, nullptr) == 0) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2012-05-23 21:09:10 +04:00
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
mFileContents = utf8Buffer.forget();
|
|
|
|
buffer = mFileContents;
|
|
|
|
}
|
2012-05-23 21:09:10 +04:00
|
|
|
#endif
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
char* currSection = nullptr;
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
// outer loop tokenizes into lines
|
|
|
|
while (char* token = NS_strtok(kNL, &buffer)) {
|
|
|
|
if (token[0] == '#' || token[0] == ';') { // it's a comment
|
|
|
|
continue;
|
|
|
|
}
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
token = (char*)NS_strspnp(kWhitespace, token);
|
|
|
|
if (!*token) { // empty line
|
|
|
|
continue;
|
|
|
|
}
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
if (token[0] == '[') { // section header!
|
|
|
|
++token;
|
|
|
|
currSection = token;
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
char* rb = NS_strtok(kRBracket, &token);
|
|
|
|
if (!rb || NS_strtok(kWhitespace, &token)) {
|
|
|
|
// there's either an unclosed [Section or a [Section]Moretext!
|
|
|
|
// we could frankly decide that this INI file is malformed right
|
|
|
|
// here and stop, but we won't... keep going, looking for
|
|
|
|
// a well-formed [section] to continue working with
|
|
|
|
currSection = nullptr;
|
|
|
|
}
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
continue;
|
|
|
|
}
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
if (!currSection) {
|
|
|
|
// If we haven't found a section header (or we found a malformed
|
|
|
|
// section header), don't bother parsing this line.
|
|
|
|
continue;
|
|
|
|
}
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
char* key = token;
|
|
|
|
char* e = NS_strtok(kEquals, &token);
|
|
|
|
if (!e || !token) {
|
|
|
|
continue;
|
|
|
|
}
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
INIValue* v;
|
|
|
|
if (!mSections.Get(currSection, &v)) {
|
|
|
|
v = new INIValue(key, token);
|
|
|
|
if (!v) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
mSections.Put(currSection, v);
|
|
|
|
continue;
|
|
|
|
}
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
// Check whether this key has already been specified; overwrite
|
|
|
|
// if so, or append if not.
|
|
|
|
while (v) {
|
|
|
|
if (!strcmp(key, v->key)) {
|
|
|
|
v->value = token;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!v->next) {
|
|
|
|
v->next = new INIValue(key, token);
|
|
|
|
if (!v->next) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2010-02-10 04:05:31 +03:00
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
v = v->next;
|
2005-08-15 22:29:55 +04:00
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
NS_ASSERTION(v, "v should never be null coming out of this loop");
|
|
|
|
}
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
return NS_OK;
|
2005-08-15 22:29:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-06-27 05:35:39 +04:00
|
|
|
nsINIParser::GetString(const char* aSection, const char* aKey,
|
|
|
|
nsACString& aResult)
|
2005-08-15 22:29:55 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
INIValue* val;
|
|
|
|
mSections.Get(aSection, &val);
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
while (val) {
|
|
|
|
if (strcmp(val->key, aKey) == 0) {
|
|
|
|
aResult.Assign(val->value);
|
|
|
|
return NS_OK;
|
2005-08-15 22:29:55 +04:00
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
val = val->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
2005-08-15 22:29:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-06-27 05:35:39 +04:00
|
|
|
nsINIParser::GetString(const char* aSection, const char* aKey,
|
|
|
|
char* aResult, uint32_t aResultLen)
|
2005-08-15 22:29:55 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
INIValue* val;
|
|
|
|
mSections.Get(aSection, &val);
|
|
|
|
|
|
|
|
while (val) {
|
|
|
|
if (strcmp(val->key, aKey) == 0) {
|
|
|
|
strncpy(aResult, val->value, aResultLen);
|
|
|
|
aResult[aResultLen - 1] = '\0';
|
|
|
|
if (strlen(val->value) >= aResultLen) {
|
|
|
|
return NS_ERROR_LOSS_OF_SIGNIFICANT_DATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2005-08-15 22:29:55 +04:00
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
val = val->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
2005-08-15 22:29:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
PLDHashOperator
|
2014-06-27 05:35:39 +04:00
|
|
|
nsINIParser::GetSectionsCB(const char* aKey, INIValue* aData,
|
|
|
|
void* aClosure)
|
2005-08-15 22:29:55 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
GSClosureStruct* cs = reinterpret_cast<GSClosureStruct*>(aClosure);
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
return cs->usercb(aKey, cs->userclosure) ? PL_DHASH_NEXT : PL_DHASH_STOP;
|
2005-08-15 22:29:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-06-27 05:35:39 +04:00
|
|
|
nsINIParser::GetSections(INISectionCallback aCB, void* aClosure)
|
2005-08-15 22:29:55 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
GSClosureStruct gs = {
|
|
|
|
aCB,
|
|
|
|
aClosure
|
|
|
|
};
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
mSections.EnumerateRead(GetSectionsCB, &gs);
|
|
|
|
return NS_OK;
|
2005-08-15 22:29:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-06-27 05:35:39 +04:00
|
|
|
nsINIParser::GetStrings(const char* aSection,
|
|
|
|
INIStringCallback aCB, void* aClosure)
|
2005-08-15 22:29:55 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
INIValue* val;
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
for (mSections.Get(aSection, &val);
|
|
|
|
val;
|
|
|
|
val = val->next) {
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
if (!aCB(val->key, val->value, aClosure)) {
|
|
|
|
return NS_OK;
|
2005-08-15 22:29:55 +04:00
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
return NS_OK;
|
2005-08-15 22:29:55 +04:00
|
|
|
}
|