Changeset 1156


Ignore:
Timestamp:
10/01/21 22:46:49 (2 years ago)
Author:
Maciej Komosinski
Message:

Added sstringURLEncode(SString& target) and sstringURLDecode(SString &target)

Location:
cpp/frams/util
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/util/sstringutils.cpp

    r973 r1156  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2020  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2021  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    367367        return false;
    368368}
     369
     370bool sstringURLEncode(SString& target)
     371{
     372        const char* x = target.c_str();
     373        bool changed = 0;
     374        SString tmp;
     375        tmp.reserve(target.length());
     376        static constexpr const char* ALLOWED_CHARS = "-_.~";
     377        for (; *x; x++)
     378        {
     379                if (!(isalnum(*x) || strchr(ALLOWED_CHARS, *x)))
     380                {
     381                        tmp += "%";
     382                        tmp += SString::sprintf("%02x", *x);
     383                        changed = 1;
     384                }
     385                else
     386                        tmp += *x;
     387        }
     388        if (changed) target = tmp;
     389        return changed;
     390}
     391
     392bool sstringURLDecode(SString &target)
     393{
     394        const char* x = target.c_str();
     395        SString tmp;
     396        char *f;
     397        while (1)
     398        {
     399                f = strchr((char*)x, '%');
     400                if (f)
     401                {
     402                        tmp.append(x, f - x);
     403                        char hex[3] = { f[1],f[2],0 };
     404                        char* after;
     405                        unsigned long intvalue = strtoul(hex, &after, 16);
     406                        tmp += (char)intvalue;
     407                        x = f + 3;
     408                }
     409                else
     410                {
     411                        if (tmp.length() == 0) return false; // nothing was changed!
     412                        tmp += x;
     413                        target = tmp;
     414                        return true;
     415                }
     416        }
     417}
  • cpp/frams/util/sstringutils.h

    r973 r1156  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2020  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2021  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    2222const char* skipQuoteString(const char* txt, const char* limit);
    2323int sstringUnquote(SString &target);
     24
     25bool sstringURLEncode(SString& target);
     26bool sstringURLDecode(SString &target);
    2427
    2528int strFindField(const SString& txt, const SString& name, int &end);
Note: See TracChangeset for help on using the changeset viewer.