Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Apr 14, 2008, 12:48:25 AM (16 years ago)
Author:
landauf
Message:

config-value accepts now strings with special chars like \n or \t and hopefully every other. strings are enclosed by "…", but the quotes are only visible in the config-file. if you want something like " ← whitespace" you must add quotes, otherwise this would be stripped to "← whitespace".

Location:
code/branches/core2/src/util
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/core2/src/util/String.cc

    r1020 r1049  
    2727
    2828#include <cctype>
    29 
     29#include <iostream>
    3030#include "String.h"
    3131
     
    9292
    9393    return quote;
     94}
     95
     96/**
     97    @brief Returns true if pos is between two quotes.
     98    @param str The string
     99    @param pos The position to check
     100    @return True if pos is between two quotes
     101*/
     102bool isBetweenQuotes(const std::string& str, unsigned int pos)
     103{
     104    if (pos == std::string::npos)
     105        return false;
     106
     107    unsigned int quotecount = 0;
     108    unsigned int quote = 0;
     109    while ((quote = getNextQuote(str, quote)) < pos)
     110    {
     111        quotecount++;
     112    }
     113
     114    if (quote == std::string::npos)
     115        return false;
     116
     117    return ((quotecount % 2) == 1);
    94118}
    95119
     
    124148    @brief Removes enclosing quotes if available.
    125149    @brief str The string to strip
    126 */
    127 void stripEnclosingQuotes(std::string* str)
     150    @return The string with removed quotes
     151*/
     152std::string stripEnclosingQuotes(const std::string& str)
    128153{
    129154    unsigned int start = std::string::npos;
    130155    unsigned int end = 0;
    131156
    132     for (unsigned int pos = 0; (pos < (*str).size()) && (pos < std::string::npos); pos++)
    133     {
    134         if ((*str)[pos] == '"')
     157    for (unsigned int pos = 0; (pos < str.size()) && (pos < std::string::npos); pos++)
     158    {
     159        if (str[pos] == '"')
    135160        {
    136161            start = pos;
     
    138163        }
    139164
    140         if (((*str)[pos] != ' ') && ((*str)[pos] != '\t') && ((*str)[pos] != '\n'))
    141             return;
    142     }
    143 
    144     for (unsigned int pos = (*str).size() - 1; pos < std::string::npos; pos--)
    145     {
    146         if ((*str)[pos] == '"')
     165        if ((str[pos] != ' ') && (str[pos] != '\t') && (str[pos] != '\n'))
     166            return str;
     167    }
     168
     169    for (unsigned int pos = str.size() - 1; pos < std::string::npos; pos--)
     170    {
     171        if (str[pos] == '"')
    147172        {
    148173            end = pos;
     
    150175        }
    151176
    152         if (((*str)[pos] != ' ') && ((*str)[pos] != '\t') && ((*str)[pos] != '\n'))
    153             return;
     177        if ((str[pos] != ' ') && (str[pos] != '\t') && (str[pos] != '\n'))
     178            return str;
    154179    }
    155180
    156181    if ((start != std::string::npos) && (end != 0))
    157         (*str) = (*str).substr(start + 1, end - start - 1);
    158 }
    159 
    160 /**
    161     @brief Returns a copy of the string with removed enclosing quotes (if available).
    162     @brief str The string to strip
    163     @return The striped copy of the string
    164 */
    165 std::string getStrippedEnclosingQuotes(const std::string& str)
    166 {
    167     std::string output = std::string(str);
    168     stripEnclosingQuotes(&output);
    169     return output;
     182        return str.substr(start + 1, end - start - 1);
     183    else
     184        return str;
    170185}
    171186
     
    235250}
    236251
     252std::string addSlashes(const std::string& str)
     253{
     254    std::string output = str;
     255
     256    for (unsigned int pos = 0; (pos = output.find('\\', pos)) < std::string::npos; pos += 2) { output.replace(pos, 2, "\\\\"); }
     257    for (unsigned int pos = 0; (pos = output.find('\n', pos)) < std::string::npos; pos += 2) { output.replace(pos, 2, "\\n"); }
     258    for (unsigned int pos = 0; (pos = output.find('\t', pos)) < std::string::npos; pos += 2) { output.replace(pos, 2, "\\t"); }
     259    for (unsigned int pos = 0; (pos = output.find('\v', pos)) < std::string::npos; pos += 2) { output.replace(pos, 2, "\\v"); }
     260    for (unsigned int pos = 0; (pos = output.find('\b', pos)) < std::string::npos; pos += 2) { output.replace(pos, 2, "\\b"); }
     261    for (unsigned int pos = 0; (pos = output.find('\r', pos)) < std::string::npos; pos += 2) { output.replace(pos, 2, "\\r"); }
     262    for (unsigned int pos = 0; (pos = output.find('\f', pos)) < std::string::npos; pos += 2) { output.replace(pos, 2, "\\f"); }
     263    for (unsigned int pos = 0; (pos = output.find('\a', pos)) < std::string::npos; pos += 2) { output.replace(pos, 2, "\\a"); }
     264    for (unsigned int pos = 0; (pos = output.find('"', pos)) < std::string::npos; pos += 2) { output.replace(pos, 2, "\\\""); }
     265    for (unsigned int pos = 0; (pos = output.find('\0', pos)) < std::string::npos; pos += 2) { output.replace(pos, 2, "\\0"); }
     266
     267    return output;
     268}
     269
     270std::string removeSlashes(const std::string& str)
     271{
     272    if (str.size() == 0)
     273        return str;
     274
     275    std::string output = "";
     276    for (unsigned int pos = 0; pos < str.size() - 1; )
     277    {
     278        if (str[pos] == '\\')
     279        {
     280            if (str[pos + 1] == '\\') { output += '\\'; pos += 2; continue; }
     281            else if (str[pos + 1] == 'n') { output += '\n'; pos += 2; continue; }
     282            else if (str[pos + 1] == 't') { output += '\t'; pos += 2; continue; }
     283            else if (str[pos + 1] == 'v') { output += '\v'; pos += 2; continue; }
     284            else if (str[pos + 1] == 'b') { output += '\b'; pos += 2; continue; }
     285            else if (str[pos + 1] == 'r') { output += '\r'; pos += 2; continue; }
     286            else if (str[pos + 1] == 'f') { output += '\f'; pos += 2; continue; }
     287            else if (str[pos + 1] == 'a') { output += '\a'; pos += 2; continue; }
     288            else if (str[pos + 1] == '"') { output += '"'; pos += 2; continue; }
     289            else if (str[pos + 1] == '0') { output += '\0'; pos += 2; continue; }
     290        }
     291        output += str[pos];
     292        pos++;
     293    }
     294    output += str[str.size() - 1];
     295
     296    return output;
     297}
     298
    237299/**
    238300    @brief Replaces each char between A and Z with its lowercase equivalent.
     
    364426unsigned int getCommentPosition(const std::string& str)
    365427{
    366     for (unsigned int i = 0; i < str.size(); i++)
     428    return getNextCommentPosition(str, 0);
     429}
     430
     431/**
     432    @brief Returns the position of the next comment-symbol, starting with start.
     433    @param str The string
     434    @param start The startposition
     435    @return The position
     436*/
     437unsigned int getNextCommentPosition(const std::string& str, unsigned int start)
     438{
     439    for (unsigned int i = start; i < str.size(); i++)
    367440        if (isComment(str.substr(i)))
    368441            return i;
  • code/branches/core2/src/util/String.h

    r1020 r1049  
    4040
    4141_UtilExport unsigned int getNextQuote(const std::string& str, unsigned int start);
     42_UtilExport bool         isBetweenQuotes(const std::string& str, unsigned int pos);
    4243
    4344_UtilExport bool         hasStringBetweenQuotes(const std::string& str);
    4445_UtilExport std::string  getStringBetweenQuotes(const std::string& str);
    4546
    46 _UtilExport void         stripEnclosingQuotes(std::string* str);
    47 _UtilExport std::string  getStrippedEnclosingQuotes(const std::string& str);
     47_UtilExport std::string  stripEnclosingQuotes(const std::string& str);
    4848
    4949_UtilExport bool         isEmpty(const std::string& str);
    5050_UtilExport bool         isComment(const std::string& str);
    5151_UtilExport bool         isNumeric(const std::string& str);
     52
     53_UtilExport std::string  addSlashes(const std::string& str);
     54_UtilExport std::string  removeSlashes(const std::string& str);
    5255
    5356_UtilExport void         lowercase(std::string* str);
     
    6366_UtilExport std::string  getComment(const std::string& str);
    6467_UtilExport unsigned int getCommentPosition(const std::string& str);
     68_UtilExport unsigned int getNextCommentPosition(const std::string& str, unsigned int start = 0);
    6569
    6670//! The Convert class has some static member functions to convert strings to values and values to strings.
Note: See TracChangeset for help on using the changeset viewer.