prerror.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  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 the Netscape Portable Runtime (NSPR).
  16. *
  17. * The Initial Developer of the Original Code is
  18. * Netscape Communications Corporation.
  19. * Portions created by the Initial Developer are Copyright (C) 1998-2000
  20. * the Initial Developer. All Rights Reserved.
  21. *
  22. * Contributor(s):
  23. *
  24. * Alternatively, the contents of this file may be used under the terms of
  25. * either the GNU General Public License Version 2 or later (the "GPL"), or
  26. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27. * in which case the provisions of the GPL or the LGPL are applicable instead
  28. * of those above. If you wish to allow use of your version of this file only
  29. * under the terms of either the GPL or the LGPL, and not to allow others to
  30. * use your version of this file under the terms of the MPL, indicate your
  31. * decision by deleting the provisions above and replace them with the notice
  32. * and other provisions required by the GPL or the LGPL. If you do not delete
  33. * the provisions above, a recipient may use your version of this file under
  34. * the terms of any one of the MPL, the GPL or the LGPL.
  35. *
  36. * ***** END LICENSE BLOCK ***** */
  37. #ifndef prerror_h___
  38. #define prerror_h___
  39. #include "prtypes.h"
  40. PR_BEGIN_EXTERN_C
  41. typedef PRInt32 PRErrorCode;
  42. #define PR_NSPR_ERROR_BASE -6000
  43. #include "prerr.h"
  44. /*
  45. ** Set error will preserve an error condition within a thread context.
  46. ** The values stored are the NSPR (platform independent) translation of
  47. ** the error. Also, if available, the platform specific oserror is stored.
  48. ** If there is no appropriate OS error number, a zero my be supplied.
  49. */
  50. NSPR_API(void) PR_SetError(PRErrorCode errorCode, PRInt32 oserr);
  51. /*
  52. ** The text value specified may be NULL. If it is not NULL and the text length
  53. ** is zero, the string is assumed to be a null terminated C string. Otherwise
  54. ** the text is assumed to be the length specified and possibly include NULL
  55. ** characters (e.g., a multi-national string).
  56. **
  57. ** The text will be copied into to thread structure and remain there
  58. ** until the next call to PR_SetError.
  59. */
  60. NSPR_API(void) PR_SetErrorText(
  61. PRIntn textLength, const char *text);
  62. /*
  63. ** Return the current threads last set error code.
  64. */
  65. NSPR_API(PRErrorCode) PR_GetError(void);
  66. /*
  67. ** Return the current threads last set os error code. This is used for
  68. ** machine specific code that desires the underlying os error.
  69. */
  70. NSPR_API(PRInt32) PR_GetOSError(void);
  71. /*
  72. ** Get the length of the error text. If a zero is returned, then there
  73. ** is no text. Otherwise, the value returned is sufficient to contain
  74. ** the error text currently available.
  75. */
  76. NSPR_API(PRInt32) PR_GetErrorTextLength(void);
  77. /*
  78. ** Copy the current threads current error text. Then actual number of bytes
  79. ** copied is returned as the result. If the result is zero, the 'text' area
  80. ** is unaffected.
  81. */
  82. NSPR_API(PRInt32) PR_GetErrorText(char *text);
  83. /*
  84. Copyright (C) 1987, 1988 Student Information Processing Board of the
  85. Massachusetts Institute of Technology.
  86. Permission to use, copy, modify, and distribute this software and its
  87. documentation for any purpose and without fee is hereby granted, provided
  88. that the above copyright notice appear in all copies and that both that
  89. copyright notice and this permission notice appear in supporting
  90. documentation, and that the names of M.I.T. and the M.I.T. S.I.P.B. not be
  91. used in advertising or publicity pertaining to distribution of the software
  92. without specific, written prior permission. M.I.T. and the M.I.T. S.I.P.B.
  93. make no representations about the suitability of this software for any
  94. purpose. It is provided "as is" without express or implied warranty.
  95. */
  96. /*
  97. * NOTE:
  98. * The interfaces for error-code-translation described in the rest of
  99. * this file are preliminary in the 3.1 release of nspr and are subject
  100. * to change in future releases.
  101. */
  102. /*
  103. ** Description: Localizable error code to string function.
  104. **
  105. **
  106. ** NSPR provides a mechanism for converting an error code to a
  107. ** descriptive string, in a caller-specified language.
  108. **
  109. ** Error codes themselves are 32 bit (signed) integers. Typically,
  110. ** the high order 24 bits are an identifier of which error table the
  111. ** error code is from, and the low order 8 bits are a sequential error
  112. ** number within the table. NSPR supports error tables whose first
  113. ** error code is not a multiple of 256, such error code assignments
  114. ** should be avoided when possible.
  115. **
  116. ** Error table 0 is defined to match the UNIX system call error table
  117. ** (sys_errlist); this allows errno values to be used directly in the
  118. ** library. Other error table numbers are typically formed by
  119. ** compacting together the first four characters of the error table
  120. ** name. The mapping between characters in the name and numeric
  121. ** values in the error code are defined in a system-independent
  122. ** fashion, so that two systems that can pass integral values between
  123. ** them can reliably pass error codes without loss of meaning; this
  124. ** should work even if the character sets used are not the
  125. ** same. (However, if this is to be done, error table 0 should be
  126. ** avoided, since the local system call error tables may differ.)
  127. **
  128. ** Libraries defining error codes need only provide a table mapping
  129. ** error code numbers to names and default English descriptions,
  130. ** calling a routine to install the table, making it ``known'' to NSPR
  131. ** library. Once installed, a table may not be removed. Any error
  132. ** code the library generates can be converted to the corresponding
  133. ** error message. There is also a default format for error codes
  134. ** accidentally returned before making the table known, which is of
  135. ** the form "unknown code foo 32", where "foo" would be the name of
  136. ** the table.
  137. **
  138. ** Normally, the error code conversion routine only supports the
  139. ** languages "i-default" and "en", returning the error-table-provided
  140. ** English description for both languages. The application may
  141. ** provide a localization plugin, allowing support for additional
  142. ** languages.
  143. **
  144. **/
  145. /**********************************************************************/
  146. /************************* TYPES AND CONSTANTS ************************/
  147. /**********************************************************************/
  148. /*
  149. * PRLanguageCode --
  150. *
  151. * NSPR represents a language code as a non-negative integer.
  152. * Languages 0 is always "i-default" the language you get without
  153. * explicit negotiation. Language 1 is always "en", English
  154. * which has been explicitly negotiated. Additional language
  155. * codes are defined by an application-provided localization plugin.
  156. */
  157. typedef PRUint32 PRLanguageCode;
  158. #define PR_LANGUAGE_I_DEFAULT 0 /* i-default, the default language */
  159. #define PR_LANGUAGE_EN 1 /* English, explicitly negotiated */
  160. /*
  161. * struct PRErrorMessage --
  162. *
  163. * An error message in an error table.
  164. */
  165. struct PRErrorMessage {
  166. const char * name; /* Macro name for error */
  167. const char * en_text; /* Default English text */
  168. };
  169. /*
  170. * struct PRErrorTable --
  171. *
  172. * An error table, provided by a library.
  173. */
  174. struct PRErrorTable {
  175. const struct PRErrorMessage * msgs; /* Array of error information */
  176. const char *name; /* Name of error table source */
  177. PRErrorCode base; /* Error code for first error in table */
  178. int n_msgs; /* Number of codes in table */
  179. };
  180. /*
  181. * struct PRErrorCallbackPrivate --
  182. *
  183. * A private structure for the localization plugin
  184. */
  185. struct PRErrorCallbackPrivate;
  186. /*
  187. * struct PRErrorCallbackTablePrivate --
  188. *
  189. * A data structure under which the localization plugin may store information,
  190. * associated with an error table, that is private to itself.
  191. */
  192. struct PRErrorCallbackTablePrivate;
  193. /*
  194. * PRErrorCallbackLookupFn --
  195. *
  196. * A function of PRErrorCallbackLookupFn type is a localization
  197. * plugin callback which converts an error code into a description
  198. * in the requested language. The callback is provided the
  199. * appropriate error table, private data for the plugin and the table.
  200. * The callback returns the appropriate UTF-8 encoded description, or NULL
  201. * if no description can be found.
  202. */
  203. typedef const char *
  204. PRErrorCallbackLookupFn(PRErrorCode code, PRLanguageCode language,
  205. const struct PRErrorTable *table,
  206. struct PRErrorCallbackPrivate *cb_private,
  207. struct PRErrorCallbackTablePrivate *table_private);
  208. /*
  209. * PRErrorCallbackNewTableFn --
  210. *
  211. * A function PRErrorCallbackNewTableFn type is a localization plugin
  212. * callback which is called once with each error table registered
  213. * with NSPR. The callback is provided with the error table and
  214. * the plugin's private structure. The callback returns any table private
  215. * data it wishes to associate with the error table. Does not need to be thread
  216. * safe.
  217. */
  218. typedef struct PRErrorCallbackTablePrivate *
  219. PRErrorCallbackNewTableFn(const struct PRErrorTable *table,
  220. struct PRErrorCallbackPrivate *cb_private);
  221. /**********************************************************************/
  222. /****************************** FUNCTIONS *****************************/
  223. /**********************************************************************/
  224. /***********************************************************************
  225. ** FUNCTION: PR_ErrorToString
  226. ** DESCRIPTION:
  227. ** Returns the UTF-8 message for an error code in
  228. ** the requested language. May return the message
  229. ** in the default language if a translation in the requested
  230. ** language is not available. The returned string is
  231. ** valid for the duration of the process. Never returns NULL.
  232. **
  233. ***********************************************************************/
  234. NSPR_API(const char *) PR_ErrorToString(PRErrorCode code,
  235. PRLanguageCode language);
  236. /***********************************************************************
  237. ** FUNCTION: PR_ErrorToName
  238. ** DESCRIPTION:
  239. ** Returns the macro name for an error code, or NULL
  240. ** if the error code is not known. The returned string is
  241. ** valid for the duration of the process.
  242. **
  243. ** Does not work for error table 0, the system error codes.
  244. **
  245. ***********************************************************************/
  246. NSPR_API(const char *) PR_ErrorToName(PRErrorCode code);
  247. /***********************************************************************
  248. ** FUNCTION: PR_ErrorLanguages
  249. ** DESCRIPTION:
  250. ** Returns the RFC 1766 language tags for the language
  251. ** codes PR_ErrorToString() supports. The returned array is valid
  252. ** for the duration of the process. Never returns NULL. The first
  253. ** item in the returned array is the language tag for PRLanguageCode 0,
  254. ** the second is for PRLanguageCode 1, and so on. The array is terminated
  255. ** with a null pointer.
  256. **
  257. ***********************************************************************/
  258. NSPR_API(const char * const *) PR_ErrorLanguages(void);
  259. /***********************************************************************
  260. ** FUNCTION: PR_ErrorInstallTable
  261. ** DESCRIPTION:
  262. ** Registers an error table with NSPR. Must be done exactly once per
  263. ** table. Memory pointed to by `table' must remain valid for the life
  264. ** of the process.
  265. **
  266. ** NOT THREAD SAFE!
  267. **
  268. ***********************************************************************/
  269. NSPR_API(PRErrorCode) PR_ErrorInstallTable(const struct PRErrorTable *table);
  270. /***********************************************************************
  271. ** FUNCTION: PR_ErrorInstallCallback
  272. ** DESCRIPTION:
  273. ** Registers an error localization plugin with NSPR. May be called
  274. ** at most one time. `languages' contains the language codes supported
  275. ** by this plugin. Languages 0 and 1 must be "i-default" and "en"
  276. ** respectively. `lookup' and `newtable' contain pointers to
  277. ** the plugin callback functions. `cb_private' contains any information
  278. ** private to the plugin functions.
  279. **
  280. ** NOT THREAD SAFE!
  281. **
  282. ***********************************************************************/
  283. NSPR_API(void) PR_ErrorInstallCallback(const char * const * languages,
  284. PRErrorCallbackLookupFn *lookup,
  285. PRErrorCallbackNewTableFn *newtable,
  286. struct PRErrorCallbackPrivate *cb_private);
  287. PR_END_EXTERN_C
  288. #endif /* prerror_h___ */