Changeset 1025


Ignore:
Timestamp:
08/13/20 15:11:55 (4 years ago)
Author:
Maciej Komosinski
Message:

Added a repr() function that converts a string using only printable characters or ascii codes when necessary

Location:
cpp/common
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • cpp/common/util-string.cpp

    r1016 r1025  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2019  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2020  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    1515#include <android/log.h> //only needed to print error messages related to a workaround for Android bug
    1616#endif
     17
     18string repr(const char *str)
     19{
     20        string out = "";
     21        char hex[4];
     22        while (*str)
     23        {
     24                if (*str >= 32 && *str < 128)
     25                        out += *str;
     26                else
     27                {
     28                        if (*str == 10) out += "\\n"; else
     29                                if (*str == 13) out += "\\r"; else
     30                                {
     31                                        sprintf(hex, "%X", *str);
     32                                        out += "\\x";
     33                                        out += hex;
     34                                }
     35                }
     36                str++;
     37        }
     38        return out;
     39}
     40
    1741
    1842string ssprintf_va(const char* format, va_list ap)
     
    3155#endif
    3256
    33         while (1)
     57        while (true)
    3458        {
    3559                buf = (char*)malloc(size);
  • cpp/common/util-string.h

    r1016 r1025  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2020  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    88#include "nonstd_stl.h"
    99#include <stdarg.h>
     10
     11string repr(const char *str);
    1012
    1113char* strmove(char *a, char *b); //strcpy that works well for overlapping strings ("Source and destination overlap")
Note: See TracChangeset for help on using the changeset viewer.