Back to index

lightning-sunbird  0.9+nobinonly
nsINIParser.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code, released
00016  * March 31, 1998.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 1998
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Samir Gehani <sgehani@netscape.com>
00025  *   Benjamin Smedberg <bsmedberg@covad.net>
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either the GNU General Public License Version 2 or later (the "GPL"), or
00029  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 
00041 #include "nsINIParser.h"
00042 #include "nsError.h"
00043 #include "nsILocalFile.h"
00044 
00045 #include <stdlib.h>
00046 #include <stdio.h>
00047 
00048 #if defined(XP_WIN) || defined(XP_OS2)
00049 #define BINARY_MODE "b"
00050 #else
00051 #define BINARY_MODE
00052 #endif
00053 
00054 // Stack based FILE wrapper to ensure that fclose is called, copied from
00055 // toolkit/mozapps/update/src/updater/readstrings.cpp
00056 
00057 class AutoFILE {
00058 public:
00059   AutoFILE(FILE *fp = nsnull) : fp_(fp) {}
00060   ~AutoFILE() { if (fp_) fclose(fp_); }
00061   operator FILE *() { return fp_; }
00062   FILE** operator &() { return &fp_; }
00063 private:
00064   FILE *fp_;
00065 };
00066 
00067 nsresult
00068 nsINIParser::Init(nsILocalFile* aFile)
00069 {
00070     nsresult rv;
00071 
00072     /* open the file */
00073     AutoFILE fd;
00074     rv = aFile->OpenANSIFileDesc("r" BINARY_MODE, &fd);
00075     if (NS_FAILED(rv))
00076       return rv;
00077 
00078     return InitFromFILE(fd);
00079 }
00080 
00081 nsresult
00082 nsINIParser::Init(const char *aPath)
00083 {
00084     /* open the file */
00085     AutoFILE fd = fopen(aPath, "r" BINARY_MODE);
00086     if (!fd)
00087         return NS_ERROR_FAILURE;
00088 
00089     return InitFromFILE(fd);
00090 }
00091 
00092 // Based on toolkit/mozapps/updater/src/updater/updater.cpp
00093 // we could use nsCRT::strtok except that nsCRT isn't part of the glue,
00094 // and may never be due to NSPR dependencies. This should probably be declared
00095 // and exported in a string-management glue header.
00096 
00100 static char*
00101 mstrspnp(const char *delims, char *str)
00102 {
00103   const char *d;
00104   do {
00105     for (d = delims; *d != '\0'; ++d) {
00106       if (*str == *d) {
00107         ++str;
00108         break;
00109       }
00110     }
00111   } while (*d);
00112 
00113   return str;
00114 }
00115 
00116 static char*
00117 mstrtok(const char *delims, char **str)
00118 {
00119   if (!*str)
00120     return NULL;
00121 
00122   char *ret = mstrspnp(delims, *str);
00123 
00124   if (!*ret) {
00125     *str = ret;
00126     return NULL;
00127   }
00128 
00129   char *i = ret;
00130   do {
00131     for (const char *d = delims; *d != '\0'; ++d) {
00132       if (*i == *d) {
00133         *i = '\0';
00134         *str = ++i;
00135         return ret;
00136       }
00137     }
00138     ++i;
00139   } while (*i);
00140 
00141   *str = NULL;
00142   return ret;
00143 }
00144 
00145 static const char kNL[] = "\r\n";
00146 static const char kEquals[] = "=";
00147 static const char kWhitespace[] = " \t";
00148 static const char kRBracket[] = "]";
00149 
00150 nsresult
00151 nsINIParser::InitFromFILE(FILE *fd)
00152 {
00153     if (!mSections.Init())
00154         return NS_ERROR_OUT_OF_MEMORY;
00155 
00156     /* get file size */
00157     if (fseek(fd, 0, SEEK_END) != 0)
00158         return NS_ERROR_FAILURE;
00159 
00160     long flen = ftell(fd);
00161     if (flen == 0)
00162         return NS_ERROR_FAILURE;
00163 
00164     /* malloc an internal buf the size of the file */
00165     mFileContents = new char[flen + 1];
00166     if (!mFileContents)
00167         return NS_ERROR_OUT_OF_MEMORY;
00168 
00169     /* read the file in one swoop */
00170     if (fseek(fd, 0, SEEK_SET) != 0)
00171         return NS_BASE_STREAM_OSERROR;
00172 
00173     int rd = fread(mFileContents, sizeof(char), flen, fd);
00174     if (rd != flen)
00175         return NS_BASE_STREAM_OSERROR;
00176 
00177     mFileContents[flen] = '\0';
00178 
00179     char *buffer = mFileContents;
00180     char *currSection = nsnull;
00181     INIValue *last = nsnull;
00182 
00183     // outer loop tokenizes into lines
00184     while (char *token = mstrtok(kNL, &buffer)) {
00185         if (token[0] == '#' || token[0] == ';') // it's a comment
00186             continue;
00187 
00188         token = mstrspnp(kWhitespace, token);
00189         if (!*token) // empty line
00190             continue;
00191 
00192         if (token[0] == '[') { // section header!
00193             ++token;
00194             currSection = token;
00195             last = nsnull;
00196 
00197             char *rb = mstrtok(kRBracket, &token);
00198             if (!rb || mstrtok(kWhitespace, &token)) {
00199                 // there's either an unclosed [Section or a [Section]Moretext!
00200                 // we could frankly decide that this INI file is malformed right
00201                 // here and stop, but we won't... keep going, looking for
00202                 // a well-formed [section] to continue working with
00203                 currSection = nsnull;
00204             }
00205 
00206             continue;
00207         }
00208 
00209         if (!currSection) {
00210             // If we haven't found a section header (or we found a malformed
00211             // section header), don't bother parsing this line.
00212             continue;
00213         }
00214 
00215         char *key = token;
00216         char *e = mstrtok(kEquals, &token);
00217         if (!e)
00218             continue;
00219 
00220         INIValue *val = new INIValue(key, token);
00221         if (!val)
00222             return NS_ERROR_OUT_OF_MEMORY;
00223 
00224         // If we haven't already added something to this section, "last" will
00225         // be null.
00226         if (!last) {
00227             mSections.Get(currSection, &last);
00228             while (last && last->next)
00229                 last = last->next;
00230         }
00231 
00232         if (last) {
00233             // Add this element on to the tail of the existing list
00234 
00235             last->next = val;
00236             last = val;
00237             continue;
00238         }
00239 
00240         // We've never encountered this section before, add it to the head
00241         mSections.Put(currSection, val);
00242     }
00243 
00244     return NS_OK;
00245 }
00246 
00247 nsresult
00248 nsINIParser::GetString(const char *aSection, const char *aKey, 
00249                        nsACString &aResult)
00250 {
00251     INIValue *val;
00252     mSections.Get(aSection, &val);
00253 
00254     while (val) {
00255         if (strcmp(val->key, aKey) == 0) {
00256             aResult.Assign(val->value);
00257             return NS_OK;
00258         }
00259 
00260         val = val->next;
00261     }
00262 
00263     return NS_ERROR_FAILURE;
00264 }
00265 
00266 nsresult
00267 nsINIParser::GetString(const char *aSection, const char *aKey, 
00268                        char *aResult, PRUint32 aResultLen)
00269 {
00270     INIValue *val;
00271     mSections.Get(aSection, &val);
00272 
00273     while (val) {
00274         if (strcmp(val->key, aKey) == 0) {
00275             strncpy(aResult, val->value, aResultLen);
00276             aResult[aResultLen - 1] = '\0';
00277             if (strlen(val->value) >= aResultLen)
00278                 return NS_ERROR_LOSS_OF_SIGNIFICANT_DATA;
00279 
00280             return NS_OK;
00281         }
00282 
00283         val = val->next;
00284     }
00285 
00286     return NS_ERROR_FAILURE;
00287 }
00288 
00289 PLDHashOperator
00290 nsINIParser::GetSectionsCB(const char *aKey, INIValue *aData,
00291                            void *aClosure)
00292 {
00293     GSClosureStruct *cs = NS_REINTERPRET_CAST(GSClosureStruct*, aClosure);
00294 
00295     return cs->usercb(aKey, cs->userclosure) ? PL_DHASH_NEXT : PL_DHASH_STOP;
00296 }
00297 
00298 nsresult
00299 nsINIParser::GetSections(INISectionCallback aCB, void *aClosure)
00300 {
00301     GSClosureStruct gs = {
00302         aCB,
00303         aClosure
00304     };
00305 
00306     mSections.EnumerateRead(GetSectionsCB, &gs);
00307     return NS_OK;
00308 }
00309 
00310 nsresult
00311 nsINIParser::GetStrings(const char *aSection,
00312                         INIStringCallback aCB, void *aClosure)
00313 {
00314     INIValue *val;
00315 
00316     for (mSections.Get(aSection, &val);
00317          val;
00318          val = val->next) {
00319 
00320         if (!aCB(val->key, val->value, aClosure))
00321             return NS_OK;
00322     }
00323 
00324     return NS_OK;
00325 }