prerror.h 11 KB

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