home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / src / md / windows / w16callb.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  5.3 KB  |  244 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  * 
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  * 
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /*
  20. ** w16callb.c -- Implement Win16 Callback functions
  21. **
  22. ** Functions here are to replace functions normally in
  23. ** LIBC which are not implemented in MSVC's LIBC.
  24. ** Some clients of NSPR expect to statically link
  25. ** to NSPR and get these functions.
  26. **
  27. ** Some are implemented as callbacks to the .EXE
  28. ** some are implemented directly in this module.
  29. **
  30. */
  31.  
  32. #include "primpl.h"
  33. #include "windowsx.h"
  34.  
  35. /*
  36. ** _pr_callback_funcs -- This is where clients register the 
  37. ** callback function structure.
  38. */
  39. struct PRMethodCallbackStr * _pr_callback_funcs;
  40.  
  41. /*
  42. ** PR_MDInitWin16() -- Register the PRMethodCallback table pointer
  43. ** 
  44. */
  45. void PR_MDRegisterCallbacks(struct PRMethodCallbackStr *f)
  46. {
  47.     _pr_callback_funcs = f;
  48. }
  49.  
  50. /*
  51. ** NSPR re-implenentations of various C runtime functions:
  52. */
  53.  
  54. /*
  55. ** PR_MD_printf() -- exported as printf()
  56. **
  57. */
  58. int  PR_MD_printf(const char *fmt, ...)
  59. {
  60.     char buffer[1024];
  61.     int ret = 0;
  62.     va_list args;
  63.  
  64.     va_start(args, fmt);
  65.  
  66. #ifdef DEBUG
  67.     PR_vsnprintf(buffer, sizeof(buffer), fmt, args);
  68.     {   
  69.         if (_pr_callback_funcs != NULL && _pr_callback_funcs->auxOutput != NULL) {
  70.             (* _pr_callback_funcs->auxOutput)(buffer);
  71.         } else {
  72.             OutputDebugString(buffer);
  73.         }
  74.     }
  75. #endif
  76.  
  77.     va_end(args);
  78.     return ret;
  79. }
  80.  
  81. /*
  82. ** PR_MD_sscanf() -- exported as sscanf()
  83. **
  84. */
  85. int  PR_MD_sscanf(const char *buf, const char *fmt, ...)
  86. {
  87.     int        retval;
  88.     va_list    arglist;
  89.  
  90.     va_start(arglist, fmt);
  91.     retval = vsscanf((const unsigned char *)buf, (const unsigned char *)fmt, arglist);
  92.     va_end(arglist);
  93.     return retval;
  94. }
  95.  
  96. /*
  97. ** PR_MD_strftime() -- exported as strftime
  98. **
  99. */
  100. size_t PR_MD_strftime(char *s, size_t len, const char *fmt, const struct tm *p) 
  101. {
  102.     if( _pr_callback_funcs ) {
  103.         return (*_pr_callback_funcs->strftime)(s, len, fmt, p);
  104.     } else {
  105.         PR_ASSERT(0);
  106.         return 0;
  107.     }
  108. }
  109.  
  110.  
  111. /*
  112. ** PR_MD_malloc() -- exported as malloc()
  113. **
  114. */
  115. void *PR_MD_malloc( size_t size )
  116. {
  117.     if( _pr_callback_funcs ) {
  118.         return (*_pr_callback_funcs->malloc)( size );
  119.     } else {
  120.         return GlobalAllocPtr(GPTR, (DWORD)size);
  121.     }
  122. } /* end malloc() */
  123.  
  124. /*
  125. ** PR_MD_calloc() -- exported as calloc()
  126. **
  127. */
  128. void *PR_MD_calloc( size_t n, size_t size )
  129. {
  130.     void *p;
  131.     size_t sz;
  132.     
  133.     if( _pr_callback_funcs ) {
  134.         return (*_pr_callback_funcs->calloc)( n, size );
  135.     } else {
  136.         sz = n * size;
  137.         p = GlobalAllocPtr(GPTR, (DWORD)sz );
  138.         memset( p, 0x00, sz );
  139.         return p;
  140.     }
  141. } /* end calloc() */
  142.  
  143. /*
  144. ** PR_MD_realloc() -- exported as realloc()
  145. **
  146. */
  147. void *PR_MD_realloc( void* old_blk, size_t size )
  148. {
  149.     if( _pr_callback_funcs ) {
  150.         return (*_pr_callback_funcs->realloc)( old_blk, size );
  151.     } else {
  152.         return GlobalReAllocPtr( old_blk, (DWORD)size, GPTR);
  153.     }
  154. } /* end realloc */
  155.  
  156. /*
  157. ** PR_MD_free() -- exported as free()
  158. **
  159. */
  160. void PR_MD_free( void *ptr )
  161. {
  162.     if( _pr_callback_funcs ) {
  163.         (*_pr_callback_funcs->free)( ptr );
  164.         return;
  165.     } else {
  166.         GlobalFreePtr( ptr );
  167.         return;
  168.     }
  169. } /* end free() */
  170.  
  171. /*
  172. ** PR_MD_getenv() -- exported as getenv()
  173. **
  174. */
  175. char *PR_MD_getenv( const char *name )
  176. {
  177.     if( _pr_callback_funcs ) {
  178.         return (*_pr_callback_funcs->getenv)( name );
  179.     } else {
  180.         return 0;
  181.     }
  182. } /* end getenv() */
  183.  
  184.  
  185. /*
  186. ** PR_MD_perror() -- exported as perror()
  187. **
  188. ** well, not really (lth. 12/5/97).
  189. ** XXX hold this thought.
  190. **
  191. */
  192. void PR_MD_perror( const char *prefix )
  193. {
  194.     return;
  195. } /* end perror() */
  196.  
  197. /*
  198. ** PR_MD_putenv() -- exported as putenv()
  199. **
  200. */
  201. int  PR_MD_putenv(const char *assoc)
  202. {
  203.     if( _pr_callback_funcs ) {
  204.         return (*_pr_callback_funcs->putenv)(assoc);
  205.     } else {
  206.         PR_ASSERT(0);
  207.         return NULL;
  208.     }
  209. }
  210.  
  211. /*
  212. ** PR_MD_fprintf() -- exported as fprintf()
  213. **
  214. */
  215. int  PR_MD_fprintf(FILE *fPtr, const char *fmt, ...)
  216. {
  217.     char buffer[1024];
  218.     va_list args;
  219.  
  220.     va_start(args, fmt);
  221.     PR_vsnprintf(buffer, sizeof(buffer), fmt, args);
  222.  
  223.     if (fPtr == NULL) 
  224.     {
  225.         if (_pr_callback_funcs != NULL && _pr_callback_funcs->auxOutput != NULL) 
  226.         {
  227.             (* _pr_callback_funcs->auxOutput)(buffer);
  228.         } 
  229.         else 
  230.         {
  231.             OutputDebugString(buffer);
  232.         }
  233.     } 
  234.     else 
  235.     {
  236.         fwrite(buffer, 0, strlen(buffer), fPtr); /* XXX Is this a sec. hole? */
  237.     }
  238.  
  239.     va_end(args);
  240.     return 0;
  241. }
  242.  
  243. /* end w16callb.c */
  244.