home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gppdem32.zip / gppDemid32 / gppdemid32.c < prev    next >
Text File  |  1996-08-05  |  4KB  |  160 lines

  1. /* gppdemid.c
  2.    Copyright 1993-1994 Eberhard Mattes
  3.    Copyright 1996      Axel Zeuner
  4.  
  5. This file is part of the GPPDEMID library.
  6. GPPDEMID is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Library General Public
  8. License as published by the Free Software Foundation; either
  9. version 2 of the License, or (at your option) any later version.
  10.  
  11. GPPDEMID is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14. Library General Public License for more details.
  15.  
  16. You should have received a copy of the GNU Library General Public
  17. License along with libiberty; see the file COPYING.LIB.  If
  18. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  19. Cambridge, MA 02139, USA.  */
  20.  
  21.  
  22. /* Note: This is 32-bit code! */
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <stdarg.h>
  27. #include <string.h>
  28. #define INCL_DOSMEMMGR
  29. #define INCL_DOSPROCESS
  30. #define INCL_DOSERRORS
  31. #include <os2.h>
  32. #include "demangle.h"
  33. #include "tools.h"
  34.  
  35.  
  36.  
  37. // __export _DLL_InitTerm
  38. APIRET _System _DLL_InitTerm(ULONG hModule, ULONG ulFlag)
  39. {
  40.    ULONG ulAction;
  41. switch (ulFlag) {
  42.    case 0:  // Init
  43.       return (1);
  44.    case 1:  // Exit
  45.       SysDestroyHeap();
  46.       return (1);
  47. }
  48. return (0);
  49. }
  50.  
  51. /* These functions are imported by cplus-de.c.  Don't do error
  52.    checking.  setjmp() and longjmp() could be used to make DEMANGLEID
  53.    return 0 if out of memory.  Note: allocate one more byte and change
  54.    the offset to 1 to work around a bug in string_prependn. */
  55.  
  56. char *xmalloc (size_t n)
  57. {
  58.    char* x= SysAlloc(n+1);
  59. if (x) return x+1;
  60. return x;
  61. }
  62.  
  63.  
  64. char *xrealloc (char *p, size_t n)
  65. {
  66.    unsigned long* h= (unsigned long*)(p-1);
  67.    unsigned char* w;
  68. w= SysAlloc(n+1);
  69. if (w) {
  70.    if (*(h-1)>n+1) {
  71.       memcpy(w,p-1,n+1);
  72.    } else {
  73.       memcpy(w,p-1,*(h-1));
  74.    } /* endif */
  75.    SysFree((char*)h);
  76. }
  77. return w+1;
  78. }
  79.  
  80.  
  81. void free( void* p)
  82. {
  83. SysFree(((char*)p)-1);
  84. }
  85.  
  86.  
  87. /* cplus-de.c uses only %d. */
  88.  
  89. int sprintf (char *dst, const char *fmt, ...)
  90. {
  91.   int i;
  92.   char *d;
  93.   va_list arg_ptr;
  94.  
  95.   d = dst;
  96.   va_start (arg_ptr, fmt);
  97.   while (*fmt != 0)
  98.     if (*fmt != '%')
  99.       *d++ = *fmt++;
  100.     else if (fmt[1] == '%')
  101.       *d++ = '%', fmt += 2;
  102.     else
  103.       {
  104.         ++fmt;
  105.         if (*fmt == 0)
  106.           break;
  107.         switch (*fmt)
  108.           {
  109.           case 'd':
  110.             i = va_arg (arg_ptr, int);
  111.             d += strlen (_itoa (i, d, 10));
  112.             break;
  113.           default:
  114.             *d++= '%';
  115.             *d++= *fmt;
  116.             break;
  117.           }
  118.         ++fmt;
  119.       }
  120.   *d = 0;
  121.   va_end (arg_ptr);
  122.   return (d - dst);
  123. }
  124.  
  125. void abort()
  126. {
  127. DosExit(EXIT_PROCESS,17);
  128. }
  129.  
  130. /* Initialization. */
  131.  
  132. unsigned long InitDemangleID32 (const char *init)
  133. {
  134.   return 1;                     /* Success */
  135. }
  136.  
  137. /* Demangler. */
  138.  
  139. unsigned long DemangleID32(const char *src, char *dst,
  140.                                   unsigned long dst_size)
  141. {
  142.   char *result;
  143.   char mangled[256];
  144.   size_t len;
  145.   len = ((const unsigned char *)src)[0];
  146.  
  147.   memcpy (mangled, src+1, len);
  148.   mangled[len] = 0;
  149.  
  150.   result = cplus_demangle (mangled, DMGL_PARAMS | DMGL_ANSI);
  151.   if (result == NULL)
  152.     return 0;                   /* Error */
  153.   len = strlen (result);
  154.   if ((unsigned long)len >= dst_size)
  155.     return 0;                   /* Error */
  156.   memcpy (dst, result, len + 1);
  157.   free (result);
  158.   return 1;                     /* Success */
  159. }
  160.