Back to index

lightning-sunbird  0.9+nobinonly
stdio.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is mozilla.org code, released
00017  * Jan 28, 2003.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Netscape Communications Corporation.
00021  * Portions created by the Initial Developer are Copyright (C) 2003
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *   Garrett Arch Blythe, 28-January-2003
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either the GNU General Public License Version 2 or later (the "GPL"), or
00029  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 
00041 #include "mozce_internal.h"
00042 
00043 #include <stdarg.h>
00044 
00045 extern "C" {
00046 #if 0
00047 }
00048 #endif
00049 
00050 
00051 #define MAXFDS 100
00052 
00053 typedef struct _fdtab_block
00054 {
00055     int fd;
00056     FILE* file;
00057     
00058 } _fdtab_block;
00059 
00060 
00061 _fdtab_block _fdtab[MAXFDS];
00062 
00063 
00064 
00065 
00066 
00067 void
00068 _initfds()
00069 {
00070     static int fdsinitialized = 0;
00071     
00072     if(fdsinitialized)
00073         return;
00074     
00075     for (int i = 0; i < MAXFDS; i++)
00076         _fdtab[i].fd = -1;
00077     
00078     fdsinitialized = 1;
00079 }
00080 
00081 int
00082 _getnewfd()
00083 {
00084     int i;
00085     
00086     for(i = 0; i < MAXFDS; i++)
00087     {
00088         if(_fdtab[i].fd == -1)
00089             return i;
00090     }
00091     
00092     return -1;
00093 }
00094 
00095 
00096 
00097 
00098 MOZCE_SHUNT_API int mozce_access(const char *path, int mode)
00099 {
00100     MOZCE_PRECHECK
00101         
00102 #ifdef DEBUG
00103         mozce_printf("-- mozce_access called\n");
00104 #endif
00105     
00106     return 0;
00107 }
00108 
00109 MOZCE_SHUNT_API void mozce_rewind(FILE* inStream)
00110 {
00111     MOZCE_PRECHECK
00112         
00113 #ifdef DEBUG
00114         mozce_printf("mozce_rewind called\n");
00115 #endif
00116     
00117     fseek(inStream, 0, SEEK_SET);
00118 }
00119 
00120 
00121 MOZCE_SHUNT_API FILE* mozce_fdopen(int fd, const char* inMode)
00122 {
00123     MOZCE_PRECHECK
00124         
00125 #ifdef DEBUG
00126         mozce_printf("-- mozce_fdopen called (mode is ignored!) \n");
00127 #endif
00128     
00129     
00130     if(fd < 0 || fd >= MAXFDS || _fdtab[fd].fd == -1)
00131         return 0;
00132     
00133     return _fdtab[fd].file;
00134 }
00135 
00136 
00137 MOZCE_SHUNT_API void mozce_perror(const char* inString)
00138 {
00139     MOZCE_PRECHECK
00140         
00141 #ifdef DEBUG
00142         mozce_printf("mozce_perror called\n");
00143 #endif
00144     
00145     fprintf(stderr, "%s", inString);
00146 }
00147 
00148 
00149 MOZCE_SHUNT_API int mozce_remove(const char* inPath)
00150 {
00151     MOZCE_PRECHECK
00152         
00153 #ifdef DEBUG
00154         mozce_printf("mozce_remove called on %s\n", inPath);
00155 #endif
00156     
00157     int retval = -1;
00158     
00159     if(NULL != inPath)
00160     {
00161         unsigned short wPath[MAX_PATH];
00162         
00163         if(0 != a2w_buffer(inPath, -1, wPath, sizeof(wPath) / sizeof(unsigned short)))
00164         {
00165             if(FALSE != DeleteFileW(wPath))
00166             {
00167                 retval = 0;
00168                 
00169             }
00170         }
00171     }
00172     
00173     return retval;
00174 }
00175 
00176 MOZCE_SHUNT_API char* mozce_getcwd(char* buff, size_t size)
00177 {
00178     MOZCE_PRECHECK
00179         
00180 #ifdef DEBUG
00181         mozce_printf("mozce_getcwd called.\n");
00182 #endif
00183     int i;
00184     unsigned short dir[MAX_PATH];
00185     GetModuleFileName(GetModuleHandle (NULL), dir, MAX_PATH);
00186     for (i = _tcslen(dir); i && dir[i] != TEXT('\\'); i--) {}
00187     dir[i + 1] = TCHAR('\0');
00188     
00189     w2a_buffer(dir, -1, buff, size);
00190     
00191     return buff;
00192 }
00193 
00194 MOZCE_SHUNT_API int mozce_printf(const char * format, ...)
00195 {
00196 #ifdef DEBUG
00197 #define MAX_CHARS_IN_VARIABLE_STRING 1024
00198     
00199     char buf[MAX_CHARS_IN_VARIABLE_STRING];
00200     
00201     TCHAR tBuf[MAX_CHARS_IN_VARIABLE_STRING];
00202     
00203     va_list ptr;
00204     va_start(ptr,format);
00205     vsprintf(buf,format,ptr);
00206     
00207     mbstowcs(tBuf, buf, MAX_CHARS_IN_VARIABLE_STRING);
00208     
00209     OutputDebugString(tBuf);
00210     
00211     return 1;
00212 #endif
00213     return 0;
00214 }
00215 
00216 static void mode2binstr(int mode, char* buffer)
00217 {
00218     if (mode | O_RDWR || (mode | O_WRONLY))  // write only == read|write
00219     {
00220         if (mode | O_CREAT)
00221         {
00222             strcpy(buffer, "wb+");
00223         }
00224         else if (mode | O_APPEND)
00225         {
00226             strcpy(buffer, "ab+");
00227         }
00228         else if (mode | O_TRUNC)
00229         {
00230             strcpy(buffer, "wb+");
00231         }
00232         
00233         else if (mode == O_RDWR)
00234         {
00235             strcpy(buffer, "rb+");
00236         }
00237     }
00238     else if (mode | O_RDONLY)
00239     {
00240         strcpy(buffer, "rb");
00241     }
00242 }
00243 
00244 MOZCE_SHUNT_API int mozce_open(const char *pathname, int flags, int mode)
00245 {
00246     MOZCE_PRECHECK
00247         
00248 #ifdef DEBUG
00249         mozce_printf("mozce_open called\n");
00250 #endif
00251     
00252     _initfds();
00253     
00254     
00255     char modestr[10];
00256     *modestr = '\0';
00257     
00258     mode2binstr(mode, modestr);
00259     if (*modestr == '\0')
00260         return -1;
00261     
00262     
00263     FILE* file = fopen(pathname, modestr);
00264     
00265     int fd = -1;
00266     
00267     if (file)
00268     {
00269         fd = _getnewfd();
00270         
00271         _fdtab[fd].fd = fd;
00272         _fdtab[fd].file = file;
00273         
00274         fflush(file);
00275         fread(NULL, 0, 0, file);
00276     }
00277     
00278     return fd;
00279 }
00280 
00281 
00282 MOZCE_SHUNT_API int mozce_close(int fd)
00283 {
00284     MOZCE_PRECHECK
00285         
00286 #ifdef DEBUG
00287         mozce_printf("mozce_close called\n");
00288 #endif
00289     
00290     
00291     
00292     
00293     if(fd < 0 || fd >= MAXFDS || _fdtab[fd].fd == -1)
00294         return -1;
00295     
00296     
00297     fclose(_fdtab[fd].file);
00298     _fdtab[fd].fd = -1;
00299     
00300     return 0;
00301 }
00302 
00303 MOZCE_SHUNT_API size_t mozce_read(int fd, void* buffer, size_t count)
00304 {
00305     MOZCE_PRECHECK
00306         
00307 #ifdef DEBUG
00308         mozce_printf("mozce_read called\n");
00309 #endif
00310     
00311     if(fd < 0 || fd >= MAXFDS || _fdtab[fd].fd == -1)
00312         return -1;
00313     
00314     size_t num = fread(buffer, 1, count, _fdtab[fd].file);
00315     
00316     if (ferror(_fdtab[fd].file))
00317         return -1;
00318     
00319     return num;
00320 }
00321 
00322 
00323 MOZCE_SHUNT_API size_t mozce_write(int fd, const void* buffer, size_t count)
00324 {
00325     MOZCE_PRECHECK
00326         
00327 #ifdef DEBUG
00328         mozce_printf("mozce_write called\n");
00329 #endif
00330     
00331     if(fd < 0 || fd >= MAXFDS || _fdtab[fd].fd == -1)
00332         return -1;
00333     
00334     size_t num = fwrite(buffer, 1, count, _fdtab[fd].file);
00335     if (ferror(_fdtab[fd].file))
00336         return -1;
00337     
00338     return num;
00339 }
00340 
00341 
00342 MOZCE_SHUNT_API int mozce_unlink(const char *pathname)
00343 {
00344     MOZCE_PRECHECK
00345         
00346 #ifdef DEBUG
00347         mozce_printf("mozce_unlink called\n");
00348 #endif
00349     return mozce_remove(pathname);
00350 }
00351 
00352 
00353 MOZCE_SHUNT_API int mozce_lseek(int fd, int offset, int whence)
00354 {
00355     MOZCE_PRECHECK
00356         
00357 #ifdef DEBUG
00358         mozce_printf("mozce_lseek called\n");
00359 #endif
00360     
00361     
00362     if(fd < 0 || fd >= MAXFDS || _fdtab[fd].fd == -1)
00363         return -1;
00364     
00365     int newpos = -1;
00366     int error = fseek(_fdtab[fd].file, offset, whence);
00367     
00368     if (!error)
00369         newpos = ftell(_fdtab[fd].file);
00370     return newpos;
00371 }
00372 
00373 
00374 
00375 MOZCE_SHUNT_API int mozce_fileno(FILE* f)
00376 {
00377     MOZCE_PRECHECK
00378         
00379 #ifdef DEBUG
00380         mozce_printf("mozce_fileno called\n");
00381 #endif
00382     
00383     // Windows SDK is broken.  _fileno should return an int, but for whatever god forsaken reason, CE returns void*.
00384     int fd;
00385     
00386     for(fd = 0; fd < MAXFDS; fd++)
00387     {
00388         if(_fdtab[fd].file == f)
00389         {
00390             return _fdtab[fd].fd;
00391         }
00392     }
00393     return (int) fileno(f);
00394     
00395 }
00396 
00397 
00398 #if 0
00399 {
00400 #endif
00401 } /* extern "C" */
00402