Back to index

lightning-sunbird  0.9+nobinonly
nsEncoderDecoderUtils.h
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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 character set conversion utility macros.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corp.
00019  * Portions created by the Initial Developer are Copyright (C) 2003
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *  Alec Flett <alecf@flett.org>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #ifndef nsEncoderDecoderUtils_h__
00040 #define nsEncoderDecoderUtils_h__
00041 
00042 #define NS_ERROR_UCONV_NOCONV \
00043   NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_UCONV, 0x01)
00044 
00045 #define NS_SUCCESS_USING_FALLBACK_LOCALE \
00046   NS_ERROR_GENERATE_SUCCESS(NS_ERROR_MODULE_UCONV, 0x02)
00047 
00048 #define NS_UNICODEDECODER_NAME "Charset Decoders"
00049 #define NS_UNICODEENCODER_NAME "Charset Encoders"
00050 
00051 #define NS_DATA_BUNDLE_CATEGORY     "uconv-charset-data"
00052 #define NS_TITLE_BUNDLE_CATEGORY    "uconv-charset-titles"
00053 
00054 struct nsConverterRegistryInfo {
00055   PRBool isEncoder;             // PR_TRUE = encoder, PR_FALSE = decoder
00056   const char *charset;
00057   nsCID cid;
00058 };
00059 
00060 #define NS_CONVERTER_REGISTRY_START \
00061   static const nsConverterRegistryInfo gConverterRegistryInfo[] = {
00062 
00063 #define NS_CONVERTER_REGISTRY_END \
00064   };
00065 
00066 
00067 #define NS_IMPL_NSUCONVERTERREGSELF                                     \
00068 static NS_IMETHODIMP                                                    \
00069 nsUConverterRegSelf(nsIComponentManager *aCompMgr,                      \
00070                     nsIFile *aPath,                                     \
00071                     const char* registryLocation,                       \
00072                     const char* componentType,                          \
00073                     const nsModuleComponentInfo *info)                  \
00074 {                                                                       \
00075   nsresult rv;                                                          \
00076   nsCOMPtr<nsICategoryManager> catman =                                 \
00077     do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);                  \
00078   if (NS_FAILED(rv)) return rv;                                         \
00079                                                                         \
00080   nsXPIDLCString previous;                                              \
00081   PRUint32 i;                                                           \
00082   for (i=0; i<sizeof(gConverterRegistryInfo)/sizeof(gConverterRegistryInfo[0]); i++) { \
00083     const nsConverterRegistryInfo* entry = &gConverterRegistryInfo[i];         \
00084     const char *category;                                               \
00085     const char *key;                                                    \
00086                                                                         \
00087     if (entry->isEncoder) {                                             \
00088       category = NS_UNICODEENCODER_NAME;                                \
00089     } else {                                                            \
00090       category = NS_UNICODEDECODER_NAME;                                \
00091     }                                                                   \
00092     key = entry->charset;                                               \
00093                                                                         \
00094     rv = catman->AddCategoryEntry(category, key, "",                    \
00095                                   PR_TRUE,                              \
00096                                   PR_TRUE,                              \
00097                                   getter_Copies(previous));             \
00098   }                                                                     \
00099   return rv;                                                            \
00100 } \
00101 static NS_IMETHODIMP \
00102 nsUConverterUnregSelf(nsIComponentManager *aCompMgr,                        \
00103                       nsIFile *aPath,                                       \
00104                       const char*,                                          \
00105                       const nsModuleComponentInfo *info)                    \
00106 { \
00107   nsresult rv;                                                          \
00108   nsCOMPtr<nsICategoryManager> catman =                                 \
00109   do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);                    \
00110   if (NS_FAILED(rv)) return rv;                                         \
00111                                                                         \
00112   nsXPIDLCString previous;                                              \
00113   PRUint32 i;                                                           \
00114   for (i=0; i<sizeof(gConverterRegistryInfo)/sizeof(gConverterRegistryInfo[0]); i++) { \
00115     const nsConverterRegistryInfo* entry = &gConverterRegistryInfo[i];         \
00116     const char *category;                                               \
00117     const char *key;                                                    \
00118                                                                         \
00119     if (entry->isEncoder) {                                             \
00120       category = NS_UNICODEDECODER_NAME;                                \
00121     } else {                                                            \
00122       category = NS_UNICODEENCODER_NAME;                                \
00123     }                                                                   \
00124     key = entry->charset;                                               \
00125                                                                         \
00126     char * value = entry->cid.ToString();                               \
00127                                                                         \
00128     rv = catman->DeleteCategoryEntry(category, key, PR_TRUE);           \
00129     CRTFREEIF(value);                                                   \
00130   }                                                                     \
00131   return rv;                                                            \
00132 }
00133 
00134 
00135 #define NS_UCONV_REG_UNREG_DECODER(_Charset, _CID)          \
00136   {                                                         \
00137     PR_FALSE,                                                \
00138     _Charset,                                               \
00139     _CID,                                                   \
00140   },
00141   
00142 #define NS_UCONV_REG_UNREG_ENCODER(_Charset, _CID)          \
00143   {                                                         \
00144     PR_TRUE,                                               \
00145     _Charset,                                               \
00146     _CID,                                                   \
00147   }, 
00148 
00149   // this needs to be written out per some odd cpp behavior that
00150   // I could not work around - the behavior is document in the cpp
00151   // info page however, so I'm not the only one to hit this!
00152 #define NS_UCONV_REG_UNREG(_Charset, _DecoderCID, _EncoderCID) \
00153   {                                                         \
00154     PR_FALSE,                                               \
00155     _Charset,                                               \
00156     _DecoderCID,                                            \
00157   },                                                        \
00158   {                                                         \
00159     PR_TRUE,                                                \
00160     _Charset,                                               \
00161     _EncoderCID,                                            \
00162   },
00163   
00164 #endif