Back to index

lightning-sunbird  0.9+nobinonly
strconv.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; 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.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "xp.h"
00039 
00040 #include "profile.h"
00041 #include "strconv.h"
00042 
00043 static char szDefaultString[] = PROFILE_DEFAULT_STRING;
00044 
00045 static BOOL isTrueNumericValue(LPSTR szString)
00046 {
00047   char szPrefix[] = PROFILE_NUMERIC_PREFIX;
00048   return (strncmp(szString, szPrefix, strlen(szPrefix)) == 0);
00049 }
00050 
00051 static LPSTR convertStringToLPSTR(LPSTR szString)
00052 {
00053   if(szString == NULL)
00054     return NULL;
00055 
00056   if(isTrueNumericValue(szString))
00057   {
00058     char szPrefix[] = PROFILE_NUMERIC_PREFIX;
00059     return (LPSTR)atol(szString + strlen(szPrefix));
00060   }
00061   else
00062     return szString;
00063 }
00064 
00065 DWORD convertStringToLPSTR1(DWORD * pdw1)
00066 {
00067   if(pdw1 == NULL)
00068     return 0L;
00069 
00070   DWORD dwRet = (isTrueNumericValue((LPSTR)(*pdw1)) ? (fTNV1) : 0);
00071 
00072   *pdw1 = (DWORD)convertStringToLPSTR((LPSTR)(*pdw1));
00073 
00074   return dwRet;
00075 }
00076 
00077 DWORD convertStringToLPSTR2(DWORD * pdw1, DWORD * pdw2)
00078 {
00079   if((pdw1 == NULL) || (pdw2 == NULL))
00080     return 0L;
00081 
00082   DWORD dwRet =   (isTrueNumericValue((LPSTR)(*pdw1)) ? (fTNV1) : 0)
00083                 | (isTrueNumericValue((LPSTR)(*pdw2)) ? (fTNV2) : 0);
00084   
00085   *pdw1 = (DWORD)convertStringToLPSTR((LPSTR)(*pdw1));
00086   *pdw2 = (DWORD)convertStringToLPSTR((LPSTR)(*pdw2));
00087 
00088   return dwRet;
00089 }
00090 
00091 DWORD convertStringToLPSTR3(DWORD * pdw1, DWORD * pdw2, DWORD * pdw3)
00092 {
00093   if((pdw1 == NULL) || (pdw2 == NULL) || (pdw3 == NULL))
00094     return 0L;
00095 
00096   DWORD dwRet =   (isTrueNumericValue((LPSTR)(*pdw1)) ? (fTNV1) : 0)
00097                 | (isTrueNumericValue((LPSTR)(*pdw2)) ? (fTNV2) : 0)
00098                 | (isTrueNumericValue((LPSTR)(*pdw3)) ? (fTNV3) : 0);
00099 
00100   *pdw1 = (DWORD)convertStringToLPSTR((LPSTR)(*pdw1));
00101   *pdw2 = (DWORD)convertStringToLPSTR((LPSTR)(*pdw2));
00102   *pdw3 = (DWORD)convertStringToLPSTR((LPSTR)(*pdw3));
00103 
00104   return dwRet;
00105 }
00106 
00107 // converts numeric value represented by string to DWORD
00108 static DWORD convertStringToDWORD(LPSTR szString)
00109 {
00110   if(szString == NULL)
00111     return 0L;
00112 
00113   if(strcmp(szString, szDefaultString) == 0)
00114     return DEFAULT_DWARG_VALUE;
00115   else
00116     return (DWORD)atol(szString);
00117 }
00118 
00119 void convertStringToDWORD1(DWORD * pdw1)
00120 {
00121   if(pdw1 == NULL)
00122     return;
00123   *pdw1 = convertStringToDWORD((LPSTR)(*pdw1));
00124 }
00125 
00126 void convertStringToDWORD2(DWORD * pdw1, DWORD * pdw2)
00127 {
00128   if((pdw1 == NULL) || (pdw2 == NULL))
00129     return;
00130   *pdw1 = convertStringToDWORD((LPSTR)(*pdw1));
00131   *pdw2 = convertStringToDWORD((LPSTR)(*pdw2));
00132 }
00133 
00134 void convertStringToDWORD3(DWORD * pdw1, DWORD * pdw2, DWORD * pdw3)
00135 {
00136   if((pdw1 == NULL) || (pdw2 == NULL) || (pdw3 == NULL))
00137     return;
00138   *pdw1 = convertStringToDWORD((LPSTR)(*pdw1));
00139   *pdw2 = convertStringToDWORD((LPSTR)(*pdw2));
00140   *pdw3 = convertStringToDWORD((LPSTR)(*pdw3));
00141 }
00142 
00143 void convertStringToDWORD4(DWORD * pdw1, DWORD * pdw2, DWORD * pdw3, DWORD * pdw4)
00144 {
00145   if((pdw1 == NULL) || (pdw2 == NULL) || (pdw3 == NULL) || (pdw4 == NULL))
00146     return;
00147 
00148   *pdw1 = convertStringToDWORD((LPSTR)(*pdw1));
00149   *pdw2 = convertStringToDWORD((LPSTR)(*pdw2));
00150   *pdw3 = convertStringToDWORD((LPSTR)(*pdw3));
00151   *pdw4 = convertStringToDWORD((LPSTR)(*pdw4));
00152 }
00153 
00154 static NPBool convertStringToBOOL(LPSTR szString)
00155 {
00156   if(szString == NULL)
00157     return (NPBool)NULL;
00158 
00159   if(isTrueNumericValue(szString))
00160   {
00161     char szPrefix[] = PROFILE_NUMERIC_PREFIX;
00162     return (NPBool)atol(szString + strlen(szPrefix));
00163   }
00164   else
00165   {
00166     NPBool npb = (stricmp(szString, "TRUE") == 0) ? TRUE : FALSE;
00167     return npb;
00168   }
00169 }
00170 
00171 DWORD convertStringToBOOL1(DWORD * pdw1)
00172 {
00173   if(pdw1 == NULL)
00174     return 0L;
00175 
00176   DWORD dwRet = (isTrueNumericValue((LPSTR)(*pdw1)) ? (fTNV1) : 0);
00177 
00178   *pdw1 = (DWORD)convertStringToBOOL((LPSTR)(*pdw1));
00179 
00180   return dwRet;
00181 }
00182 
00183 static NPReason convertStringToNPReason(LPSTR szString)
00184 {
00185   if(szString == NULL)
00186     return NPRES_DONE;
00187 
00188   if(isTrueNumericValue(szString))
00189   {
00190     char szPrefix[] = PROFILE_NUMERIC_PREFIX;
00191     return (NPReason)atol(szString + strlen(szPrefix));
00192   }
00193   else
00194   {
00195     if(stricmp(ENTRY_NPRES_DONE, szString) == 0)
00196       return NPRES_DONE;
00197     else if(stricmp(ENTRY_NPRES_NETWORK_ERR, szString) == 0)
00198       return NPRES_NETWORK_ERR;
00199     else if(stricmp(ENTRY_NPRES_USER_BREAK, szString) == 0)
00200       return NPRES_USER_BREAK;
00201     else
00202       return NPRES_DONE;
00203   }
00204 }
00205 
00206 DWORD convertStringToNPReason1(DWORD * pdw1)
00207 {
00208   if(pdw1 == NULL)
00209     return 0L;
00210 
00211   *pdw1 = (DWORD)convertStringToNPReason((LPSTR)(*pdw1));
00212 
00213   return 0L;
00214 }
00215 
00216 static NPNVariable convertStringToNPNVariable(LPSTR szString)
00217 {
00218   if(szString == NULL)
00219     return (NPNVariable)0;
00220 
00221   if(isTrueNumericValue(szString))
00222   {
00223     char szPrefix[] = PROFILE_NUMERIC_PREFIX;
00224     return (NPNVariable)atol(szString + strlen(szPrefix));
00225   }
00226   else
00227   {
00228     if(stricmp(ENTRY_NPNVXDISPLAY, szString) == 0)
00229       return NPNVxDisplay;
00230     else if(stricmp(ENTRY_NPNVXTAPPCONTEXT, szString) == 0)
00231       return NPNVxtAppContext;
00232     else if(stricmp(ENTRY_NPNVNETSCAPEWINDOW, szString) == 0)
00233       return NPNVnetscapeWindow;
00234     else if(stricmp(ENTRY_NPNVJAVASCRIPTENABLEDBOOL, szString) == 0)
00235       return NPNVjavascriptEnabledBool;
00236     else if(stricmp(ENTRY_NPNVASDENABLEDBOOL, szString) == 0)
00237       return NPNVasdEnabledBool;
00238     else if(stricmp(ENTRY_NPNVISOFFLINEBOOL, szString) == 0)
00239       return NPNVisOfflineBool;
00240     else
00241       return (NPNVariable)0;
00242   }
00243 }
00244 
00245 DWORD convertStringToNPNVariable1(DWORD * pdw1)
00246 {
00247   if(pdw1 == NULL)
00248     return 0L;
00249 
00250   DWORD dwRet = (isTrueNumericValue((LPSTR)(*pdw1)) ? (fTNV1) : 0);
00251   
00252   *pdw1 = (DWORD)convertStringToNPNVariable((LPSTR)(*pdw1));
00253 
00254   return dwRet;
00255 }
00256 
00257 static NPPVariable convertStringToNPPVariable(LPSTR szString)
00258 {
00259   if(szString == NULL)
00260     return (NPPVariable)0;
00261 
00262   if(isTrueNumericValue(szString))
00263   {
00264     char szPrefix[] = PROFILE_NUMERIC_PREFIX;
00265     return (NPPVariable)atol(szString + strlen(szPrefix));
00266   }
00267   else
00268   {
00269     if(stricmp(ENTRY_NPPVPLUGINNAMESTRING, szString) == 0)
00270       return NPPVpluginNameString;
00271     else if(stricmp(ENTRY_NPPVPLUGINDESCRIPTIONSTRING, szString) == 0)
00272       return NPPVpluginDescriptionString;
00273     else if(stricmp(ENTRY_NPPVPLUGINWINDOWBOOL, szString) == 0)
00274       return NPPVpluginWindowBool;
00275     else if(stricmp(ENTRY_NPPVPLUGINTRANSPARENTBOOL, szString) == 0)
00276       return NPPVpluginTransparentBool;
00277     else if(stricmp(ENTRY_NPPVPLUGINKEEPLIBRARYINMEMORY, szString) == 0)
00278       return NPPVpluginKeepLibraryInMemory;
00279     else if(stricmp(ENTRY_NPPVPLUGINWINDOWSIZE, szString) == 0)
00280       return NPPVpluginWindowSize;
00281     else
00282       return (NPPVariable)0;
00283   }
00284 }
00285 
00286 DWORD convertStringToNPPVariable1(DWORD * pdw1)
00287 {
00288   if(pdw1 == NULL)
00289     return 0L;
00290 
00291   DWORD dwRet = (isTrueNumericValue((LPSTR)(*pdw1)) ? (fTNV1) : 0);
00292 
00293   *pdw1 = (DWORD)convertStringToNPPVariable((LPSTR)(*pdw1));
00294 
00295   return dwRet;
00296 }
00297 
00298 NPByteRange * convertStringToNPByteRangeList(LPSTR szString)
00299 {
00300   NPByteRange **brNextFromPrev, *brList = 0;
00301   if(szString) {
00302     int offset = -1, len = -1;
00303     char *p = szString;
00304     while (EOF != sscanf((const char*)p, "%d-%d", &offset, &len)) {
00305       if (offset == -1 || len == -1)
00306         break;
00307       NPByteRange *brCurr = new NPByteRange;
00308       brCurr->offset = offset;
00309       brCurr->length = len;
00310       brCurr->next = 0;
00311       if (!brList)
00312         brList = brCurr;
00313       else
00314         *brNextFromPrev = brCurr;
00315       
00316       brNextFromPrev = &brCurr->next;
00317       
00318       if (!(p = strchr(p, ',')))
00319         break;
00320       while(*(++p) == ' '); // cut off white spaces
00321     } 
00322   }
00323   return brList;
00324 }