Changeset 23


Ignore:
Timestamp:
Jan 16, 2001, 8:49:10 PM (25 years ago)
Author:
umoeller
Message:

Fixes for V0.9.7.

Location:
trunk
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/helpers/dosh.h

    r18 r23  
    290290                           ULONG ulAttr);
    291291
    292     APIRET doshReadTextFile(PSZ pszFile,
     292    APIRET doshLoadTextFile(const char *pcszFile,
    293293                            PSZ* ppszContent);
    294294
  • trunk/include/helpers/gpih.h

    r18 r23  
    7272                           BYTE bDivisor);
    7373
    74     #ifdef INCL_GPILOGCOLORTABLE
    75 
    76         /*
    77          *@@ gpihSwitchToRGB:
    78          *      this switches the given HPS into RGB mode.
    79          *      Requires INCL_GPILOGCOLORTABLE.
    80          */
    81 
    82         #define gpihSwitchToRGB(hps)                \
    83             GpiCreateLogColorTable(hps, 0, LCOLF_RGB, 0, 0, NULL);
    84 
    85     #endif
     74    BOOL gpihSwitchToRGB(HPS hps);
     75    typedef BOOL XWPENTRY GPIHSWITCHTORGB(HPS hps);
     76    typedef GPIHSWITCHTORGB *PGPIHSWITCHTORGB;
    8677
    8778    /* ******************************************************************
     
    10899                            ULONG ulWidth);
    109100
    110     VOID APIENTRY gpihDraw3DFrame(HPS hps,
     101    VOID XWPENTRY gpihDraw3DFrame(HPS hps,
    111102                                  PRECTL prcl,
    112103                                  USHORT usWidth,
    113104                                  LONG lColorLeft,
    114105                                  LONG lColorRight);
    115     typedef VOID APIENTRY GPIHDRAW3DFRAME(HPS hps,
     106    typedef VOID XWPENTRY GPIHDRAW3DFRAME(HPS hps,
    116107                                          PRECTL prcl,
    117108                                          USHORT usWidth,
  • trunk/include/helpers/stringh.h

    r21 r23  
    6363                const char *pcszInsert);
    6464
    65     ULONG strhrpl(PSZ *ppszBuf,
    66                   PULONG pulOfs,
    67                   const char *pcszSearch,
    68                   const char *pcszReplace);
     65    ULONG strhFindReplace(PSZ *ppszBuf,
     66                          PULONG pulOfs,
     67                          const char *pcszSearch,
     68                          const char *pcszReplace);
    6969
    7070    ULONG strhWords(PSZ psz);
     
    129129    PSZ strhFindNextLine(PSZ pszSearchIn, PULONG pulOffset);
    130130
    131     PSZ strhFindKey(const char *pcszSearchIn,
    132                     const char *pcszKey,
    133                     PBOOL pfIsAllUpperCase);
    134 
    135     PSZ strhGetParameter(const char *pcszSearchIn,
    136                          const char *pcszKey,
    137                          PSZ pszCopyTo,
    138                          ULONG cbCopyTo);
    139 
    140     PSZ strhSetParameter(PSZ* ppszBuf,
    141                          const char *pcszKey,
    142                          PSZ pszNewParam,
    143                          BOOL fRespectCase);
    144 
    145     BOOL strhDeleteLine(PSZ pszSearchIn, PSZ pszKey);
    146 
    147131    BOOL strhBeautifyTitle(PSZ psz);
    148132
  • trunk/include/helpers/xstring.h

    r21 r23  
    8282    typedef XSTRSET *PXSTRSET;
    8383
    84     ULONG XWPENTRY xstrcpy(PXSTRING pxstr, const char *pcszSource);
    85     typedef ULONG XWPENTRY XSTRCPY(PXSTRING pxstr, const char *pcszSource);
     84    ULONG XWPENTRY xstrcpy(PXSTRING pxstr, const char *pcszSource, ULONG ulSourceLength);
     85    typedef ULONG XWPENTRY XSTRCPY(PXSTRING pxstr, const char *pcszSource, ULONG ulSourceLength);
    8686    typedef XSTRCPY *PXSTRCPY;
    8787
    88     ULONG XWPENTRY xstrcat(PXSTRING pxstr, const char *pcszSource);
    89     typedef ULONG XWPENTRY XSTRCAT(PXSTRING pxstr, const char *pcszSource);
     88    ULONG XWPENTRY xstrcat(PXSTRING pxstr, const char *pcszSource, ULONG ulSourceLength);
     89    typedef ULONG XWPENTRY XSTRCAT(PXSTRING pxstr, const char *pcszSource, ULONG ulSourceLength);
    9090    typedef XSTRCAT *PXSTRCAT;
    9191
     
    102102
    103103    #define xstrIsString(psz) ( (psz != 0) && (*(psz) != 0) )
     104
     105    ULONG XWPENTRY xstrrpl(PXSTRING pxstr,
     106                           ULONG ulFirstReplOfs,
     107                           ULONG cReplLen,
     108                           const XSTRING *pstrReplaceWith);
     109    typedef ULONG XWPENTRY XSTRRPL(PXSTRING pxstr,
     110                                   ULONG ulFirstReplOfs,
     111                                   ULONG cReplLen,
     112                                   const XSTRING *pstrReplaceWith);
     113    typedef XSTRRPL *PXSTRRPL;
    104114
    105115    PSZ XWPENTRY xstrFindWord(const XSTRING *pxstr,
     
    119129    typedef XSTRFINDWORD *PXSTRFINDWORD;
    120130
    121     ULONG XWPENTRY xstrrpl(PXSTRING pxstr,
    122                            PULONG pulOfs,
    123                            const XSTRING *pstrSearch,
    124                            const XSTRING *pstrReplace,
    125                            size_t *pShiftTable,
    126                            PBOOL pfRepeatFind);
    127     typedef ULONG XWPENTRY XSTRRPL(PXSTRING pxstr,
     131    ULONG XWPENTRY xstrFindReplace(PXSTRING pxstr,
    128132                                   PULONG pulOfs,
    129133                                   const XSTRING *pstrSearch,
     
    131135                                   size_t *pShiftTable,
    132136                                   PBOOL pfRepeatFind);
    133     typedef XSTRRPL *PXSTRRPL;
     137    typedef ULONG XWPENTRY XSTRFINDREPLACE(PXSTRING pxstr,
     138                                           PULONG pulOfs,
     139                                           const XSTRING *pstrSearch,
     140                                           const XSTRING *pstrReplace,
     141                                           size_t *pShiftTable,
     142                                           PBOOL pfRepeatFind);
     143    typedef XSTRFINDREPLACE *PXSTRFINDREPLACE;
    134144
    135     ULONG XWPENTRY xstrcrpl(PXSTRING pxstr,
    136                             PULONG pulOfs,
    137                             const char *pcszSearch,
    138                             const char *pcszReplace);
    139     typedef ULONG XWPENTRY XSTRCRPL(PXSTRING pxstr,
     145    ULONG XWPENTRY xstrFindReplaceC(PXSTRING pxstr,
    140146                                    PULONG pulOfs,
    141147                                    const char *pcszSearch,
    142148                                    const char *pcszReplace);
    143     typedef XSTRCRPL *PXSTRCRPL;
     149    typedef ULONG XWPENTRY XSTRFINDREPLACEC(PXSTRING pxstr,
     150                                            PULONG pulOfs,
     151                                            const char *pcszSearch,
     152                                            const char *pcszReplace);
     153    typedef XSTRFINDREPLACEC *PXSTRFINDREPLACEC;
     154
     155    VOID XWPENTRY xstrConvertLineFormat(PXSTRING pxstr, BOOL fToCFormat);
     156    typedef VOID XWPENTRY XSTRCONVERTLINEFORMAT(PXSTRING pxstr, BOOL fToCFormat);
     157    typedef XSTRCONVERTLINEFORMAT *PXSTRCONVERTLINEFORMAT;
     158
    144159#endif
    145160
  • trunk/src/helpers/dosh.c

    r22 r23  
    10651065
    10661066/*
    1067  *@@ doshReadTextFile:
     1067 *@@ doshLoadTextFile:
    10681068 *      reads a text file from disk, allocates memory
    10691069 *      via malloc() and sets a pointer to this
     
    10741074 *      Otherwise, you should free() the buffer when
    10751075 *      no longer needed.
    1076  */
    1077 
    1078 APIRET doshReadTextFile(PSZ pszFile,        // in: file name to read
    1079                         PSZ* ppszContent)   // out: newly allocated buffer with file's content
     1076 *
     1077 *@@changed V0.9.7 (2001-01-15) [umoeller]: renamed from doshReadTextFile
     1078 */
     1079
     1080APIRET doshLoadTextFile(const char *pcszFile,  // in: file name to read
     1081                        PSZ* ppszContent)      // out: newly allocated buffer with file's content
    10801082{
    10811083    ULONG   ulSize,
     
    10851087    PSZ     pszContent = NULL;
    10861088
    1087     APIRET arc = DosOpen(pszFile,
     1089    APIRET arc = DosOpen((PSZ)pcszFile,
    10881090                         &hFile,
    10891091                         &ulAction,                      // action taken
  • trunk/src/helpers/gpih.c

    r21 r23  
    152152    *pb++ = (BYTE)((LONG)(*pb) * bMultiplier / bDivisor); // green
    153153    *pb++ = (BYTE)((LONG)(*pb) * bMultiplier / bDivisor); // red
     154}
     155
     156/*
     157 *@@ gpihSwitchToRGB:
     158 *      this switches the given HPS into RGB mode. You should
     159 *      always use this if you are operating with RGB colors.
     160 *
     161 *      This is just a shortcut to calling
     162 *
     163 +          GpiCreateLogColorTable(hps, 0, LCOLF_RGB, 0, 0, NULL);
     164 *
     165 *@@changed V0.9.7 (2001-01-15) [umoeller]: turned macro into function
     166 */
     167
     168BOOL gpihSwitchToRGB(HPS hps)
     169{
     170    return (GpiCreateLogColorTable(hps, 0, LCOLF_RGB, 0, 0, NULL));
    154171}
    155172
  • trunk/src/helpers/helpers_post.in

    r15 r23  
    7171                    $(HLPINC)\linklist.h $(HLPINC)\winh.h
    7272
    73 $(OUTPUTDIR)\cctl_tooltip.obj:     $(@B).c $(HLPINC)\comctl.h \
     73$(OUTPUTDIR)\cctl_tooltip.obj:      $(@B).c $(HLPINC)\comctl.h \
    7474               $(PROJECTINC)\setup.h \
    7575               $(HLPINC)\cnrh.h $(HLPINC)\except.h $(HLPINC)\gpih.h \
    7676                    $(HLPINC)\linklist.h $(HLPINC)\winh.h
    7777
    78 $(OUTPUTDIR)\comctl.obj:     $(@B).c $(HLPINC)\$(@B).h \
     78$(OUTPUTDIR)\comctl.obj:            $(@B).c $(HLPINC)\$(@B).h \
    7979               $(PROJECTINC)\setup.h \
    8080               $(HLPINC)\cnrh.h $(HLPINC)\except.h $(HLPINC)\gpih.h \
    8181                    $(HLPINC)\linklist.h $(HLPINC)\winh.h
    8282
    83 $(OUTPUTDIR)\datetime.obj:     $(@B).c $(HLPINC)\$(@B).h \
     83$(OUTPUTDIR)\configsys.obj:         $(@B).c $(HLPINC)\$(@B).h \
     84               $(PROJECTINC)\setup.h \
     85               $(HLPINC)\dosh.h $(HLPINC)\stringh.h $(HLPINC)\xstring.h
     86
     87$(OUTPUTDIR)\datetime.obj:          $(@B).c $(HLPINC)\$(@B).h \
    8488               $(PROJECTINC)\setup.h
    8589
  • trunk/src/helpers/helpers_pre.in

    r14 r23  
    7272$(OUTPUTDIR)\cctl_tooltip.obj \
    7373$(OUTPUTDIR)\comctl.obj \
     74$(OUTPUTDIR)\configsys.obj \
    7475$(OUTPUTDIR)\cnrh.obj \
    7576$(OUTPUTDIR)\gpih.obj \
  • trunk/src/helpers/stringh.c

    r21 r23  
    432432
    433433/*
    434  *@@ strhrpl:
    435  *      wrapper around xstrrpl to work with C strings.
     434 *@@ strhFindReplace:
     435 *      wrapper around xstrFindReplace to work with C strings.
    436436 *      Note that *ppszBuf can get reallocated and must
    437437 *      be free()'able.
    438438 *
    439439 *      Repetitive use of this wrapper is not recommended
    440  *      because it is considerably slower than xstrrpl.
     440 *      because it is considerably slower than xstrFindReplace.
    441441 *
    442442 *@@added V0.9.6 (2000-11-01) [umoeller]
    443  */
    444 
    445 ULONG strhrpl(PSZ *ppszBuf,                // in/out: string
    446               PULONG pulOfs,               // in: where to begin search (0 = start);
    447                                            // out: ofs of first char after replacement string
    448               const char *pcszSearch,      // in: search string; cannot be NULL
    449               const char *pcszReplace)     // in: replacement string; cannot be NULL
     443 *@@changed V0.9.7 (2001-01-15) [umoeller]: renamed from strhrpl
     444 */
     445
     446ULONG strhFindReplace(PSZ *ppszBuf,                // in/out: string
     447                      PULONG pulOfs,               // in: where to begin search (0 = start);
     448                                                   // out: ofs of first char after replacement string
     449                      const char *pcszSearch,      // in: search string; cannot be NULL
     450                      const char *pcszReplace)     // in: replacement string; cannot be NULL
    450451{
    451452    ULONG   ulrc = 0;
     
    455456    size_t  ShiftTable[256];
    456457    BOOL    fRepeat = FALSE;
    457     xstrInit(&xstrBuf, 0);
    458     xstrset(&xstrBuf, *ppszBuf);
    459     xstrInit(&xstrFind, 0);
    460     xstrset(&xstrFind, (PSZ)pcszSearch);
    461     xstrInit(&xstrReplace, 0);
    462     xstrset(&xstrReplace, (PSZ)pcszReplace);
    463 
    464     if ((ulrc = xstrrpl(&xstrBuf,
    465                         pulOfs,
    466                         &xstrFind,
    467                         &xstrReplace,
    468                         ShiftTable,
    469                         &fRepeat)))
     458    xstrInitSet(&xstrBuf, *ppszBuf);
     459                // reallocated and returned, so we're safe
     460    xstrInitSet(&xstrFind, (PSZ)pcszSearch);
     461    xstrInitSet(&xstrReplace, (PSZ)pcszReplace);
     462                // these two are never freed, so we're safe too
     463
     464    if ((ulrc = xstrFindReplace(&xstrBuf,
     465                                pulOfs,
     466                                &xstrFind,
     467                                &xstrReplace,
     468                                ShiftTable,
     469                                &fRepeat)))
    470470        // replaced:
    471471        *ppszBuf = xstrBuf.psz;
     
    10091009    }
    10101010
    1011     if (pulOffset)
     1011    if ((pulOffset) && (prc))
    10121012        *pulOffset = prc - pcszSearchIn;
    10131013
     
    10351035        *pulOffset = pNextLine - pszSearchIn;
    10361036    return (pNextLine);
    1037 }
    1038 
    1039 /*
    1040  *@@ strhFindKey:
    1041  *      finds pszKey in pszSearchIn; similar to strhistr,
    1042  *      but this one makes sure the key is at the beginning
    1043  *      of a line. Spaces before the key are tolerated.
    1044  *      Returns NULL if the key was not found.
    1045  *
    1046  *      Used by strhGetParameter/strhSetParameter; useful
    1047  *      for analyzing CONFIG.SYS settings.
    1048  *
    1049  *@@changed V0.9.0 [umoeller]: fixed bug in that this would also return something if only the first chars matched
    1050  *@@changed V0.9.0 [umoeller]: fixed bug which could cause character before pszSearchIn to be examined
    1051  */
    1052 
    1053 PSZ strhFindKey(const char *pcszSearchIn,   // in: text buffer to search
    1054                 const char *pcszKey,        // in: key to search for
    1055                 PBOOL pfIsAllUpperCase) // out: TRUE if key is completely in upper case;
    1056                                      // can be NULL if not needed
    1057 {
    1058     const char  *p = NULL;
    1059     PSZ         pReturn = NULL;
    1060     // BOOL    fFound = FALSE;
    1061 
    1062     p = pcszSearchIn;
    1063     do {
    1064         p = strhistr(p, pcszKey);
    1065 
    1066         if ((p) && (p >= pcszSearchIn))
    1067         {
    1068             // make sure the key is at the beginning of a line
    1069             // by going backwards until we find a char != " "
    1070             const char *p2 = p;
    1071             while (     (*p2 == ' ')
    1072                      && (p2 > pcszSearchIn)
    1073                   )
    1074                 p2--;
    1075 
    1076             // if previous char is an EOL sign, go on
    1077             if (    (p2 == pcszSearchIn)     // order fixed V0.9.0, Rdiger Ihle
    1078                  || (*(p2-1) == '\r')
    1079                  || (*(p2-1) == '\n')
    1080                )
    1081             {
    1082                 // now check whether the char after the search
    1083                 // is a "=" char
    1084                 // ULONG cbKey = strlen(pszKey);
    1085 
    1086                 // tolerate spaces before "="
    1087                 /* PSZ p3 = p;
    1088                 while (*(p3+cbKey) == ' ')
    1089                     p3++;
    1090 
    1091                 if (*(p3+cbKey) == '=') */
    1092                 {
    1093                     // found:
    1094                     pReturn = (PSZ)p; // go on, p contains found key
    1095 
    1096                     // test for all upper case?
    1097                     if (pfIsAllUpperCase)
    1098                     {
    1099                         ULONG cbKey2 = strlen(pcszKey),
    1100                               ul = 0;
    1101                         *pfIsAllUpperCase = TRUE;
    1102                         for (ul = 0; ul < cbKey2; ul++)
    1103                             if (islower(*(p+ul)))
    1104                             {
    1105                                 *pfIsAllUpperCase = FALSE;
    1106                                 break; // for
    1107                             }
    1108                     }
    1109 
    1110                     break; // do
    1111                 }
    1112             } // else search next key
    1113 
    1114             p++; // search on after this key
    1115         }
    1116     } while ((!pReturn) && (p != NULL) && (p != pcszSearchIn));
    1117 
    1118     return (pReturn);
    1119 }
    1120 
    1121 /*
    1122  *@@ strhGetParameter:
    1123  *      searches pszSearchIn for the key pszKey; if found, it
    1124  *      returns a pointer to the following characters in pszSearchIn
    1125  *      and, if pszCopyTo != NULL, copies the rest of the line to
    1126  *      that buffer, of which cbCopyTo specified the size.
    1127  *
    1128  *      If the key is not found, NULL is returned.
    1129  *      String search is done by calling strhFindKey.
    1130  *      This is useful for querying CONFIG.SYS settings.
    1131  *
    1132  *      <B>Example:</B>
    1133  *
    1134  *      this would return "YES" if you searched for "PAUSEONERROR=",
    1135  *      and "PAUSEONERROR=YES" existed in pszSearchIn.
    1136  */
    1137 
    1138 PSZ strhGetParameter(const char *pcszSearchIn,  // in: text buffer to search
    1139                      const char *pcszKey,       // in: key to search for
    1140                      PSZ pszCopyTo,             // out: key value
    1141                      ULONG cbCopyTo)            // out: sizeof(*pszCopyTo)
    1142 {
    1143     PSZ p = strhFindKey(pcszSearchIn, pcszKey, NULL),
    1144         prc = NULL;
    1145     if (p)
    1146     {
    1147         prc = p + strlen(pcszKey);
    1148         if (pszCopyTo)
    1149         // copy to pszCopyTo
    1150         {
    1151             ULONG cb;
    1152             PSZ pEOL = strhFindEOL(prc, &cb);
    1153             if (pEOL)
    1154             {
    1155                 if (cb > cbCopyTo)
    1156                     cb = cbCopyTo-1;
    1157                 strhncpy0(pszCopyTo, prc, cb);
    1158             }
    1159         }
    1160     }
    1161 
    1162     return (prc);
    1163 }
    1164 
    1165 /*
    1166  *@@ strhSetParameter:
    1167  *      searches *ppszBuf for the key pszKey; if found, it
    1168  *      replaces the characters following this key up to the
    1169  *      end of the line with pszParam. If pszKey is not found in
    1170  *      *ppszBuf, it is appended to the file in a new line.
    1171  *
    1172  *      If any changes are made, *ppszBuf is re-allocated.
    1173  *
    1174  *      This function searches w/out case sensitivity.
    1175  *
    1176  *      Returns a pointer to the new parameter inside the buffer.
    1177  *
    1178  *@@changed V0.9.0 [umoeller]: changed function prototype to PSZ* ppszSearchIn
    1179  */
    1180 
    1181 PSZ strhSetParameter(PSZ* ppszBuf,    // in: text buffer to search
    1182                      const char *pcszKey,   // in: key to search for
    1183                      PSZ pszNewParam, // in: new parameter to set for key
    1184                      BOOL fRespectCase)  // in: if TRUE, pszNewParam will
    1185                              // be converted to upper case if the found key is
    1186                              // in upper case also. pszNewParam should be in
    1187                              // lower case if you use this.
    1188 {
    1189     BOOL fIsAllUpperCase = FALSE;
    1190     PSZ pKey = strhFindKey(*ppszBuf, pcszKey, &fIsAllUpperCase),
    1191         prc = NULL;
    1192 
    1193     if (pKey)
    1194     {
    1195         // key found in file:
    1196         // replace existing parameter
    1197         PSZ pOldParam = pKey + strlen(pcszKey);
    1198 
    1199         prc = pOldParam;
    1200         // pOldParam now has the old parameter, which we
    1201         // will overwrite now
    1202 
    1203         if (pOldParam)
    1204         {
    1205             ULONG cbOldParam;
    1206             PSZ pEOL = strhFindEOL(pOldParam, &cbOldParam);
    1207             // pEOL now has first end-of-line after the parameter
    1208 
    1209             if (pEOL)
    1210             {
    1211                 XSTRING strBuf;
    1212                 ULONG   ulOfs = 0;
    1213 
    1214                 PSZ pszOldCopy = (PSZ)malloc(cbOldParam+1);
    1215                 strncpy(pszOldCopy, pOldParam, cbOldParam);
    1216                 pszOldCopy[cbOldParam] = '\0';
    1217 
    1218                 xstrInit(&strBuf, 0);
    1219                 xstrset(&strBuf, *ppszBuf);         // this must not be freed!
    1220                 /* xstrInit(&strFind, 0);
    1221                 xstrset(&strFind, pszOldCopy);      // this must not be freed!
    1222                 xstrInit(&strReplace, 0);
    1223                 xstrset(&strReplace, pszNewParam);  // this must not be freed!
    1224                    */
    1225 
    1226                 // check for upper case desired?
    1227                 if (fRespectCase)
    1228                     if (fIsAllUpperCase)
    1229                         strupr(pszNewParam);
    1230 
    1231                 xstrcrpl(&strBuf, &ulOfs, pszOldCopy, pszNewParam);
    1232 
    1233                 free(pszOldCopy);
    1234 
    1235                 *ppszBuf = strBuf.psz;
    1236             }
    1237         }
    1238     }
    1239     else
    1240     {
    1241         PSZ pszNew = (PSZ)malloc(strlen(*ppszBuf)
    1242                               + strlen(pcszKey)
    1243                               + strlen(pszNewParam)
    1244                               + 5);     // 2 * \r\n + null byte
    1245         // key not found: append to end of file
    1246         sprintf(pszNew, "%s\r\n%s%s\r\n",
    1247                 *ppszBuf, pcszKey, pszNewParam);
    1248         free(*ppszBuf);
    1249         *ppszBuf = pszNew;
    1250     }
    1251 
    1252     return (prc);
    1253 }
    1254 
    1255 /*
    1256  *@@ strhDeleteLine:
    1257  *      this deletes the line in pszSearchIn which starts with
    1258  *      the key pszKey. Returns TRUE if the line was found and
    1259  *      deleted.
    1260  *
    1261  *      This copies within pszSearchIn.
    1262  */
    1263 
    1264 BOOL strhDeleteLine(PSZ pszSearchIn,        // in: buffer to search
    1265                     PSZ pszKey)             // in: key to find
    1266 {
    1267     BOOL fIsAllUpperCase = FALSE;
    1268     PSZ pKey = strhFindKey(pszSearchIn, pszKey, &fIsAllUpperCase);
    1269     BOOL brc = FALSE;
    1270 
    1271     if (pKey) {
    1272         PSZ pEOL = strhFindEOL(pKey, NULL);
    1273         // pEOL now has first end-of-line after the key
    1274         if (pEOL)
    1275         {
    1276             // delete line by overwriting it with
    1277             // the next line
    1278             strcpy(pKey, pEOL+2);
    1279         }
    1280         else
    1281         {
    1282             // EOL not found: we must be at the end of the file
    1283             *pKey = '\0';
    1284         }
    1285         brc = TRUE;
    1286     }
    1287 
    1288     return (brc);
    12891037}
    12901038
     
    18481596                            szLine,         // bytes string
    18491597                            szAscii);       // ASCII string
    1850             xstrcat(&strReturn, szTemp);
     1598            xstrcat(&strReturn, szTemp, 0);
    18511599
    18521600            // restart line buffer
  • trunk/src/helpers/textview.c

    r18 r23  
    23962396                if (pwndParams->fsStatus & WPM_TEXT)
    23972397                {
    2398                     xstrcpy(&ptxvd->xfd.strViewText, pwndParams->pszText);
     2398                    xstrcpy(&ptxvd->xfd.strViewText,
     2399                            pwndParams->pszText,
     2400                            0);
    23992401                    ptxvd->lViewXOfs = 0;
    24002402                    ptxvd->lViewYOfs = 0;
     
    34733475
    34743476    // use text from window
    3475     xstrcpy(&xfd.strViewText, pszViewText);
     3477    xstrcpy(&xfd.strViewText, pszViewText, 0);
    34763478
    34773479    // setup page
  • trunk/src/helpers/winh.c

    r21 r23  
    24322432 *
    24332433 *@@added V0.9.6 (2000-10-16) [umoeller]
     2434 *@@changed V0.9.7 (2001-01-15) [umoeller]: now using XSTRING
    24342435 */
    24352436
    24362437VOID CallBatchCorrectly(PPROGDETAILS pProgDetails,
    2437                         PSZ *ppszParams,            // in/out: modified parameters (reallocated)
     2438                        PXSTRING pstrParams,        // in/out: modified parameters (reallocated)
    24382439                        const char *pcszEnvVar,     // in: env var spec'g command proc
    24392440                                                    // (e.g. "OS2_SHELL"); can be NULL
     
    24422443    // XXX.CMD file as executable:
    24432444    // fix args to /C XXX.CMD
    2444     XSTRING strNewParams;
    2445     xstrInit(&strNewParams, 200);
    2446     xstrcpy(&strNewParams, "/C ");
    2447     xstrcat(&strNewParams, pProgDetails->pszExecutable);
    2448     if (*ppszParams)
    2449     {
    2450         // append old params
    2451         xstrcat(&strNewParams, " ");
    2452         xstrcat(&strNewParams, *ppszParams);
    2453         free(*ppszParams);
    2454     }
    2455     *ppszParams = strNewParams.psz;
    2456             // freed by caller
     2445
     2446    PSZ     pszOldParams = NULL;
     2447    ULONG   ulOldParamsLength = pstrParams->ulLength;
     2448    if (ulOldParamsLength)
     2449        // we have parameters already:
     2450        // make a backup... we'll append that later
     2451        pszOldParams = strdup(pstrParams->psz);
     2452
     2453    // set new params to "/C filename.cmd"
     2454    xstrcpy(pstrParams, "/C ", 0);
     2455    xstrcat(pstrParams,
     2456            pProgDetails->pszExecutable,
     2457            0);
     2458
     2459    if (pszOldParams)
     2460    {
     2461        // .cmd had params:
     2462        // append space and old params
     2463        xstrcatc(pstrParams, ' ');
     2464        xstrcat(pstrParams,
     2465                pszOldParams,
     2466                ulOldParamsLength);
     2467        free(pszOldParams);
     2468    }
    24572469
    24582470    // set executable to $(OS2_SHELL)
     
    25082520{
    25092521    HAPP            happ = NULLHANDLE;
    2510     PSZ             pszParamsPatched = NULL;
     2522    XSTRING         strParamsPatched;
    25112523    BOOL            fIsWindowsApp = FALSE,
    25122524                    fIsWindowsEnhApp = FALSE;
     
    25272539    // we need this for string manipulations below...
    25282540    if (ProgDetails.pszParameters)
    2529         pszParamsPatched = strdup(ProgDetails.pszParameters);
     2541        xstrInitCopy(&strParamsPatched,
     2542                     ProgDetails.pszParameters,
     2543                     100);
     2544    else
     2545        // no old params:
     2546        xstrInit(&strParamsPatched, 100);
    25302547
    25312548    // _Pmpf((__FUNCTION__ ": old progc: 0x%lX", pcProgDetails->progt.progc));
     
    25872604        {
    25882605            // enhanced Win-OS/2 session:
    2589             XSTRING str2;
    2590             xstrInit(&str2, 200);
    2591             xstrcpy(&str2, "/3 ");
    2592             if (pszParamsPatched)
     2606            PSZ psz = NULL;
     2607            if (strParamsPatched.ulLength)
     2608                // "/3 " + existing params
     2609                psz = strdup(strParamsPatched.psz);
     2610
     2611            xstrcpy(&strParamsPatched, "/3 ", 0);
     2612
     2613            if (psz)
    25932614            {
    2594                 // append existing params
    2595                 xstrcat(&str2, pszParamsPatched);
    2596                 free(pszParamsPatched);
     2615                xstrcat(&strParamsPatched, psz, 0);
     2616                free(psz);
    25972617            }
    2598 
    2599             pszParamsPatched = str2.psz;
    26002618        }
    26012619
     
    26332651                    {
    26342652                        CallBatchCorrectly(&ProgDetails,
    2635                                            &pszParamsPatched,
     2653                                           &strParamsPatched,
    26362654                                           "OS2_SHELL",
    26372655                                           "CMD.EXE");
     
    26492667                    {
    26502668                        CallBatchCorrectly(&ProgDetails,
    2651                                            &pszParamsPatched,
     2669                                           &strParamsPatched,
    26522670                                           NULL,
    26532671                                           "COMMAND.COM");
     
    27162734    // _Pmpf(("    new progc: 0x%lX", ProgDetails.progt.progc));
    27172735
    2718     ProgDetails.pszParameters = pszParamsPatched;
     2736    ProgDetails.pszParameters = strParamsPatched.psz;
    27192737
    27202738    happ = WinStartApp(hwndNotify,
    27212739                                // receives WM_APPTERMINATENOTIFY
    27222740                       &ProgDetails,
    2723                        pszParamsPatched,
     2741                       strParamsPatched.psz,
    27242742                       NULL,            // "reserved", PMREF says...
    27252743                       SAF_INSTALLEDCMDLINE);
     
    27342752    // _Pmpf((__FUNCTION__ ": got happ 0x%lX", happ));
    27352753
    2736     if (pszParamsPatched)
    2737         free(pszParamsPatched);
     2754    xstrClear(&strParamsPatched);
    27382755    if (pszWinOS2Env)
    27392756        free(pszWinOS2Env);
     
    30683085 *@@ winhReplaceWindowText:
    30693086 *      this is a combination of winhQueryWindowText
    3070  *      and xstrrpl (stringh.c) to replace substrings
    3071  *      in a window.
     3087 *      and strhFindReplace to replace substrings in a window.
    30723088 *
    30733089 *      This is useful for filling in placeholders
     
    30933109    {
    30943110        ULONG ulOfs = 0;
    3095         if (strhrpl(&pszText, &ulOfs, pszSearch, pszReplaceWith) > 0)
     3111        if (strhFindReplace(&pszText, &ulOfs, pszSearch, pszReplaceWith) > 0)
    30963112        {
    30973113            WinSetWindowText(hwnd, pszText);
  • trunk/src/helpers/xstring.c

    r21 r23  
    370370 *      If pxstr contains something, its contents are destroyed.
    371371 *
     372 *      With ulSourceLength, specify the length of pcszSource.
     373 *      If you specify 0, this function will run strlen(pcszSource)
     374 *      itself. If you already know the length of pcszSource (or
     375 *      only want to copy a substring), you can speed this function
     376 *      up a bit this way.
     377 *
    372378 *      Returns the length of the new string (excluding the null
    373379 *      terminator), or null upon errors.
     
    377383 +          XSTRING str;
    378384 +          xstrInit(&str, 0);
    379  +          xstrcpy(&str, "blah");
     385 +          xstrcpy(&str, "blah", 0);
    380386 *
    381387 *      This sequence can be abbreviated using xstrInitCopy.
     
    383389 *@@changed V0.9.2 (2000-04-01) [umoeller]: renamed from strhxcpy
    384390 *@@changed V0.9.6 (2000-11-01) [umoeller]: rewritten
     391 *@@changed V0.9.7 (2001-01-15) [umoeller]: added ulSourceLength
    385392 */
    386393
    387394ULONG xstrcpy(PXSTRING pxstr,               // in/out: string
    388               const char *pcszSource)       // in: source, can be NULL
    389 {
    390     xstrClear(pxstr);
     395              const char *pcszSource,       // in: source, can be NULL
     396              ULONG ulSourceLength)         // in: length of pcszSource or 0
     397{
     398    // xstrClear(pxstr);        NOOOO! this frees the string, we want to keep the memory
    391399
    392400    if (pxstr)
    393401    {
    394         ULONG   ulSourceLength = 0;
    395402        if (pcszSource)
    396             ulSourceLength = strlen(pcszSource);
    397 
    398         if (ulSourceLength)
    399403        {
    400             // we do have a source string:
    401             ULONG cbNeeded = ulSourceLength + 1;
    402             if (cbNeeded > pxstr->cbAllocated)
     404            if (ulSourceLength == 0)
     405                ulSourceLength = strlen(pcszSource);
     406
     407            if (ulSourceLength)
    403408            {
    404                 // we need more memory than we have previously
    405                 // allocated:
    406                 pxstr->cbAllocated = cbNeeded;
    407                 pxstr->psz = (PSZ)malloc(cbNeeded);
     409                // we do have a source string:
     410                ULONG cbNeeded = ulSourceLength + 1;
     411                if (cbNeeded > pxstr->cbAllocated)
     412                {
     413                    // we need more memory than we have previously
     414                    // allocated:
     415                    pxstr->cbAllocated = cbNeeded;
     416                    pxstr->psz = (PSZ)malloc(cbNeeded);
     417                }
     418                // else: we have enough memory
     419
     420                // strcpy(pxstr->psz, pcszSource);
     421                memcpy(pxstr->psz,
     422                       pcszSource,
     423                       ulSourceLength);
     424                *(pxstr->psz + ulSourceLength) = 0;
    408425            }
    409             // else: we have enough memory
    410 
    411             strcpy(pxstr->psz, pcszSource);
     426            else
     427            {
     428                // no source specified or source is empty:
     429                if (pxstr->cbAllocated)
     430                    // we did have a string: set to empty,
     431                    // but leave allocated memory intact
     432                    *(pxstr->psz) = 0;
     433                // else: pxstr->psz is still NULL
     434            }
     435
     436            // in all cases, set new length
     437            pxstr->ulLength = ulSourceLength;
    412438        }
    413         else
    414         {
    415             // no source specified or source is empty:
    416             if (pxstr->cbAllocated)
    417                 // we did have a string: set to empty,
    418                 // but leave allocated memory intact
    419                 *(pxstr->psz) = 0;
    420             // else: pxstr->psz is still NULL
    421         }
    422 
    423         // in all cases, set new length
    424         pxstr->ulLength = ulSourceLength;
    425439    }
    426440
     
    435449 *      If pxstr is empty, this behaves just like xstrcpy.
    436450 *
     451 *      With ulSourceLength, specify the length of pcszSource.
     452 *      If you specify 0, this function will run strlen(pcszSource)
     453 *      itself. If you already know the length of pcszSource (or
     454 *      only want to copy a substring), you can speed this function
     455 *      up a bit this way.
     456 *
    437457 *      Returns the length of the new string (excluding the null
    438458 *      terminator) if the string was changed, or 0 if nothing
     
    446466 +          XSTRING str;
    447467 +          xstrInit(&str, 0);
    448  +          xstrcpy(&str, "blah");
    449  +          xstrcat(&str, "blup");
     468 +          xstrcpy(&str, "blah", 0);
     469 +          xstrcat(&str, "blup", 0);
    450470 *
    451471 *      After this, str.psz points to a new string containing
     
    458478 *@@changed V0.9.6 (2000-11-01) [umoeller]: rewritten
    459479 *@@changed V0.9.7 (2000-12-10) [umoeller]: return value was wrong
     480 *@@changed V0.9.7 (2001-01-15) [umoeller]: added ulSourceLength
    460481 */
    461482
    462483ULONG xstrcat(PXSTRING pxstr,               // in/out: string
    463               const char *pcszSource)       // in: source, can be NULL
     484              const char *pcszSource,       // in: source, can be NULL
     485              ULONG ulSourceLength)         // in: length of pcszSource or 0
    464486{
    465487    ULONG   ulrc = 0;
     
    467489    if (pxstr)
    468490    {
    469         ULONG   ulSourceLength = 0;
    470491        if (pcszSource)
    471             ulSourceLength = strlen(pcszSource);
    472 
    473         if (ulSourceLength)
    474492        {
    475             // we do have a source string:
    476 
    477             // 1) memory management
    478             ULONG   cbNeeded = pxstr->ulLength + ulSourceLength + 1;
    479             if (cbNeeded > pxstr->cbAllocated)
     493            if (ulSourceLength == 0)
     494                ulSourceLength = strlen(pcszSource);
     495
     496            if (ulSourceLength)
    480497            {
    481                 // we need more memory than we have previously
    482                 // allocated:
    483                 if (pxstr->cbAllocated)
    484                     // appendee already had memory:
    485                     // reallocate
    486                     pxstr->psz = (PSZ)realloc(pxstr->psz,
    487                                               cbNeeded);
    488                 else
    489                     // appendee has no memory:
    490                     pxstr->psz = (PSZ)malloc(cbNeeded);
    491 
    492                 pxstr->cbAllocated = cbNeeded;
    493                         // ulLength is unchanged yet
    494             }
    495             // else: we have enough memory, both if appendee
    496             //       is empty or not empty
    497 
    498             // now we have:
    499             // -- if appendee (pxstr) had enough memory, no problem
    500             // -- if appendee (pxstr) needed more memory
    501             //      -- and was not empty: pxstr->psz now points to a
    502             //         reallocated copy of the old string
    503             //      -- and was empty: pxstr->psz now points to a
    504             //         new (unitialized) buffer
    505 
    506             // 2) append source string:
    507             memcpy(pxstr->psz + pxstr->ulLength,
    508                    pcszSource,
    509                    ulSourceLength + 1);     // null terminator
    510 
    511             // in all cases, set new length
    512             pxstr->ulLength += ulSourceLength;
    513             ulrc = pxstr->ulLength;     // V0.9.7 (2000-12-10) [umoeller]
    514 
    515         } // end if (ulSourceLength)
     498                // we do have a source string:
     499
     500                // 1) memory management
     501                ULONG   cbNeeded = pxstr->ulLength + ulSourceLength + 1;
     502                if (cbNeeded > pxstr->cbAllocated)
     503                {
     504                    // we need more memory than we have previously
     505                    // allocated:
     506                    if (pxstr->cbAllocated)
     507                        // appendee already had memory:
     508                        // reallocate
     509                        pxstr->psz = (PSZ)realloc(pxstr->psz,
     510                                                  cbNeeded);
     511                    else
     512                        // appendee has no memory:
     513                        pxstr->psz = (PSZ)malloc(cbNeeded);
     514
     515                    pxstr->cbAllocated = cbNeeded;
     516                            // ulLength is unchanged yet
     517                }
     518                // else: we have enough memory, both if appendee
     519                //       is empty or not empty
     520
     521                // now we have:
     522                // -- if appendee (pxstr) had enough memory, no problem
     523                // -- if appendee (pxstr) needed more memory
     524                //      -- and was not empty: pxstr->psz now points to a
     525                //         reallocated copy of the old string
     526                //      -- and was empty: pxstr->psz now points to a
     527                //         new (unitialized) buffer
     528
     529                // 2) append source string:
     530                memcpy(pxstr->psz + pxstr->ulLength,
     531                       pcszSource,
     532                       ulSourceLength + 1);     // null terminator
     533
     534                // in all cases, set new length
     535                pxstr->ulLength += ulSourceLength;
     536                ulrc = pxstr->ulLength;     // V0.9.7 (2000-12-10) [umoeller]
     537
     538            } // end if (ulSourceLength)
     539        }
     540
    516541        // else no source specified or source is empty:
    517542        // do nothing
     
    601626
    602627/*
     628 *@@ xstrrpl:
     629 *      replaces cSearchLen characters in pxstr, starting
     630 *      at the position ulStart, with the string
     631 *      in pxstrReplaceWith.
     632 *
     633 *      Returns the new length of the string, excluding
     634 *      the null terminator, or 0 if the replacement failed
     635 *      (e.g. because the offsets were too large).
     636 *
     637 *      This has been extracted from xstrFindReplace because
     638 *      if you already know the position of a substring,
     639 *      you can now call this directly. This properly
     640 *      reallocates the string if more memory is needed.
     641 *
     642 *      Example:
     643 *
     644 +          XSTRING xstr, xstrReplacement;
     645 +          xstrInitCopy(&xstr, "This is a test string.");
     646 +          //  positions:       0123456789012345678901
     647 +          //                             1         2
     648 +
     649 +          xstrInitCopy(&xstrReplacement, "stupid");
     650 +
     651 +          xstrrpl(&xstr,
     652 +                  10,     // position of "test"
     653 +                  4,      // length of "test"
     654 +                  &xstrReplacement);
     655 *
     656 *      This would yield "This is a stupid string."
     657 *
     658 *@@added V0.9.7 (2001-01-15) [umoeller]
     659 */
     660
     661ULONG xstrrpl(PXSTRING pxstr,                   // in/out: string
     662              ULONG ulFirstReplOfs,             // in: ofs of first char to replace
     663              ULONG cReplLen,                   // in: no. of chars to replace
     664              const XSTRING *pstrReplaceWith)   // in: string to replace chars with
     665{
     666    ULONG   ulrc = 0;
     667
     668    // security checks...
     669    if (    (ulFirstReplOfs + cReplLen <= pxstr->ulLength)
     670         && (pstrReplaceWith)
     671         // && (pstrReplaceWith->ulLength)      no, this can be empty
     672       )
     673    {
     674        ULONG   cReplaceLen = pstrReplaceWith->ulLength;
     675                    // can be 0!
     676
     677        // length of new string
     678        ULONG   cbNeeded = pxstr->ulLength
     679                         + cReplaceLen
     680                         - cReplLen
     681                         + 1;                  // null terminator
     682        // offset where pszSearch was found
     683                // ulFirstReplOfs = pFound - pxstr->psz; now ulFirstReplOfs
     684        PSZ     pFound = pxstr->psz + ulFirstReplOfs;
     685
     686        // now check if we have enough memory...
     687        if (pxstr->cbAllocated < cbNeeded)
     688        {
     689            // no, we need more memory:
     690            // allocate new buffer
     691            PSZ pszNew = (PSZ)malloc(cbNeeded);
     692
     693            if (ulFirstReplOfs)
     694                // "found" was not at the beginning:
     695                // copy from beginning up to found-offset
     696                memcpy(pszNew,
     697                       pxstr->psz,
     698                       ulFirstReplOfs);     // up to "found"
     699
     700            if (cReplaceLen)
     701            {
     702                // we have a replacement:
     703                // insert it next
     704                memcpy(pszNew + ulFirstReplOfs,
     705                       pstrReplaceWith->psz,
     706                       cReplaceLen + 1);        // include null terminator
     707            }
     708
     709            // copy rest:
     710            // pxstr      frontFOUNDtail
     711            //            0         1
     712            //            01234567890123
     713            //            ³    ³    ³  ³
     714            //            ³    ³    ÀÄ ulFirstReplOfs + cReplLen = 10
     715            //            ³    ³       ³
     716            //            ³    ÀÄ ulFirstReplOfs = 5
     717            //            ³            ³
     718            //            pxstr->ulLength = 14
     719            memcpy(pszNew + ulFirstReplOfs + cReplaceLen,
     720                   pFound + cReplLen,
     721                   // remaining bytes:
     722                   pxstr->ulLength - ulFirstReplOfs - cReplLen // 9
     723                        + 1); // null terminator
     724
     725            // replace old buffer with new one
     726            free(pxstr->psz);
     727            pxstr->psz = pszNew;
     728            pxstr->ulLength = cbNeeded - 1;
     729            pxstr->cbAllocated = cbNeeded;
     730        } // end if (pxstr->cbAllocated < cbNeeded)
     731        else
     732        {
     733            // we have enough memory left,
     734            // we can just overwrite in the middle...
     735
     736            PSZ     pszAfterFoundBackup = 0;
     737            // calc length of string after "found"
     738            ULONG   cTailLength = pxstr->ulLength - ulFirstReplOfs - cReplLen;
     739
     740            // if "replace" is longer than "found",
     741            // make a backup of the stuff after "found",
     742            // or this would get overwritten
     743            if (cReplaceLen > cReplLen)
     744            {
     745                pszAfterFoundBackup = (PSZ)malloc(cTailLength + 1);
     746                memcpy(pszAfterFoundBackup,
     747                       pFound + cReplLen,
     748                       cTailLength + 1);
     749            }
     750
     751            // now overwrite "found" in the middle
     752            if (cReplaceLen)
     753            {
     754                memcpy(pxstr->psz + ulFirstReplOfs,
     755                       pstrReplaceWith->psz,
     756                       cReplaceLen);        // no null terminator
     757            }
     758
     759            // now append tail (stuff after "found") again...
     760            if (pszAfterFoundBackup)
     761            {
     762                // we made a backup above:
     763                memcpy(pxstr->psz + ulFirstReplOfs + cReplaceLen,
     764                       pszAfterFoundBackup,
     765                       cTailLength + 1);
     766                free(pszAfterFoundBackup);
     767                        // done!
     768            }
     769            else
     770                // no backup:
     771                if (cReplaceLen < cReplLen)
     772                    // "replace" is shorter than "found:
     773                    memcpy(pxstr->psz + ulFirstReplOfs + cReplaceLen,
     774                           pFound + cReplLen,
     775                           cTailLength + 1);
     776                // else (cReplaceLen == cReplLen):
     777                // we can leave the tail as it is
     778
     779            pxstr->ulLength = cbNeeded - 1;
     780        }
     781
     782        ulrc = cbNeeded - 1;
     783    } // end checks
     784
     785    return (ulrc);
     786}
     787
     788/*
    603789 *@@ xstrFindWord:
    604790 *      searches for pstrFind in pxstr, starting at ulOfs.
     
    668854
    669855/*
    670  *@@ xstrrpl:
     856 *@@ xstrFindReplace:
    671857 *      replaces the first occurence of pstrSearch with
    672858 *      pstrReplace in pxstr.
     
    688874 *      (*pulOffset == 0), this starts from the beginning
    689875 *      of pxstr.
     876 *
    690877 *      If the string was found, *pulOffset will be set to the
    691878 *      first character after the new replacement string. This
     
    697884 *      speed advantage):
    698885 *
    699  *      -- strhrpl operates on C strings only;
    700  *
    701  *      -- xstrcrpl uses C strings for the search and replace
     886 *      -- strhFindReplace operates on C strings only;
     887 *
     888 *      -- xstrFindReplaceC uses C strings for the search and replace
    702889 *         parameters.
    703890 *
    704891 *      <B>Example usage:</B>
    705892 *
    706  +          XSTRING str;
    707  +          ULONG ulOffset = 0;
    708  +          xstrInit(&str, 0);
    709  +          xstrcpy(&str, "Test phrase 1. Test phrase 2.");
    710  +          while (xstrrpl(&str,
    711  +                         &ulPos,      // in/out: offset
    712  +                         "Test",      // search
    713  +                         "Dummy")     // replace
     893 +          XSTRING strBuf,
     894 +                  strFind,
     895 +                  strRepl;
     896 +          size_t  ShiftTable[256];
     897 +          BOOL    fRepeat = FALSE;
     898 +          ULONG   ulOffset = 0;
     899 +
     900 +          xstrInitCopy(&strBuf, "Test phrase 1. Test phrase 2.", 0);
     901 +          xstrInitSet(&strFind, "Test");
     902 +          xstrInitSet(&strRepl, "Dummy");
     903 +          while (xstrFindReplace(&str,
     904 +                                 &ulPos,      // in/out: offset
     905 +                                 &strFind,    // search
     906 +                                 &strRepl,    // replace
     907 +                                 ShiftTable,
     908 +                                 &fRepeat))
    714909 +              ;
    715910 *
     
    722917 *@@changed V0.9.6 (2000-11-01) [umoeller]: rewritten
    723918 *@@changed V0.9.6 (2000-11-12) [umoeller]: now using strhmemfind
    724  */
    725 
    726 ULONG xstrrpl(PXSTRING pxstr,               // in/out: string
    727               PULONG pulOfs,                // in: where to begin search (0 = start);
    728                                             // out: ofs of first char after replacement string
    729               const XSTRING *pstrSearch,    // in: search string; cannot be NULL
    730               const XSTRING *pstrReplace,   // in: replacement string; cannot be NULL
    731               size_t *pShiftTable,          // in: shift table (see strhmemfind)
    732               PBOOL pfRepeatFind)           // in: repeat find? (see strhmemfind)
     919 *@@changed V0.9.7 (2001-01-15) [umoeller]: renamed from xstrrpl; extracted new xstrrpl
     920 */
     921
     922ULONG xstrFindReplace(PXSTRING pxstr,               // in/out: string
     923                      PULONG pulOfs,                // in: where to begin search (0 = start);
     924                                                    // out: ofs of first char after replacement string
     925                      const XSTRING *pstrSearch,    // in: search string; cannot be NULL
     926                      const XSTRING *pstrReplace,   // in: replacement string; cannot be NULL
     927                      size_t *pShiftTable,          // in: shift table (see strhmemfind)
     928                      PBOOL pfRepeatFind)           // in: repeat find? (see strhmemfind)
    733929{
    734930    ULONG    ulrc = 0;      // default: not found
     
    744940        {
    745941            // yes:
    746             /* PSZ     pFound = strstr(pxstr->psz + *pulOfs,
    747                                     pstrSearch->psz);        */
    748             PSZ pFound = (PSZ)strhmemfind(pxstr->psz + *pulOfs, // in: haystack
    749                                           pxstr->ulLength - *pulOfs,
    750                                           pstrSearch->psz,
    751                                           cSearchLen,
    752                                           pShiftTable,
    753                                           pfRepeatFind);
     942            ULONG   ulOfs = *pulOfs;
     943            const char *pFound
     944                = (const char *)strhmemfind(pxstr->psz + ulOfs, // in: haystack
     945                                            pxstr->ulLength - ulOfs,
     946                                            pstrSearch->psz,
     947                                            cSearchLen,
     948                                            pShiftTable,
     949                                            pfRepeatFind);
    754950            if (pFound)
    755951            {
     952                ULONG ulFirstReplOfs = pFound - pxstr->psz;
    756953                // found in buffer from ofs:
    757                 ULONG   cReplaceLen = pstrReplace->ulLength;
    758                             // can be 0!
    759 
    760                 // length of new string
    761                 ULONG   cbNeeded = pxstr->ulLength
    762                                  + cReplaceLen
    763                                  - cSearchLen
    764                                  + 1,                  // null terminator
    765                 // offset where pszSearch was found
    766                         ulFoundOfs = pFound - pxstr->psz;
    767 
    768                 // now check if we have enough memory...
    769                 if (pxstr->cbAllocated < cbNeeded)
    770                 {
    771                     // no, we need more memory:
    772                     // allocate new buffer
    773                     PSZ pszNew = (PSZ)malloc(cbNeeded);
    774 
    775                     if (ulFoundOfs)
    776                         // "found" was not at the beginning:
    777                         // copy from beginning up to found-offset
    778                         memcpy(pszNew,
    779                                pxstr->psz,
    780                                ulFoundOfs);     // up to "found"
    781 
    782                     if (cReplaceLen)
    783                     {
    784                         // we have a replacement:
    785                         // insert it next
    786                         memcpy(pszNew + ulFoundOfs,
    787                                pstrReplace->psz,
    788                                cReplaceLen + 1);        // include null terminator
    789                     }
    790 
    791                     // copy rest:
    792                     // pxstr      frontFOUNDtail
    793                     //            0         1
    794                     //            01234567890123
    795                     //            ³    ³    ³  ³
    796                     //            ³    ³    ÀÄ ulFoundOfs + cSearchLen = 10
    797                     //            ³    ³       ³
    798                     //            ³    ÀÄ ulFoundOfs = 5
    799                     //            ³            ³
    800                     //            pxstr->ulLength = 14
    801                     memcpy(pszNew + ulFoundOfs + cReplaceLen,
    802                            pFound + cSearchLen,
    803                            // remaining bytes:
    804                            pxstr->ulLength - ulFoundOfs - cSearchLen // 9
    805                                 + 1); // null terminator
    806 
    807                     // replace old buffer with new one
    808                     free(pxstr->psz);
    809                     pxstr->psz = pszNew;
    810                     pxstr->ulLength = cbNeeded - 1;
    811                     pxstr->cbAllocated = cbNeeded;
    812                 } // end if (pxstr->cbAllocated < cbNeeded)
    813                 else
    814                 {
    815                     // we have enough memory left,
    816                     // we can just overwrite in the middle...
    817 
    818                     PSZ     pszAfterFoundBackup = 0;
    819                     // calc length of string after "found"
    820                     ULONG   cTailLength = pxstr->ulLength - ulFoundOfs - cSearchLen;
    821 
    822                     // if "replace" is longer than "found",
    823                     // make a backup of the stuff after "found",
    824                     // or this would get overwritten
    825                     if (cReplaceLen > cSearchLen)
    826                     {
    827                         pszAfterFoundBackup = (PSZ)malloc(cTailLength + 1);
    828                         memcpy(pszAfterFoundBackup,
    829                                pFound + cSearchLen,
    830                                cTailLength + 1);
    831                     }
    832 
    833                     // now overwrite "found" in the middle
    834                     if (cReplaceLen)
    835                     {
    836                         memcpy(pxstr->psz + ulFoundOfs,
    837                                pstrReplace->psz,
    838                                cReplaceLen);        // no null terminator
    839                     }
    840 
    841                     // now append tail (stuff after "found") again...
    842                     if (pszAfterFoundBackup)
    843                     {
    844                         // we made a backup above:
    845                         memcpy(pxstr->psz + ulFoundOfs + cReplaceLen,
    846                                pszAfterFoundBackup,
    847                                cTailLength + 1);
    848                         free(pszAfterFoundBackup);
    849                                 // done!
    850                     }
    851                     else
    852                         // no backup:
    853                         if (cReplaceLen < cSearchLen)
    854                             // "replace" is shorter than "found:
    855                             memcpy(pxstr->psz + ulFoundOfs + cReplaceLen,
    856                                    pFound + cSearchLen,
    857                                    cTailLength + 1);
    858                         // else (cReplaceLen == cSearchLen):
    859                         // we can leave the tail as it is
    860 
    861                     pxstr->ulLength = cbNeeded - 1;
    862                 }
     954                // replace pFound with pstrReplace
     955                ulrc = xstrrpl(pxstr,
     956                               ulFirstReplOfs,              // where to start
     957                               cSearchLen,                  // chars to replace
     958                               pstrReplace);
    863959
    864960                // return new length
    865                 ulrc = cbNeeded - 1;
    866                 *pulOfs = ulFoundOfs + cReplaceLen;
     961                *pulOfs = ulFirstReplOfs + pstrReplace->ulLength;
    867962            } // end if (pFound)
    868963        } // end if (    (*pulOfs < pxstr->ulLength) ...
     
    873968
    874969/*
    875  *@@ xstrcrpl:
    876  *      wrapper around xstrrpl() which allows using C strings
    877  *      for the find and replace parameters.
     970 *@@ xstrFindReplaceC:
     971 *      wrapper around xstrFindReplace() which allows using
     972 *      C strings for the find and replace parameters.
    878973 *
    879974 *      This creates two temporary XSTRING's for pcszSearch
    880  *      pcszReplace. As a result, this is slower than xstrrpl.
     975 *      and pcszReplace and thus cannot use the shift table
     976 *      for repetitive searches. As a result, this is slower
     977 *      than xstrFindReplace.
     978 *
    881979 *      If you search with the same strings several times,
    882  *      you'll be better off using xstrrpl() directly.
     980 *      you'll be better off using xstrFindReplace() directly.
    883981 *
    884982 *@@added V0.9.6 (2000-11-01) [umoeller]
    885  */
    886 
    887 ULONG xstrcrpl(PXSTRING pxstr,              // in/out: string
    888                PULONG pulOfs,               // in: where to begin search (0 = start);
    889                                             // out: ofs of first char after replacement string
    890                const char *pcszSearch,      // in: search string; cannot be NULL
    891                const char *pcszReplace)     // in: replacement string; cannot be NULL
    892 {
    893     // ULONG   ulrc = 0;
     983 *@@changed V0.9.7 (2001-01-15) [umoeller]: renamed from xstrcrpl
     984 */
     985
     986ULONG xstrFindReplaceC(PXSTRING pxstr,              // in/out: string
     987                       PULONG pulOfs,               // in: where to begin search (0 = start);
     988                                                    // out: ofs of first char after replacement string
     989                       const char *pcszSearch,      // in: search string; cannot be NULL
     990                       const char *pcszReplace)     // in: replacement string; cannot be NULL
     991{
    894992    XSTRING xstrFind,
    895993            xstrReplace;
     
    9021000    xstrInitSet(&xstrReplace, (PSZ)pcszReplace);
    9031001
    904     return (xstrrpl(pxstr, pulOfs, &xstrFind, &xstrReplace, ShiftTable, &fRepeat));
     1002    return (xstrFindReplace(pxstr, pulOfs, &xstrFind, &xstrReplace, ShiftTable, &fRepeat));
     1003}
     1004
     1005/*
     1006 *@@ xstrConvertLineFormat:
     1007 *      converts between line formats.
     1008 *
     1009 *      If (fToCFormat == TRUE), all \r\n pairs are replaced
     1010 *      with \n chars (UNIX or C format).
     1011 *
     1012 *      Reversely, if (fToCFormat == FALSE), all \n chars
     1013 *      are converted to \r\n pairs (DOS and OS/2 formats).
     1014 *      No check is made whether this has already been done.
     1015 *
     1016 *@@added V0.9.7 (2001-01-15) [umoeller]
     1017 */
     1018
     1019VOID xstrConvertLineFormat(PXSTRING pxstr,
     1020                           BOOL fToCFormat) // in: if TRUE, to C format; if FALSE, to OS/2 format.
     1021{
     1022    XSTRING     strFind,
     1023                strRepl;
     1024    size_t      ShiftTable[256];
     1025    BOOL        fRepeat = FALSE;
     1026    ULONG       ulOfs = 0;
     1027
     1028    if (fToCFormat)
     1029    {
     1030        // OS/2 to C:
     1031        xstrInitSet(&strFind, "\r\n");
     1032        xstrInitSet(&strRepl, "\n");
     1033    }
     1034    else
     1035    {
     1036        // C to OS/2:
     1037        xstrInitSet(&strFind, "\n");
     1038        xstrInitSet(&strRepl, "\r\n");
     1039    }
     1040
     1041    while (xstrFindReplace(pxstr,
     1042                           &ulOfs,
     1043                           &strFind,
     1044                           &strRepl,
     1045                           ShiftTable,
     1046                           &fRepeat))
     1047            ;
    9051048}
    9061049
     
    9121055            strFind,
    9131056            strReplace;
     1057    size_t  shift[256];
     1058    BOOL    fRepeat = FALSE;
    9141059    ULONG   ulOfs = 0;
    9151060
     
    9181063    xstrInit(&strReplace, 0);
    9191064
    920     xstrcpy(&str, "Test string 1. Test string 2. Test string 3. !");
    921     xstrcpy(&strFind, "Test");
    922     xstrcpy(&strReplace, "Dummy");
     1065    xstrcpy(&str, "Test string 1. Test string 2. Test string 3. !", 0);
     1066    xstrcpy(&strFind, "Test", 0);
     1067    xstrcpy(&strReplace, "Dummy", 0);
    9231068
    9241069    printf("Old string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated);
    9251070
    926     while (xstrrpl(&str,
    927                    ulOfs,
     1071    printf("Replacing \"%s\" with \"%s\".\n", strFind.psz, strReplace.psz);
     1072
     1073    fRepeat = FALSE;
     1074    ulOfs = 0;
     1075    while (xstrFindReplace(&str,
     1076                           &ulOfs,
     1077                           &strFind,
     1078                           &strReplace,
     1079                           shift, &fRepeat));
     1080        ;
     1081
     1082    printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated);
     1083
     1084    xstrcpy(&strFind, strReplace.psz, 0);
     1085    xstrClear(&strReplace);
     1086
     1087    printf("Replacing \"%s\" with \"%s\".\n", strFind.psz, strReplace.psz);
     1088
     1089    fRepeat = FALSE;
     1090    ulOfs = 0;
     1091    while (xstrFindReplace(&str,
     1092                   &ulOfs,
    9281093                   &strFind,
    9291094                   &strReplace,
    930                    &ulOfs))
     1095                   shift, &fRepeat));
    9311096        ;
    9321097
    9331098    printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated);
    9341099
    935     xstrcpy(&strFind, strReplace.psz);
    936     xstrClear(&strReplace);
     1100    xstrcpy(&strFind, " ", 0);
     1101    xstrcpy(&strReplace, ".", 0);
     1102
     1103    printf("Replacing \"%s\" with \"%s\".\n", strFind.psz, strReplace.psz);
     1104
     1105    fRepeat = FALSE;
    9371106    ulOfs = 0;
    938     while (xstrrpl(&str,
    939                    ulOfs,
     1107    while (xstrFindReplace(&str,
     1108                   &ulOfs,
    9401109                   &strFind,
    9411110                   &strReplace,
    942                    &ulOfs))
     1111                   shift, &fRepeat));
    9431112        ;
    9441113
    9451114    printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated);
    9461115
    947     xstrcpy(&strFind, " ");
    948     xstrcpy(&strReplace, ".");
     1116    xstrcpy(&strFind, ".", 0);
     1117    xstrcpy(&strReplace, "*.........................*", 0);
     1118
     1119    printf("Replacing \"%s\" with \"%s\".\n", strFind.psz, strReplace.psz);
     1120
     1121    fRepeat = FALSE;
    9491122    ulOfs = 0;
    950     while (xstrrpl(&str,
    951                    ulOfs,
     1123    while (xstrFindReplace(&str,
     1124                   &ulOfs,
    9521125                   &strFind,
    9531126                   &strReplace,
    954                    &ulOfs))
     1127                   shift, &fRepeat));
    9551128        ;
    9561129
    9571130    printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated);
    9581131
    959     xstrcpy(&strFind, ".");
    960     xstrcpy(&strReplace, "***************************");
     1132    printf("Reserving extra mem.\n");
     1133
     1134    xstrReserve(&str, 6000);
     1135    printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated);
     1136
     1137    xstrcpy(&strFind, "..........", 0);
     1138    xstrcpy(&strReplace, "@", 0);
     1139
     1140    printf("Replacing \"%s\" with \"%s\".\n", strFind.psz, strReplace.psz);
     1141
     1142    fRepeat = FALSE;
    9611143    ulOfs = 0;
    962     while (xstrrpl(&str,
    963                    ulOfs,
     1144    while (xstrFindReplace(&str,
     1145                   &ulOfs,
    9641146                   &strFind,
    9651147                   &strReplace,
    966                    &ulOfs))
     1148                   shift, &fRepeat));
    9671149        ;
    9681150
    9691151    printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated);
    9701152
    971     xstrcpy(&strFind, "*");
    972     xstrClear(&strReplace);
    973     ulOfs = 0;
    974     while (xstrrpl(&str,
    975                    ulOfs,
    976                    &strFind,
    977                    &strReplace,
    978                    &ulOfs))
    979         ;
    980 
    981     printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated);
    982 } */
    983 
     1153    return (0);
     1154}
     1155*/
     1156
     1157
Note: See TracChangeset for help on using the changeset viewer.