home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c160 / 1.ddi / SOURCE / P4MISC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-04  |  8.0 KB  |  324 lines

  1.  
  2. /* p4misc.c   (c)Copyright Sequiter Software Inc., 1987-1990. All rights reserved. */
  3.  
  4. #include  "p4misc.h"
  5. #include  "d4all.h"
  6.  
  7. #include  <stdio.h>
  8. #include  <string.h>
  9.  
  10. #ifdef DO_CHSIZE
  11.    #include <dos.h>
  12.    #include <io.h>
  13. #endif
  14.  
  15.  
  16. #ifdef NO_FILELENGTH
  17. #include  <sys/types.h>
  18. #include  <sys/stat.h>
  19.  
  20. long  filelength( int hand )
  21. {
  22.    struct stat   str_stat ;
  23.  
  24.    fstat( hand, &str_stat ) ;         
  25.  
  26.    return( (long) str_stat.st_size ) ;
  27. }
  28. #endif
  29.    
  30.  
  31. #ifdef NO_MEMMOVE
  32. void *memmove( void *dest, void *src, size_t count )
  33. {
  34.    if ( dest < src )
  35.       if ( (char *)dest + count  <= (char *) src )
  36.       {
  37.          memcpy( dest, src, count ) ;
  38.      return( src ) ;
  39.       }
  40.       else
  41.       {
  42.      /* Start at beginning of 'src' */
  43.      int  i ;
  44.      for ( i=0; i< count; i++ )
  45.         ((char *) dest)[i] =  ((char *)src)[i] ;
  46.       }
  47.  
  48.    if ( src < dest )
  49.       if ( (char *) src + count  <=  (char *) dest )
  50.       {
  51.          memcpy( dest, src, count ) ;
  52.      return( src ) ;
  53.       }
  54.       else
  55.       {
  56.      /* Start at end of 'src' */
  57.      for(;count!=0;)
  58.          {
  59.             --count ;
  60.         ((char *)dest)[count] =  ((char *) src)[count] ;
  61.          }
  62.       }
  63.  
  64.    return( src ) ;
  65. }
  66. #endif
  67.  
  68. #ifdef LANGUAGE
  69.    #define  NO_STRUPR
  70.    #define  NO_STRLWR
  71. #endif
  72.  
  73. char * u4upper( char *str )
  74. {
  75.    #ifdef NO_STRUPR
  76.       char *ptr ;
  77.    
  78.       ptr =  str ;
  79.    
  80.       while ( *ptr != '\000' )
  81.       {
  82.          if ( *ptr >= 'a'  &&  *ptr <= 'z' )
  83.         *ptr &=  0337 ;
  84.          #ifdef GERMAN
  85.             if ( *ptr >= '\201' )
  86.             {
  87.                switch( *ptr )
  88.                {
  89.                   case '\204':
  90.                      *ptr =  '\214' ;
  91.                      break ;
  92.                   case '\224':
  93.                      *ptr =  '\231' ;
  94.                      break ;
  95.                   case (char) '\201':
  96.                      *ptr =  '\232' ;
  97.                      break ;
  98.                }
  99.             }
  100.          #endif
  101.          ptr++ ;
  102.       }
  103.    
  104.       return( str ) ;
  105.    #else
  106.       return strupr( str ) ;
  107.    #endif
  108. }
  109.    
  110. char *u4lower( char *str )
  111. {
  112.    #ifdef NO_STRLWR
  113.       char *ptr ;
  114.    
  115.       ptr =  str ;
  116.    
  117.       while ( *ptr != '\000' )
  118.       {
  119.          if ( *ptr >= 'A' && *ptr <= 'Z' )
  120.         *ptr |=  040 ;
  121.             #ifdef GERMAN
  122.                if ( *ptr >= '\201' )
  123.                {
  124.                   switch( *ptr )
  125.                   {
  126.                      case '\214':
  127.                         *ptr =  '\204' ;
  128.                         break ;
  129.                      case '\231':
  130.                         *ptr =  '\224' ;
  131.                         break ;
  132.                      case '\232':
  133.                         *ptr =  '\201' ;
  134.                         break ;
  135.                   }
  136.                }
  137.             #endif
  138.          ptr++ ;
  139.       }
  140.    
  141.       return( str ) ;
  142.    #else
  143.       return strlwr( str ) ;
  144.    #endif
  145. }
  146.  
  147. int u4toupper( int i_chr )
  148. {
  149.    char str[2] ;
  150.    if ( i_chr >= 256 )  return i_chr ;
  151.    str[0] =  (char) i_chr ;
  152.    str[1] =  0 ;
  153.    u4upper( str ) ;
  154.    return (int) str[0] ;
  155. }
  156.  
  157. int u4tolower( int i_chr )
  158. {
  159.    char str[2] ;
  160.    if ( i_chr >= 256 )  return i_chr ;
  161.    str[0] =  (char) i_chr ;
  162.    str[1] =  0 ;
  163.    u4lower( str ) ;
  164.    return (int) str[0] ;
  165. }
  166.  
  167. int  u4remove( char *path )
  168. {
  169. #ifdef NO_REMOVE
  170.    char  buf[80] ;
  171.    memset( buf, 0, sizeof(buf) ) ;
  172.    strncpy( buf, path, sizeof(buf)-1 ) ;
  173.    u4lower( buf ) ;
  174.    return( unlink( buf ) ) ;
  175. #else
  176.    #ifdef UNIX
  177.       char  buf[80] ;
  178.       memset( buf, 0, sizeof(buf) ) ;
  179.       strncpy( buf, path, sizeof(buf)-1 ) ;
  180.       u4lower( buf ) ;
  181.       return( remove( buf ) ) ;
  182.    #else
  183.       return remove( path ) ;
  184.    #endif
  185. #endif
  186. }
  187.  
  188.  
  189. #ifdef NO_STRNICMP
  190. strnicmp( char *a, char *b, size_t n )
  191. {
  192.    unsigned char  a_char, b_char ;
  193.  
  194.    for ( ; *a != '\000'  &&  *b != '\000' && n != 0; a++, b++, n-- )
  195.    {
  196.       a_char =  (unsigned char) *a  & 0xDF ;
  197.       b_char =  (unsigned char) *b  & 0xDF ;
  198.  
  199.       if ( a_char < b_char )  return -1 ;
  200.       if ( a_char > b_char )  return  1 ;
  201.    }
  202.  
  203.    return 0 ;
  204. }
  205. #endif
  206.  
  207.  
  208. #ifdef DO_CHSIZE
  209.    #ifdef OS2
  210.       int  chsize( int  h, long sz ) 
  211.       {
  212.          extern unsigned far pascal DOSNEWSIZE( unsigned, unsigned long ) ;
  213.    
  214.          if ( DOSNEWSIZE( h, (unsigned long) sz) != 0 )  return -1 ;
  215.          return 0 ;
  216.       }
  217.    #else 
  218.       #ifdef IS_386
  219.          int  chsize( int  h, long sz ) 
  220.          {
  221.             union REGS in_regs, out_regs ;
  222.             long lrc ;
  223.    
  224.             lrc = lseek( h, sz, 0 ) ;
  225.             if ( lrc != sz )  return -1 ;
  226.    
  227.             in_regs.h.ah  = 0x40 ;
  228.             in_regs.x.ebx = h ;
  229.             in_regs.x.ecx = 0 ;
  230.             int386( 0x21, &in_regs, &out_regs ) ;
  231.             if ( out_regs.w.ax == 0 )  
  232.                return 0 ;
  233.             else
  234.                return -1 ;
  235.          }
  236.       #else
  237.          int  chsize( int  h, long sz ) 
  238.          {
  239.             union REGS in_regs, out_regs ;
  240.             long lrc ;
  241.    
  242.             lrc = lseek( h, sz, 0 ) ;
  243.             if ( lrc != sz )  return -1 ;
  244.    
  245.             in_regs.h.ah  = 0x40 ;
  246.             in_regs.x.bx = h ;
  247.             in_regs.x.cx = 0 ;
  248.             int86( 0x21, &in_regs, &out_regs ) ;
  249.             if ( out_regs.x.ax == 0 )  
  250.                return 0  ;
  251.             else
  252.                return -1 ;
  253.          }
  254.       #endif
  255.    #endif
  256. #endif
  257.  
  258. #ifdef LANGUAGE
  259.    /* This mapping is for German. */
  260.    int v4map[256] =
  261.    {
  262. /*     0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15 */
  263.      137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,
  264.  
  265. /*    16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 */
  266.      153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,
  267.  
  268. /*    32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47 */
  269.        0,114,129,120,121,106,119,131, 95, 96,107,104,109,105,108,115,
  270.  
  271. /*    48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63 */
  272.        1,  2,  3,  4,  5,  6,  7,  8,  9, 10,111,110,101,103,102,113,
  273.  
  274. /*    64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79 */
  275.      118, 11, 14, 15, 17, 18, 20, 21, 22, 23, 24, 25, 26, 27, 28, 30,
  276.  
  277. /*    80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95 */
  278.       32, 33, 34, 35, 36, 37, 39, 40, 41, 42, 43, 97,116, 98,132,134,
  279.  
  280. /*    96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111 */
  281.      130, 44, 50, 51, 53, 54, 59, 60, 61, 62, 67, 68, 69, 70, 71, 74,
  282.  
  283. /*   112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127 */
  284.       78, 79, 80, 82, 83, 85, 89, 90, 91, 92, 94, 99,117,100,133,169,
  285.  
  286. /*   128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143 */
  287.       16, 84, 55, 48, 45, 47, 49, 52, 57, 58, 56, 66, 65, 64, 12, 13,
  288.  
  289. /*   144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159 */
  290.       19,127,128, 77, 73, 76, 88, 87, 93, 31, 38,122,123,124,126,125,
  291.  
  292. /*   160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175 */
  293.       46, 63, 75, 86, 72, 29,135,136,112,170,171,172,173,174,175,176,
  294.  
  295. /*   176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191 */
  296.      177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,
  297.  
  298. /*   192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207 */
  299.      193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,
  300.  
  301. /*   208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223 */
  302.      209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,
  303.  
  304. /*   224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239 */
  305.      225, 81,226,227,228,229,230,231,232,233,234,235,236,237,238,239,
  306.  
  307. /*   240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255 */
  308.      240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,
  309.    } ;
  310.  
  311.    int  u4memcmp( unsigned char *a, unsigned char *b, size_t len )
  312.    {
  313.       int i ;
  314.  
  315.       for (i=0; i<len; i++)
  316.          if ( a[i] != b[i] )
  317.          {
  318.             if ( v4map[a[i]] < v4map[b[i]] )  return -1 ;
  319.             return 1 ;
  320.          }
  321.       return 0 ;
  322.    }
  323. #endif
  324.