Beispiel #1
0
//! Converts escape sequences to the corresponding characters
void char_constants< char >::translate_escape_sequences(string_type& str)
{
    using namespace std; // to make sure we can use C functions unqualified

    string_type::iterator it = str.begin();
    while (it != str.end())
    {
        it = std::find(it, str.end(), '\\');
        if (std::distance(it, str.end()) >= 2)
        {
            it = str.erase(it);
            switch (*it)
            {
            case 'n':
                *it = '\n'; break;
            case 'r':
                *it = '\r'; break;
            case 'a':
                *it = '\a'; break;
            case '\\':
                ++it; break;
            case 't':
                *it = '\t'; break;
            case 'b':
                *it = '\b'; break;
            case 'x':
                {
                    string_type::iterator b = it;
                    if (std::distance(++b, str.end()) >= 2)
                    {
                        char_type c1 = *b++, c2 = *b++;
                        if (isxdigit(c1) && isxdigit(c2))
                        {
                            *it++ = char_type((to_number(c1) << 4) | to_number(c2));
                            it = str.erase(it, b);
                        }
                    }
                    break;
                }
            default:
                {
                    if (*it >= '0' && *it <= '7')
                    {
                        string_type::iterator b = it;
                        int c = (*b++) - '0';
                        if (*b >= '0' && *b <= '7')
                            c = c * 8 + (*b++) - '0';
                        if (*b >= '0' && *b <= '7')
                            c = c * 8 + (*b++) - '0';

                        *it++ = char_type(c);
                        it = str.erase(it, b);
                    }
                    break;
                }
            }
        }
    }
}
Beispiel #2
0
int CCommonFnc::BYTE_ConvertFromHexStringToArray(string_type hexaString, BYTE* pArray, DWORD* pbArrayLen) {
    int         status = STAT_OK;
    DWORD       pos = 0;
    DWORD       pos2 = 0;
	string_type     hexNum;
    DWORD       num;
    BYTE*       pTempArray = NULL;
    DWORD       tempArrayPos = 0;

    // EAT SPACES
    //hexaString.TrimLeft(); hexaString.TrimRight();
	hexaString.erase(hexaString.find_last_not_of(_CONV(" ")) + 1);
	size_t startpos = hexaString.find_first_not_of(_CONV(" "));
	if (string_type::npos != startpos) {
		hexaString = hexaString.substr(startpos);
	}
    hexaString += _CONV(" ");
    hexaString.length();

    if (status == STAT_OK) {
        pTempArray = new BYTE[hexaString.length()];
        memset(pTempArray, 0, hexaString.length());

        pos = pos2 = 0;
        /*while ((pos = hexaString.Find(' ', pos2)) != -1) {
            hexNum = hexaString.Mid(pos2, pos - pos2);
            hexNum.TrimLeft(); hexNum.TrimRight();
            if (hexNum.GetLength() > 0) {
                num = strtol((LPCTSTR) hexNum, NULL, 16);
        
                if (num == 0xFF) pTempArray[tempArrayPos] = 0xFF;
                else pTempArray[tempArrayPos] = (BYTE) num & 0xFF;
                
                tempArrayPos++;
            }
            pos2 = pos + 1;
        }*/
		while ((pos = hexaString.find(' ', pos2)) != string_type::npos) {
			hexNum = hexaString.substr((pos2, pos - pos2));
			hexNum.erase(hexNum.find_last_not_of(_CONV(" ")) + 1);
			size_t startpos2 = hexNum.find_first_not_of(_CONV(" "));
			if (string_type::npos != startpos2) {
				hexNum = hexNum.substr(startpos2);
			}
			if (hexNum.length() > 0) {
				num = type_to_int((LPCTSTR)hexNum.c_str(), NULL, 16);

				if (num == 0xFF) pTempArray[tempArrayPos] = 0xFF;
				else pTempArray[tempArrayPos] = (BYTE)num & 0xFF;

				tempArrayPos++;
			}
			pos2 = pos + 1;
		}

        if (tempArrayPos > *pbArrayLen) {
            status = STAT_NOT_ENOUGHT_MEMORY;
        }  
        else {
            memcpy(pArray, pTempArray, tempArrayPos);
        }
        *pbArrayLen = tempArrayPos;

        if (pTempArray) delete[] pTempArray;
    }

    return status;
}
Beispiel #3
0
//! Converts escape sequences to the corresponding characters
void char_constants< wchar_t >::translate_escape_sequences(string_type& str)
{
    using namespace std; // to make sure we can use C functions unqualified

    string_type::iterator it = str.begin();
    while (it != str.end())
    {
        it = std::find(it, str.end(), L'\\');
        if (std::distance(it, str.end()) >= 2)
        {
            it = str.erase(it);
            switch (*it)
            {
            case L'n':
                *it = L'\n'; break;
            case L'r':
                *it = L'\r'; break;
            case L'a':
                *it = L'\a'; break;
            case L'\\':
                ++it; break;
            case L't':
                *it = L'\t'; break;
            case L'b':
                *it = L'\b'; break;
            case L'x':
                {
                    string_type::iterator b = it;
                    if (std::distance(++b, str.end()) >= 2)
                    {
                        char_type c1 = *b++, c2 = *b++;
                        if (iswxdigit(c1) && iswxdigit(c2))
                        {
                            *it++ = char_type((to_number(c1) << 4) | to_number(c2));
                            it = str.erase(it, b);
                        }
                    }
                    break;
                }
            case L'u':
                {
                    string_type::iterator b = it;
                    if (std::distance(++b, str.end()) >= 4)
                    {
                        char_type c1 = *b++, c2 = *b++, c3 = *b++, c4 = *b++;
                        if (iswxdigit(c1) && iswxdigit(c2) && iswxdigit(c3) && iswxdigit(c4))
                        {
                            *it++ = char_type(
                                (to_number(c1) << 12) |
                                (to_number(c2) << 8) |
                                (to_number(c3) << 4) |
                                to_number(c4));
                            it = str.erase(it, b);
                        }
                    }
                    break;
                }
            case L'U':
                {
                    string_type::iterator b = it;
                    if (std::distance(++b, str.end()) >= 8)
                    {
                        char_type c1 = *b++, c2 = *b++, c3 = *b++, c4 = *b++;
                        char_type c5 = *b++, c6 = *b++, c7 = *b++, c8 = *b++;
                        if (iswxdigit(c1) && iswxdigit(c2) && iswxdigit(c3) && iswxdigit(c4) &&
                            iswxdigit(c5) && iswxdigit(c6) && iswxdigit(c7) && iswxdigit(c8))
                        {
                            *it++ = char_type(
                                (to_number(c1) << 28) |
                                (to_number(c2) << 24) |
                                (to_number(c3) << 20) |
                                (to_number(c4) << 16) |
                                (to_number(c5) << 12) |
                                (to_number(c6) << 8) |
                                (to_number(c7) << 4) |
                                to_number(c8));
                            it = str.erase(it, b);
                        }
                    }
                    break;
                }
            default:
                {
                    if (*it >= L'0' && *it <= L'7')
                    {
                        string_type::iterator b = it;
                        int c = (*b++) - L'0';
                        if (*b >= L'0' && *b <= L'7')
                            c = c * 8 + (*b++) - L'0';
                        if (*b >= L'0' && *b <= L'7')
                            c = c * 8 + (*b++) - L'0';

                        *it++ = char_type(c);
                        it = str.erase(it, b);
                    }
                    break;
                }
            }
        }
    }
}
void expand_escapes(string_type& s)
{
   for(unsigned int i = 0; i < s.size(); ++i)
   {
      if(s[i] == BOOST_RE_STR('\\'))
      {
         switch(s[i+1])
         {
         case BOOST_RE_STR('a'):
            s.erase(s.begin() + i);
            s[i] = BOOST_RE_STR('\a');
            break;
         case BOOST_RE_STR('b'):
            s.erase(s.begin() + i);
            s[i] = BOOST_RE_STR('\b');
            break;
         case BOOST_RE_STR('f'):
            s.erase(s.begin() + i);
            s[i] = BOOST_RE_STR('\f');
            break;
         case BOOST_RE_STR('n'):
            s.erase(s.begin() + i);
            s[i] = BOOST_RE_STR('\n');
            break;
         case BOOST_RE_STR('r'):
            s.erase(s.begin() + i);
            s[i] = BOOST_RE_STR('\r');
            break;
         case BOOST_RE_STR('t'):
            s.erase(s.begin() + i);
            s[i] = BOOST_RE_STR('\t');
            break;
         case BOOST_RE_STR('v'):
            s.erase(s.begin() + i);
            s[i] = BOOST_RE_STR('\v');
            break;
         default:
            if( (s[i + 1] >= BOOST_RE_STR('0')) && (s[i + 1] <= BOOST_RE_STR('9')) )
            {
               int val = 0;
               unsigned int pos = i;
               ++i;
               while((i < s.size()) && (s[i] >= BOOST_RE_STR('0')) && (s[i] <= BOOST_RE_STR('9')))
               {
                  val *= 10;
                  val += s[i] - BOOST_RE_STR('0');
                  ++i;
               }
               s.erase(s.begin() + pos, s.begin() + i);
               if(0 == val)
               {
                  s.insert(s.begin()+pos, ' ');
                  s[pos] = 0;
               }
               else
                  s.insert(s.begin() + pos, (string_type::value_type)val);
               i = pos;
            }
            else
            {
               s.erase(s.begin() + i);
            }
         }
      }
   }
}