Handling of C-type strings (char*). More...
#include "stdafx.h"
#include "debug.h"
#include "core/alloc_func.hpp"
#include "core/math_func.hpp"
#include "string_func.h"
#include "table/control_codes.h"
#include <stdarg.h>
#include <ctype.h>
#include <unicode/ustring.h>
#include "language.h"
#include "gfx_func.h"
Go to the source code of this file.
Functions | |
static int CDECL | vseprintf (char *str, const char *last, const char *format, va_list ap) |
Safer implementation of vsnprintf; same as vsnprintf except:
| |
void | ttd_strlcat (char *dst, const char *src, size_t size) |
Appends characters from one string to another. | |
void | ttd_strlcpy (char *dst, const char *src, size_t size) |
Copies characters from one buffer to another. | |
char * | strecat (char *dst, const char *src, const char *last) |
Appends characters from one string to another. | |
char * | strecpy (char *dst, const char *src, const char *last) |
Copies characters from one buffer to another. | |
char *CDECL | str_fmt (const char *str,...) |
void | str_validate (char *str, const char *last, bool allow_newlines, bool ignore) |
Scans the string for valid characters and if it finds invalid ones, replaces them with a question mark '?' (if not ignored). | |
bool | StrValid (const char *str, const char *last) |
Checks whether the given string is valid, i.e. | |
void | str_strip_colours (char *str) |
Scans the string for colour codes and strips them. | |
size_t | Utf8StringLength (const char *s) |
Get the length of an UTF-8 encoded string in number of characters and thus not the number of bytes that the encoded string contains. | |
void | strtolower (char *str) |
Convert a given ASCII string to lowercase. | |
bool | IsValidChar (WChar key, CharSetFilter afilter) |
Only allow certain keys. | |
int CDECL | seprintf (char *str, const char *last, const char *format,...) |
Safer implementation of snprintf; same as snprintf except:
| |
char * | md5sumToString (char *buf, const char *last, const uint8 md5sum[16]) |
Convert the md5sum to a hexadecimal string representation. | |
size_t | Utf8Decode (WChar *c, const char *s) |
Decode and consume the next UTF-8 encoded character. | |
size_t | Utf8Encode (char *buf, WChar c) |
Encode a unicode character and place it in the buffer. | |
size_t | Utf8TrimString (char *s, size_t maxlen) |
Properly terminate an UTF8 string to some maximum length. | |
int | strnatcmp (const char *s1, const char *s2) |
Compares two strings using case insensitive natural sort. |
Handling of C-type strings (char*).
Definition in file string.cpp.
bool IsValidChar | ( | WChar | key, | |
CharSetFilter | afilter | |||
) |
Only allow certain keys.
You can define the filter to be used. This makes sure no invalid keys can get into an editbox, like BELL.
key | character to be checked | |
afilter | the filter to use |
Definition at line 331 of file string.cpp.
References CS_ALPHA, CS_ALPHANUMERAL, CS_HEXADECIMAL, CS_NUMERAL, and CS_NUMERAL_SPACE.
Referenced by IConsoleCmdExec(), OskWindow::OnClick(), IConsoleWindow::OnKeyPress(), TranslateTTDPatchCodes(), and OskWindow::UpdateOskState().
char* md5sumToString | ( | char * | buf, | |
const char * | last, | |||
const uint8 | md5sum[16] | |||
) |
Convert the md5sum to a hexadecimal string representation.
buf | buffer to put the md5sum into | |
last | last character of buffer (usually lastof(buf)) | |
md5sum | the md5sum itself |
Definition at line 423 of file string.cpp.
References seprintf().
Referenced by HandleSavegameLoadCrash(), IsGoodGRFConfigList(), and PrintGrfInfo().
int CDECL seprintf | ( | char * | str, | |
const char * | last, | |||
const char * | format, | |||
... | ||||
) |
Safer implementation of snprintf; same as snprintf except:
replace sizeof with lastof.
str | buffer to write to up to last | |
last | last character we may write to | |
format | the formatting (see snprintf) |
Definition at line 405 of file string.cpp.
References vseprintf().
Referenced by AI_CreateAIDummy(), CheckExternalFiles(), CloneVehicleName(), CrashLogOSX::DisplayCrashDialog(), NetworkContentListWindow::DrawDetails(), AIInfo::DummyConstructor(), CrashLog::FillCrashLog(), FormatBytes(), FormatNumber(), CrashLog::GamelogFillCrashLog(), NetworkAddress::GetAddressAsString(), AIScanner::GetAIConsoleLibraryList(), AIScanner::GetAIConsoleList(), DriverFactoryBase::GetDriversInfo(), GetFiosItem(), GetLanguageList(), BaseMedia< Tbase_set >::GetSetsList(), GRFBuildParamList(), HandleSavegameLoadCrash(), IniSaveSettings(), IsSameAI(), AIInstance::LoadCompatibilityScripts(), CrashLog::LogCompiler(), CrashLog::LogConfiguration(), CrashLogWindows::LogError(), CrashLogUnix::LogError(), CrashLogOSX::LogError(), CrashLog::LogGamelog(), CrashLog::LogLibraries(), CrashLogWindows::LogModules(), CrashLog::LogOpenTTDVersion(), CrashLogWindows::LogOSVersion(), CrashLogUnix::LogOSVersion(), CrashLogOSX::LogOSVersion(), CrashLogWindows::LogRegisters(), CrashLogWindows::LogStacktrace(), CrashLogUnix::LogStacktrace(), CrashLogOSX::LogStacktrace(), MakeIntList(), MakeManyOfMany(), MakeOneOfMany(), md5sumToString(), NetworkHTTPSocketHandler::NetworkHTTPSocketHandler(), NetworkAddress::Resolve(), SetFallbackFont(), ShowErrorMessage(), ShowHelp(), CrashLog::WriteCrashLog(), and CrashLog::WriteSavegame().
void str_validate | ( | char * | str, | |
const char * | last, | |||
bool | allow_newlines, | |||
bool | ignore | |||
) |
Scans the string for valid characters and if it finds invalid ones, replaces them with a question mark '?' (if not ignored).
str | the string to validate | |
last | the last valid character of str | |
allow_newlines | whether newlines should be allowed or ignored | |
ignore | whether to ignore or replace with a question mark |
Definition at line 186 of file string.cpp.
References Utf8Decode(), and Utf8EncodedCharLen().
Referenced by FiosFileScanner::AddFile(), FiosGetFileList(), GetFileTitle(), IConsolePrint(), Packet::Recv_string(), and SlString().
char* strecat | ( | char * | dst, | |
const char * | src, | |||
const char * | last | |||
) |
Appends characters from one string to another.
Appends the source string to the destination string with respect of the terminating null-character and and the last pointer to the last element in the destination buffer. If the last pointer is set to NULL no boundary check is performed.
dst | The buffer containing the target string | |
src | The buffer containing the string to append | |
last | The pointer to the last element of the destination buffer |
Definition at line 117 of file string.cpp.
References strecpy().
Referenced by CloneVehicleName(), DoAutosave(), DrawRoadVehDetails(), GetDebugString(), GetFileTitle(), GetFullFilename(), KeycodeToString(), SaveKeycodes(), and IniFile::SaveToDisk().
char* strecpy | ( | char * | dst, | |
const char * | src, | |||
const char * | last | |||
) |
Copies characters from one buffer to another.
Copies the source string to the destination buffer with respect of the terminating null-character and the last pointer to the last element in the destination buffer. If the last pointer is set to NULL no boundary check is performed.
dst | The destination buffer | |
src | The buffer containing the string to copy | |
last | The pointer to the last element of the destination buffer |
Definition at line 145 of file string.cpp.
References DEBUG, and error().
Referenced by ScenarioScanner::AddFile(), FiosFileScanner::AddFile(), TarScanner::AddFile(), AI_CreateAIDummy(), CloneVehicleName(), DEF_UDP_RECEIVE_COMMAND(), DrawString(), DrawStringMultiLine(), ExtractTar(), FioFOpenFile(), FiosGetFileList(), FormatNumber(), FormatString(), GamelogRevision(), GenerateTownNameString(), NetworkAddress::GetAddressAsString(), GetFileTitle(), GetKeyboardLayout(), GRFBuildParamList(), IConsoleCopyInParams(), IniSaveSettings(), IsSameAI(), MakeAustrianTownName(), MakeCatalanTownName(), MakeCzechTownName(), MakeDanishTownName(), MakeDutchTownName(), MakeEnglishAdditionalTownName(), MakeEnglishOriginalTownName(), MakeFinnishTownName(), MakeFrenchTownName(), MakeGermanTownName(), MakeHungarianTownName(), MakeItalianTownName(), MakeNorwegianTownName(), MakePolishTownName(), MakeRomanianTownName(), MakeScreenshot(), MakeSillyTownName(), MakeSlovakTownName(), MakeSpanishTownName(), MakeSwedishTownName(), MakeSwissTownName(), MakeTurkishTownName(), NetworkAddChatMessage(), NetworkAddress::NetworkAddress(), NetworkFindBroadcastIPsInternal(), NetworkGameListHandleDelayedInsert(), NetworkSendCommand(), NetworkServerChangeClientName(), NetworkServerSetCompanyPassword(), NetworkUDPQueryServerThread(), SaveLoadWindow::OnClick(), NetworkContentDownloadStatusWindow::OnDownloadProgress(), NetworkStartServerWindow::OnKeyPress(), NetworkGameWindow::OnKeyPress(), AIDebugWindow::OnKeyPress(), CustomCurrencyWindow::OnQueryTextFinished(), NetworkStartServerWindow::OnQueryTextFinished(), ClientNetworkContentSocketHandler::OnReceiveData(), DriverFactoryBase::RegisterDriver(), NetworkAddress::Resolve(), IniFile::SaveToDisk(), FileScanner::Scan(), DriverFactoryBase::SelectDriver(), SetFallbackFont(), SignListWindow::SetFilterString(), ShowHelp(), ShowRefitOptionsList(), strecat(), CrashLog::WriteScreenshot(), and DriverFactoryBase::~DriverFactoryBase().
int strnatcmp | ( | const char * | s1, | |
const char * | s2 | |||
) |
Compares two strings using case insensitive natural sort.
s1 | First string to compare. | |
s2 | Second string to compare. |
Definition at line 572 of file string.cpp.
References _current_collator, DRAW_STRING_BUFFER, and lengthof.
Referenced by CargoSpecNameSorter(), EngineNameSorter(), VehicleGroupWindow::GroupNameSorter(), IndustryDirectoryWindow::IndustryNameSorter(), IndustryTypeNameSorter(), NewGRFWindow::NameSorter(), NetworkContentListWindow::NameSorter(), NetworkGameWindow::NGameNameSorter(), SignList::SignNameSorter(), TownDirectoryWindow::TownNameSorter(), NetworkContentListWindow::TypeSorter(), and VehicleNameSorter().
void strtolower | ( | char * | str | ) |
Convert a given ASCII string to lowercase.
NOTE: only support ASCII characters, no UTF8 fancy. As currently the function is only used to lowercase data-filenames if they are not found, this is sufficient. If more, or general functionality is needed, look to r7271 where it was removed because it was broken when using certain locales: eg in Turkish the uppercase 'I' was converted to '?', so just revert to the old functionality
str | string to convert |
Definition at line 319 of file string.cpp.
Referenced by AIScanner::FindInfo(), FioFOpenFile(), AIScanner::ImportLibrary(), AIScanner::RegisterAI(), AIScanner::RegisterLibrary(), and SimplifyFileName().
bool StrValid | ( | const char * | str, | |
const char * | last | |||
) |
Checks whether the given string is valid, i.e.
contains only valid (printable) characters and is properly terminated.
str | The string to validate. | |
last | The last character of the string, i.e. the string must be terminated here or earlier. |
Definition at line 247 of file string.cpp.
References Utf8Decode(), and Utf8EncodedCharLen().
Referenced by LanguagePackHeader::IsValid().
void ttd_strlcat | ( | char * | dst, | |
const char * | src, | |||
size_t | size | |||
) |
Appends characters from one string to another.
Appends the source string to the destination string with respect of the terminating null-character and the maximum size of the destination buffer.
dst | The buffer containing the target string | |
src | The buffer containing the string to append | |
size | The maximum size of the destination buffer |
Definition at line 65 of file string.cpp.
References ttd_strlcpy().
Referenced by BuildWithFullPath(), AIInstance::LoadCompatibilityScripts(), and ScriptScanner::ScanDir().
void ttd_strlcpy | ( | char * | dst, | |
const char * | src, | |||
size_t | size | |||
) |
Copies characters from one buffer to another.
Copies the source string to the destination buffer with respect of the terminating null-character and the maximum size of the destination buffer.
dst | The destination buffer | |
src | The buffer containing the string to copy | |
size | The maximum size of the destination buffer |
Definition at line 91 of file string.cpp.
Referenced by BuildWithFullPath(), AIScanner::FindInfo(), FioCreateDirectory(), ServerNetworkGameSocketHandler::GetClientName(), GetClipboardContents(), IConsoleHistoryNavigate(), IniLoadSettings(), AIController::LoadedLibrary(), mkpath(), NewGRFWindow::OnClick(), SaveLoadWindow::OnClick(), ScriptScanner::ScanDir(), SetSettingValue(), and ttd_strlcat().
size_t Utf8Decode | ( | WChar * | c, | |
const char * | s | |||
) |
Decode and consume the next UTF-8 encoded character.
c | Buffer to place decoded character. | |
s | Character stream to retrieve character from. |
Definition at line 444 of file string.cpp.
References GB(), and HasBit().
Referenced by DrawString(), MoveTextBufferPos(), str_strip_colours(), str_validate(), StrValid(), and TranslateTTDPatchCodes().
size_t Utf8Encode | ( | char * | buf, | |
WChar | c | |||
) |
Encode a unicode character and place it in the buffer.
buf | Buffer to place character. | |
c | Unicode character to encode. |
Definition at line 484 of file string.cpp.
References GB().
Referenced by CheckForMissingGlyphsInLoadedLanguagePack(), CopyFromOldName(), DrawNewsString(), UnmappedChoiceList::Flush(), FormatString(), FS2OTTD(), InsertTextBufferChar(), and TranslateTTDPatchCodes().
size_t Utf8StringLength | ( | const char * | s | ) |
Get the length of an UTF-8 encoded string in number of characters and thus not the number of bytes that the encoded string contains.
s | The string to get the length for. |
Definition at line 300 of file string.cpp.
Referenced by AISign::BuildSign(), CmdFoundTown(), CmdPlaceSign(), CmdRenameCompany(), CmdRenameDepot(), CmdRenameEngine(), CmdRenameGroup(), CmdRenamePresident(), CmdRenameSign(), CmdRenameStation(), CmdRenameTown(), CmdRenameVehicle(), CmdRenameWaypoint(), GenerateCompanyName(), GeneratePresidentName(), AIVehicle::SetName(), AISign::SetName(), AIGroup::SetName(), AICompany::SetName(), AIBaseStation::SetName(), and VerifyTownName().
size_t Utf8TrimString | ( | char * | s, | |
size_t | maxlen | |||
) |
Properly terminate an UTF8 string to some maximum length.
s | string to check if it needs additional trimming | |
maxlen | the maximum length the buffer can have. |
Definition at line 518 of file string.cpp.
References Utf8EncodedCharLen().
Referenced by NetworkAddChatMessage().
static int CDECL vseprintf | ( | char * | str, | |
const char * | last, | |||
const char * | format, | |||
va_list | ap | |||
) | [static] |
Safer implementation of vsnprintf; same as vsnprintf except:
replace sizeof with lastof.
str | buffer to write to up to last | |
last | last character we may write to | |
format | the formatting (see snprintf) | |
ap | the list of arguments for the format |
Definition at line 44 of file string.cpp.
References min().
Referenced by seprintf().