home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / sys / mac / programm / 13982 < prev    next >
Encoding:
Text File  |  1992-08-14  |  2.7 KB  |  120 lines

  1. Newsgroups: comp.sys.mac.programmer
  2. Path: sparky!uunet!mcsun!ub4b!info-sparc1.info.ucl.ac.be!NewsWatcher
  3. From: Meessen@slig.ucl.ac.be  (Christophe Meessen)
  4. Subject: Re: annoying pascal strings
  5. Message-ID: <Meessen-140892134803@130.104.58.11>
  6. Followup-To: comp.sys.mac.programmer
  7. Sender: news@info.ucl.ac.be (News Administrator)
  8. Nntp-Posting-Host: 130.104.58.11
  9. Organization: Universite Catholique de Louvain (Belgium)
  10. References: <1992Aug10.132907.23439@das.harvard.edu> <zben-100892215906@zben-mac-ii.umd.edu> <1992Aug13.174942.18261@kronos.arc.nasa.gov>
  11. Date: Fri, 14 Aug 1992 12:21:49 GMT
  12. Lines: 106
  13.  
  14. In article <1992Aug13.174942.18261@kronos.arc.nasa.gov>,
  15. joshr@kronos.arc.nasa.gov (Joshua Rabinowitz-Summer-91) wrote:
  16. > Some common C functions: these modify the string in place.
  17. > ------------
  18. > here is header
  19. > char * CToPStr(char *aCStr);
  20. > char * PToCStr(char *aPStr);
  21. > =
  22. > ------------
  23. > here is source
  24. > ------------
  25. > #include "StrFuncs.h"
  26. > #include <string.h>
  27. > #include <Global.h>
  28. > char * CToPStr(char *aCStr)
  29. > {
  30. >    long i, len;
  31. >    len = strlen(aCStr);
  32. >    for (i=len; i>0; i--) 
  33. >       aCStr[i] = aCStr[i-1];
  34. >       
  35. >    aCStr[0] = Min(255, len);
  36. >    return aCStr;
  37. > }
  38.  
  39. Using pointers is faster.
  40.  
  41. unsigned char * CToPStr( unsigned char *aCStr ) {
  42.     unsigned char i, *srcPtr, *dstPtr;
  43.     
  44.     // scan the line until meets a '\0' or reach PStr limit
  45.     for( i = 0, srcPtr = aCStr; (*srcPtr != '\0') && (i < 255 ); i++ )
  46.                                 srcPtr++;  
  47.  
  48.     // shift the string one char
  49.     dstPtr = srcPtr--;
  50.     while( srcPtr >= aCStr )
  51.        *dstPtr-- = *srcPtr--;
  52.     
  53.     // store the string size
  54.     *aCStr = i;
  55. }
  56.  
  57. Using Apple toolbox routines it becomes:
  58.  
  59. unsigned char * CToPStr( unsigned char *aCStr ) {
  60.     unsigned char i, *dstPtr;
  61.     
  62.     // scan the line until meets a '\0' or reach PString limit
  63.     for( i = 0, dstPtr = aCStr; (*dstPtr != '\0') && (i < 255 ); i++ )
  64.                                 dstPtr++;  
  65.  
  66.     // shift the string one char
  67.     BlockMove( aCStr, aCStr+1, i );
  68.     
  69.     // store the string size
  70.     *aCStr = i;
  71. }
  72.  
  73.  
  74. > char *PToCStr(char *aPStr)
  75. > {
  76. >    long i, len;
  77. >    len = *aPStr;
  78. >    for (i=0; i<len; i++) 
  79. >       aPStr[i] = aPStr[i+1];
  80. >    aPStr[len] = '\0';
  81. >    return aPStr;
  82. > }
  83.  
  84.  
  85. here it's simpler:
  86.  
  87. unsigned char * PToCStr( char * aPStr ) {
  88.    unsigned char i, srcPtr, dstPtr;
  89.  
  90.    srcPtr++ = dstPtr = aPStr;
  91.    for( i = *aPStr; i ; i-- )
  92.        *dstPtr++ = *srcPtr++;
  93.    *dstPtr = "\0";
  94. }
  95.  
  96. or using the ToolBox
  97.  
  98. unsigned char * PToCStr( char * aPStr ) {
  99.    unsigned char i, srcPtr, dstPtr;
  100.     
  101.             i = *aPStr;
  102.    BlockMove( aPStr, aPstr + 1, (long)*aPStr );
  103.    aPStr + i = i;
  104. }
  105.  
  106.  
  107.  
  108. Bien cordialement,
  109.  
  110.                            Christophe MEESSEN
  111.