PageRenderTime 52ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/gecko_api/include/nsINIParser.h

http://firefox-mac-pdf.googlecode.com/
C Header | 162 lines | 52 code | 22 blank | 88 comment | 0 complexity | d0b3eb5f8ebf8611732e96fde17ec31d MD5 | raw file
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * The Original Code is Mozilla Communicator client code, released
  16. * March 31, 1998.
  17. *
  18. * The Initial Developer of the Original Code is
  19. * Netscape Communications Corporation.
  20. * Portions created by the Initial Developer are Copyright (C) 1998
  21. * the Initial Developer. All Rights Reserved.
  22. *
  23. * Contributor(s):
  24. * Samir Gehani <sgehani@netscape.com>
  25. * Benjamin Smedberg <bsmedberg@covad.net>
  26. *
  27. * Alternatively, the contents of this file may be used under the terms of
  28. * either the GNU General Public License Version 2 or later (the "GPL"), or
  29. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  30. * in which case the provisions of the GPL or the LGPL are applicable instead
  31. * of those above. If you wish to allow use of your version of this file only
  32. * under the terms of either the GPL or the LGPL, and not to allow others to
  33. * use your version of this file under the terms of the MPL, indicate your
  34. * decision by deleting the provisions above and replace them with the notice
  35. * and other provisions required by the GPL or the LGPL. If you do not delete
  36. * the provisions above, a recipient may use your version of this file under
  37. * the terms of any one of the MPL, the GPL or the LGPL.
  38. *
  39. * ***** END LICENSE BLOCK ***** */
  40. // This file was shamelessly copied from mozilla/xpinstall/wizard/unix/src2
  41. #ifndef nsINIParser_h__
  42. #define nsINIParser_h__
  43. #ifdef MOZILLA_INTERNAL_API
  44. #define nsINIParser nsINIParser_internal
  45. #endif
  46. #include "nscore.h"
  47. #include "nsClassHashtable.h"
  48. #include "nsAutoPtr.h"
  49. #include <stdio.h>
  50. class nsILocalFile;
  51. class NS_COM_GLUE nsINIParser
  52. {
  53. public:
  54. nsINIParser() { }
  55. ~nsINIParser() { }
  56. /**
  57. * Initialize the INIParser with a nsILocalFile. If this method fails, no
  58. * other methods should be called. This method reads and parses the file,
  59. * the class does not hold a file handle open. An instance must only be
  60. * initialized once.
  61. */
  62. nsresult Init(nsILocalFile* aFile);
  63. /**
  64. * Initialize the INIParser with a file path. If this method fails, no
  65. * other methods should be called. This method reads and parses the file,
  66. * the class does not hold a file handle open. An instance must only
  67. * be initialized once.
  68. */
  69. nsresult Init(const char *aPath);
  70. /**
  71. * Callback for GetSections
  72. * @return PR_FALSE to stop enumeration, or PR_TRUE to continue.
  73. */
  74. typedef PRBool
  75. (* PR_CALLBACK INISectionCallback)(const char *aSection,
  76. void *aClosure);
  77. /**
  78. * Enumerate the sections within the INI file.
  79. */
  80. nsresult GetSections(INISectionCallback aCB, void *aClosure);
  81. /**
  82. * Callback for GetStrings
  83. * @return PR_FALSE to stop enumeration, or PR_TRUE to continue
  84. */
  85. typedef PRBool
  86. (* PR_CALLBACK INIStringCallback)(const char *aString,
  87. const char *aValue,
  88. void *aClosure);
  89. /**
  90. * Enumerate the strings within a section. If the section does
  91. * not exist, this function will silently return.
  92. */
  93. nsresult GetStrings(const char *aSection,
  94. INIStringCallback aCB, void *aClosure);
  95. /**
  96. * Get the value of the specified key in the specified section
  97. * of the INI file represented by this instance.
  98. *
  99. * @param aSection section name
  100. * @param aKey key name
  101. * @param aResult the value found
  102. * @throws NS_ERROR_FAILURE if the specified section/key could not be
  103. * found.
  104. */
  105. nsresult GetString(const char *aSection, const char *aKey,
  106. nsACString &aResult);
  107. /**
  108. * Alternate signature of GetString that uses a pre-allocated buffer
  109. * instead of a nsACString (for use in the standalone glue before
  110. * the glue is initialized).
  111. *
  112. * @throws NS_ERROR_LOSS_OF_SIGNIFICANT_DATA if the aResult buffer is not
  113. * large enough for the data. aResult will be filled with as
  114. * much data as possible.
  115. *
  116. * @see GetString [1]
  117. */
  118. nsresult GetString(const char *aSection, const char* aKey,
  119. char *aResult, PRUint32 aResultLen);
  120. private:
  121. struct INIValue
  122. {
  123. INIValue(const char *aKey, const char *aValue)
  124. : key(aKey), value(aValue) { }
  125. const char *key;
  126. const char *value;
  127. nsAutoPtr<INIValue> next;
  128. };
  129. struct GSClosureStruct
  130. {
  131. INISectionCallback usercb;
  132. void *userclosure;
  133. };
  134. nsClassHashtable<nsDepCharHashKey, INIValue> mSections;
  135. nsAutoArrayPtr<char> mFileContents;
  136. nsresult InitFromFILE(FILE *fd);
  137. static PLDHashOperator GetSectionsCB(const char *aKey,
  138. INIValue *aData, void *aClosure);
  139. };
  140. #endif /* nsINIParser_h__ */