Changeset 426


Ignore:
Timestamp:
Sep 26, 2016, 1:45:54 PM (9 years ago)
Author:
pr
Message:

Fix quoting and allow escaps. Bug 1244.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/helpers/stringh.c

    r335 r426  
    2323
    2424/*
    25  *      Copyright (C) 1997-2006 Ulrich M”ller.
     25 *      Copyright (C) 1997-2016 Ulrich M”ller.
    2626 *      Parts Copyright (C) 1991-1999 iMatix Corporation.
    2727 *      This file is part of the "XWorkplace helpers" source package.
     
    13221322 *@@changed WarpIN V1.0.11 (2006-08-29) [pr]: handle attrib names in quoted strings @@fixes 718
    13231323 *@@changed WarpIN V1.0.12 (2006-09-07) [pr]: fix attrib handling again @@fixes 718 @@fixes 836
     1324 *@@changed WarpIN V1.0.23 (2016-09-23) [pr]: fix single quote handling and allow escaping @@fixes 1244
    13241325 */
    13251326
     
    13311332           ulLength = strlen(pszSearchIn);
    13321333    BOOL   fInQuote = FALSE;
     1334    CHAR   cQuote = '\0', cPrev = '\0';
    13331335
    13341336    // use alloca(), so memory is freed on function exit
     
    13391341    for (p = pszSearchIn2;   *p == '\'' || *p == '"'  || *p == ' '
    13401342                          || *p == '\n' || *p == '\r' || *p == '\t'; p++);
    1341     for (pszStart = p; *p; p++)
     1343    for (pszStart = p; *p; cPrev = *(p++))
    13421344    {
    13431345        if (fInQuote)
    13441346        {
    13451347            // V1.0.12 (2006-09-07) [pr]: allow end of line to terminate a (broken) quote
    1346             if (*p == '"' || *p == '\n' || *p == '\r')
     1348            if ((*p == cQuote && cPrev != '\\') || *p == '\n' || *p == '\r')
    13471349                fInQuote = FALSE;
    13481350        }
    13491351        else
    13501352        {
    1351             if (*p == '"')
     1353            if (*p == '"' || *p == '\'')
     1354            {
    13521355                fInQuote = TRUE;
     1356                cQuote = *p;
     1357            }
    13531358            else
    13541359            {
     
    14481453 *@@added V0.9.0 [umoeller]
    14491454 *@@changed V1.0.13 (2006-09-10) [pr]: improved parsing
     1455 *@@changed WarpIN V1.0.23 (2016-09-23) [pr]: allow escaping @@fixes 1244
    14501456 */
    14511457
     
    14631469    {
    14641470        // determine end character to search for: a space
    1465         CHAR cEnd = ' ';
     1471        CHAR cEnd = ' ', cPrev = '\0';
    14661472        // V1.0.3 (2004-11-10) [pr]: @@fixes 461
    14671473        // V1.0.13 (2006-09-10) [pr]: optimized
    1468         if ((*pParam == '\"') || (*pParam == '\''))
     1474        if ((*pParam == '"') || (*pParam == '\''))
    14691475        {
    14701476            // or, if the data is enclosed in quotes, a quote or single quote
     
    14841490            if (   (   (cEnd == ' ')
    14851491                    && ((*pParam == ' ') || (*pParam == '\r') || (*pParam == '\n')))
    1486                 || (*pParam == cEnd)
     1492                || ((*pParam == cEnd) && (cPrev != '\\'))
    14871493               )
    14881494                // end character found
     
    14991505                    break;
    15001506            }
     1507
    15011508            ulCount++;
    1502             pParam++;
    1503         }
    1504 
    1505         // copy attribute to new buffer
    1506         if (ulCount)
    1507         {
    1508             prc = (PSZ)malloc(ulCount+1);
    1509             memcpy(prc, pParam2, ulCount);
    1510             *(prc+ulCount) = 0;
     1509            cPrev = *(pParam++);
     1510        }
     1511
     1512        // copy attribute to new buffer, de-escaping if necessary
     1513        if (ulCount && (prc = (PSZ) malloc(ulCount+1)))
     1514        {
     1515            ULONG i = 0, j = 0;
     1516
     1517            for(cPrev = '\0'; i < ulCount; ++i, ++j)
     1518            {
     1519                if (cEnd != ' ' && pParam2[i] == cEnd && cPrev == '\\')
     1520                    --j;
     1521
     1522                cPrev = prc[j] = pParam2[i];
     1523            }
     1524
     1525            prc[j] = '\0';
    15111526        }
    15121527    }
Note: See TracChangeset for help on using the changeset viewer.