Back to index

lightning-sunbird  0.9+nobinonly
w16callb.c
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 the Netscape Portable Runtime (NSPR).
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-2000
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 /*
00039 ** w16callb.c -- Implement Win16 Callback functions
00040 **
00041 ** Functions here are to replace functions normally in
00042 ** LIBC which are not implemented in MSVC's LIBC.
00043 ** Some clients of NSPR expect to statically link
00044 ** to NSPR and get these functions.
00045 **
00046 ** Some are implemented as callbacks to the .EXE
00047 ** some are implemented directly in this module.
00048 **
00049 */
00050 
00051 #include "primpl.h"
00052 #include "windowsx.h"
00053 
00054 /*
00055 ** _pr_callback_funcs -- This is where clients register the 
00056 ** callback function structure.
00057 */
00058 struct PRMethodCallbackStr * _pr_callback_funcs;
00059 
00060 /*
00061 ** PR_MDInitWin16() -- Register the PRMethodCallback table pointer
00062 ** 
00063 */
00064 void PR_MDRegisterCallbacks(struct PRMethodCallbackStr *f)
00065 {
00066     _pr_callback_funcs = f;
00067 }
00068 
00069 /*
00070 ** NSPR re-implenentations of various C runtime functions:
00071 */
00072 
00073 /*
00074 ** PR_MD_printf() -- exported as printf()
00075 **
00076 */
00077 int  PR_MD_printf(const char *fmt, ...)
00078 {
00079     char buffer[1024];
00080     int ret = 0;
00081     va_list args;
00082 
00083     va_start(args, fmt);
00084 
00085 #ifdef DEBUG
00086     PR_vsnprintf(buffer, sizeof(buffer), fmt, args);
00087     {   
00088         if (_pr_callback_funcs != NULL && _pr_callback_funcs->auxOutput != NULL) {
00089             (* _pr_callback_funcs->auxOutput)(buffer);
00090         } else {
00091             OutputDebugString(buffer);
00092         }
00093     }
00094 #endif
00095 
00096     va_end(args);
00097     return ret;
00098 }
00099 
00100 /*
00101 ** PR_MD_sscanf() -- exported as sscanf()
00102 **
00103 */
00104 int  PR_MD_sscanf(const char *buf, const char *fmt, ...)
00105 {
00106        int           retval;
00107        va_list       arglist;
00108 
00109        va_start(arglist, fmt);
00110        retval = vsscanf((const unsigned char *)buf, (const unsigned char *)fmt, arglist);
00111        va_end(arglist);
00112        return retval;
00113 }
00114 
00115 /*
00116 ** PR_MD_strftime() -- exported as strftime
00117 **
00118 */
00119 size_t PR_MD_strftime(char *s, size_t len, const char *fmt, const struct tm *p) 
00120 {
00121     if( _pr_callback_funcs ) {
00122         return (*_pr_callback_funcs->strftime)(s, len, fmt, p);
00123     } else {
00124         PR_ASSERT(0);
00125         return 0;
00126     }
00127 }
00128 
00129 
00130 /*
00131 ** PR_MD_malloc() -- exported as malloc()
00132 **
00133 */
00134 void *PR_MD_malloc( size_t size )
00135 {
00136     if( _pr_callback_funcs ) {
00137         return (*_pr_callback_funcs->malloc)( size );
00138     } else {
00139         return GlobalAllocPtr(GPTR, (DWORD)size);
00140     }
00141 } /* end malloc() */
00142 
00143 /*
00144 ** PR_MD_calloc() -- exported as calloc()
00145 **
00146 */
00147 void *PR_MD_calloc( size_t n, size_t size )
00148 {
00149     void *p;
00150     size_t sz;
00151     
00152     if( _pr_callback_funcs ) {
00153         return (*_pr_callback_funcs->calloc)( n, size );
00154     } else {
00155         sz = n * size;
00156         p = GlobalAllocPtr(GPTR, (DWORD)sz );
00157         memset( p, 0x00, sz );
00158         return p;
00159     }
00160 } /* end calloc() */
00161 
00162 /*
00163 ** PR_MD_realloc() -- exported as realloc()
00164 **
00165 */
00166 void *PR_MD_realloc( void* old_blk, size_t size )
00167 {
00168     if( _pr_callback_funcs ) {
00169         return (*_pr_callback_funcs->realloc)( old_blk, size );
00170     } else {
00171         return GlobalReAllocPtr( old_blk, (DWORD)size, GPTR);
00172     }
00173 } /* end realloc */
00174 
00175 /*
00176 ** PR_MD_free() -- exported as free()
00177 **
00178 */
00179 void PR_MD_free( void *ptr )
00180 {
00181     if( _pr_callback_funcs ) {
00182         (*_pr_callback_funcs->free)( ptr );
00183         return;
00184     } else {
00185         GlobalFreePtr( ptr );
00186         return;
00187     }
00188 } /* end free() */
00189 
00190 /*
00191 ** PR_MD_getenv() -- exported as getenv()
00192 **
00193 */
00194 char *PR_MD_getenv( const char *name )
00195 {
00196     if( _pr_callback_funcs ) {
00197         return (*_pr_callback_funcs->getenv)( name );
00198     } else {
00199         return 0;
00200     }
00201 } /* end getenv() */
00202 
00203 
00204 /*
00205 ** PR_MD_perror() -- exported as perror()
00206 **
00207 ** well, not really (lth. 12/5/97).
00208 ** XXX hold this thought.
00209 **
00210 */
00211 void PR_MD_perror( const char *prefix )
00212 {
00213     return;
00214 } /* end perror() */
00215 
00216 /*
00217 ** PR_MD_putenv() -- exported as putenv()
00218 **
00219 */
00220 int  PR_MD_putenv(const char *assoc)
00221 {
00222     if( _pr_callback_funcs ) {
00223         return (*_pr_callback_funcs->putenv)(assoc);
00224     } else {
00225         PR_ASSERT(0);
00226         return NULL;
00227     }
00228 }
00229 
00230 /*
00231 ** PR_MD_fprintf() -- exported as fprintf()
00232 **
00233 */
00234 int  PR_MD_fprintf(FILE *fPtr, const char *fmt, ...)
00235 {
00236     char buffer[1024];
00237     va_list args;
00238 
00239     va_start(args, fmt);
00240     PR_vsnprintf(buffer, sizeof(buffer), fmt, args);
00241 
00242     if (fPtr == NULL) 
00243     {
00244         if (_pr_callback_funcs != NULL && _pr_callback_funcs->auxOutput != NULL) 
00245         {
00246             (* _pr_callback_funcs->auxOutput)(buffer);
00247         } 
00248         else 
00249         {
00250             OutputDebugString(buffer);
00251         }
00252     } 
00253     else 
00254     {
00255         fwrite(buffer, 1, strlen(buffer), fPtr); /* XXX Is this a sec. hole? */
00256     }
00257 
00258     va_end(args);
00259     return 0;
00260 }
00261 
00262 /* end w16callb.c */