Back to index

lightning-sunbird  0.9+nobinonly
xp.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 #ifndef XP_WIN
00041 #include "pplib.h"
00042 #endif // !XP_WIN
00043 
00044 /****************************************/
00045 /*                                      */
00046 /*            File utilities            */
00047 /*                                      */
00048 /****************************************/
00049 
00050 // checks file existence
00051 BOOL XP_IsFile(LPSTR szFileName)
00052 {
00053 #ifdef XP_WIN
00054   OFSTRUCT of;
00055   return (HFILE_ERROR != OpenFile(szFileName, &of, OF_EXIST));
00056 #endif
00057 #if defined(XP_UNIX) || defined(XP_OS2)
00058   struct stat s;
00059   return (stat(szFileName, &s) != -1);
00060 #endif
00061 #ifdef XP_MAC /*  HACK */
00062        return 1;
00063 #endif
00064 }
00065 
00066 void XP_DeleteFile(LPSTR szFileName)
00067 {
00068 #ifdef XP_WIN
00069   DeleteFile(szFileName);
00070   return;
00071 #endif // XP_WIN
00072 
00073 #if (defined XP_UNIX || defined XP_MAC || defined XP_OS2)
00074   remove(szFileName);
00075   return;
00076 #endif // XP_UNIX || XP_MAC || XP_OS2
00077 }
00078 
00079 XP_HFILE XP_CreateFile(LPSTR szFileName)
00080 {
00081 #ifdef XP_WIN
00082   OFSTRUCT of;
00083   HFILE hFile = OpenFile(szFileName, &of, OF_CREATE | OF_WRITE);
00084   return (hFile != HFILE_ERROR) ? hFile : NULL;
00085 #else
00086   return (XP_HFILE)fopen(szFileName, "w+");
00087 #endif // XP_WIN
00088 }
00089 
00090 XP_HFILE XP_OpenFile(LPSTR szFileName)
00091 {
00092 #ifdef XP_WIN
00093   OFSTRUCT of;
00094   HFILE hFile = OpenFile(szFileName, &of, OF_READ | OF_WRITE);
00095   return (hFile != HFILE_ERROR) ? hFile : NULL;
00096 #else
00097   return (XP_HFILE)fopen(szFileName, "r+");
00098 #endif // XP_WIN
00099 }
00100 
00101 void XP_CloseFile(XP_HFILE hFile)
00102 {
00103   if(hFile != NULL)
00104   {
00105 #ifdef XP_WIN
00106     CloseHandle((HANDLE)hFile);
00107     return;
00108 #endif // XP_WIN
00109 
00110 #if (defined UNIX || defined XP_MAC || defined XP_OS2)
00111     fclose(hFile);
00112     return;
00113 #endif // UNIX || XP_MAC || XP_OS2
00114   }
00115 }
00116 
00117 DWORD XP_WriteFile(XP_HFILE hFile, void * pBuf, int iSize)
00118 {
00119 #ifdef XP_WIN
00120   DWORD dwRet;
00121   WriteFile((HANDLE)hFile, pBuf, iSize, &dwRet, NULL);
00122   return dwRet;
00123 #endif // XP_WIN
00124 
00125 #if (defined XP_UNIX || defined XP_MAC || defined XP_OS2)
00126   return (DWORD)fwrite(pBuf, iSize, 1, hFile);
00127 #endif // XP_UNIX || XP_MAC || XP_OS2
00128 }
00129 
00130 void XP_FlushFileBuffers(XP_HFILE hFile)
00131 {
00132 #ifdef XP_WIN
00133   FlushFileBuffers((HANDLE)hFile);
00134   return;
00135 #endif // XP_WIN
00136 
00137 #if (defined XP_UNIX || defined XP_MAC || defined XP_OS2)
00138   fflush(hFile);
00139 #endif // XP_UNIX || XP_MAC || XP_OS2
00140 }
00141 
00142 /****************************************/
00143 /*                                      */
00144 /*       Private profile utilities      */
00145 /*                                      */
00146 /****************************************/
00147 
00148 DWORD XP_GetPrivateProfileString(LPSTR szSection, LPSTR szKey, LPSTR szDefault, LPSTR szString, DWORD dwSize, LPSTR szFileName)
00149 {
00150 #ifdef XP_WIN
00151   return GetPrivateProfileString(szSection, szKey, szDefault, szString, dwSize, szFileName);
00152 #else
00153   XP_HFILE hFile = XP_OpenFile(szFileName);
00154   if(hFile == NULL)
00155   {
00156     strcpy(szString, szDefault);
00157     int iRet = strlen(szString);
00158     return iRet;
00159   }
00160   DWORD dwRet = PP_GetString(szSection, szKey, szDefault, szString, dwSize, hFile);
00161   XP_CloseFile(hFile);
00162   return dwRet;
00163 #endif // XP_WIN
00164 }
00165 
00166 int XP_GetPrivateProfileInt(LPSTR szSection, LPSTR szKey, int iDefault, LPSTR szFileName)
00167 {
00168 #ifdef XP_WIN
00169   return GetPrivateProfileInt(szSection, szKey, iDefault, szFileName);
00170 #else
00171   static char szString[80];
00172   XP_GetPrivateProfileString(szSection, szKey, "", szString, sizeof(szString), szFileName);
00173   if(!*szString)
00174     return iDefault;
00175   int iRet = atoi(szString);
00176   return iRet;
00177 #endif // XP_WIN
00178 }
00179 
00180 BOOL XP_WritePrivateProfileString(LPSTR szSection, LPSTR szKey, LPSTR szString, LPSTR szFileName)
00181 {
00182 #ifdef XP_WIN
00183   return WritePrivateProfileString(szSection, szKey, szString, szFileName);
00184 #else
00185   XP_HFILE hFile = XP_OpenFile(szFileName);
00186   if(hFile == NULL)
00187   {
00188     hFile = XP_CreateFile(szFileName);
00189     if(hFile == NULL)
00190       return FALSE;
00191   }
00192   BOOL bRet = PP_WriteString(szSection, szKey, szString, hFile);
00193   XP_CloseFile(hFile);
00194   return bRet;
00195 #endif // XP_WIN
00196 }
00197 
00198 BOOL XP_WritePrivateProfileInt(LPSTR szSection, LPSTR szKey, int iValue, LPSTR szFileName)
00199 {
00200   char szString[80];
00201   _itoa(iValue, szString, 10);
00202   return XP_WritePrivateProfileString(szSection, szKey, szString, szFileName);
00203 }
00204 
00205 /****************************************/
00206 /*                                      */
00207 /*       Miscellaneous utilities        */
00208 /*                                      */
00209 /****************************************/
00210 
00211 // returns milliseconds passed since last reboot
00212 DWORD XP_GetTickCount()
00213 {
00214 #ifdef XP_WIN
00215   return GetTickCount();
00216 #else
00217   return (DWORD)0;
00218 #endif // XP_WIN
00219 }
00220 
00221 void XP_Sleep(DWORD dwSleepTime)
00222 {
00223   if((long)dwSleepTime <= 0L)
00224     return;
00225 
00226 #ifdef XP_WIN
00227   Sleep(dwSleepTime);
00228 #endif // XP_WIN
00229 }