OpenTTD: src/string.cpp File Reference

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

Data Structures | Functions
string.cpp File Reference

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 "string_base.h"`
`#include "table/control_codes.h"`
`#include <stdarg.h>`
`#include <ctype.h>`
`#include "safeguards.h"`

Go to the source code of this file.

## Data Structures
class | DefaultStringIterator
| Fallback simple string iterator. More...

## Functions
int CDECL | vseprintf (char *str, const char *last, const char *format, va_list ap)
| Safer implementation of vsnprintf; same as vsnprintf except:
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 * | stredup (const char *s, const char *last)
| Create a duplicate of the given string.
char *CDECL | str_fmt (const char *str,...)
| Format, "printf", into a newly allocated string.
void | str_fix_scc_encoded (char *str, const char *last)
| Scan the string for old values of SCC_ENCODED and fix it to it's new, static value.
void | str_validate (char *str, const char *last, StringValidationSettings settings)
| Scans the string for valid characters and if it finds invalid ones, replaces them with a question mark '?' (if not ignored)
void | ValidateString (const char *str)
| Scans the string for valid characters and if it finds invalid ones, replaces them with a question mark '?'.
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.
bool | 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.
static const char * | SkipGarbage (const char *str)
| Skip some of the 'garbage' in the string that we don't want to use to sort on.
int | strnatcmp (const char *s1, const char *s2, bool ignore_garbage_at_front)
| Compares two strings using case insensitive natural sort.

## Detailed Description

Handling of C-type strings (char*).

Definition in file string.cpp.

## Function Documentation

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.

Parameters

  key | character to be checked
  afilter | the filter to use

Returns
  true or false depending if the character is printable/valid or not

Definition at line 338 of file string.cpp.

References CS_ALPHA, CS_ALPHANUMERAL, CS_HEXADECIMAL, CS_NUMERAL, and CS_NUMERAL_SPACE.

Referenced by IConsoleCmdExec(), Textbuf::InsertString(), OskWindow::OnClick(), TranslateTTDPatchCodes(), and OskWindow::UpdateOskState().

char* md5sumToString | ( | char * | buf,
|  | const char * | last,
|  | const uint8 | md5sum[16]
| ) |  |

Convert the md5sum to a hexadecimal string representation.

Parameters

  buf | buffer to put the md5sum into
  last | last character of buffer (usually lastof(buf))
  md5sum | the md5sum itself

Returns
  a pointer to the next character after the md5sum

Definition at line 416 of file string.cpp.

References seprintf().

Referenced by HandleSavegameLoadCrash(), IsGoodGRFConfigList(), MakePNGImage(), NetworkContentListWindow::OpenExternalSearch(), OutputContentState(), PrintGrfInfo(), and ClientNetworkGameSocketHandler::Receive_SERVER_CHECK_NEWGRFS().

int CDECL seprintf | ( | char * | str,
|  | const char * | last,
|  | const char * | format,
|  |  | ...
| ) |  |

Safer implementation of snprintf; same as snprintf except:

- last instead of size, i.e. replace sizeof with lastof.
- return gives the amount of characters added, not what it would add.
  Parameters

    str | buffer to write to up to last
    last | last character we may write to
    format | the formatting (see snprintf)

  Returns
    the number of added characters

Definition at line 398 of file string.cpp.

References vseprintf().

Referenced by _GenerateWorld(), ScriptInfo::AddLabels(), ScriptInfo::AddSetting(), AppendPathSeparator(), NetworkChatWindow::ChatTabCompletion(), CheckExternalFiles(), ScriptInfo::CheckMethod(), CloneVehicleName(), CmdRenamePresident(), Squirrel::CompileError(), Squirrel::CreateClassInstanceVM(), debug_print(), DeleteGRFPresetFromConfig(), DetermineBasePaths(), DeterminePaths(), CrashLogOSX::DisplayCrashDialog(), DoAutosave(), DoLoad(), DoScanWorkingDirectory(), ClientNetworkContentSocketHandler::DownloadSelectedContentHTTP(), NetworkContentListWindow::DrawDetails(), AIInfo::DummyConstructor(), CrashLog::FillCrashLog(), GameScannerInfo::FindInfo(), AIScannerInfo::FindInfo(), GameScannerLibrary::FindLibrary(), AIScannerLibrary::FindLibrary(), FiosBrowseTo(), FiosGetFileList(), FiosMakeFilename(), FormatBytes(), FormatNumber(), FormatString(), CrashLog::GamelogFillCrashLog(), GamelogPrint(), GenerateCompanyPasswordHash(), NetworkAddress::GetAddressAsString(), BlitterFactory::GetBlittersInfo(), ServerNetworkGameSocketHandler::GetClientName(), ScriptScanner::GetConsoleList(), GetDebugString(), DriverFactoryBase::GetDriversInfo(), GetFiosItem(), GetLanguageList(), GameScannerInfo::GetScriptName(), AIScannerInfo::GetScriptName(), GameScannerLibrary::GetScriptName(), AIScannerLibrary::GetScriptName(), BaseMedia< Tbase_set >::GetSetsList(), GetTextfile(), GRFBuildParamList(), HandleSavegameLoadCrash(), IConsoleGetSetting(), IConsoleListSettings(), IniSaveSettings(), ScriptInstance::LoadCompatibilityScripts(), LoadGRFPresetFromConfig(), CrashLog::LogCompiler(), CrashLog::LogConfiguration(), CrashLog::LogGamelog(), CrashLog::LogLibraries(), CrashLog::LogOpenTTDVersion(), MakeIntList(), MakeManyOfMany(), MakeOneOfMany(), MakePNGImage(), MakeScreenshotName(), md5sumToString(), NetworkFindName(), NetworkHTTPSocketHandler::NetworkHTTPSocketHandler(), NetworkGameWindow::OnClick(), LandInfoWindow::OnInit(), NetworkContentListWindow::OpenExternalSearch(), PrintGrfInfo(), ClientNetworkGameSocketHandler::Receive_SERVER_CHAT(), ClientNetworkUDPSocketHandler::Receive_SERVER_RESPONSE(), ScriptScanner::RegisterScript(), NetworkAddress::Resolve(), Squirrel::RunError(), SaveGRFPresetToConfig(), SaveVersionInConfig(), ScanPath(), Script_CreateDummy(), Script_CreateDummyInfo(), SetFallbackFont(), ScriptConfig::SettingsToString(), ShowErrorMessage(), ShowHelp(), StateGameLoop(), CrashLog::WriteCrashLog(), and CrashLog::WriteSavegame().

static const char* SkipGarbage | ( | const char * | str | ) |
| static

Skip some of the 'garbage' in the string that we don't want to use to sort on.

This way the alphabetical sorting will work better as we would be actually using those characters instead of some other characters such as spaces and tildes at the begin of the name.

Parameters

  str | The string to skip the initial garbage of.

Returns
  The string with the garbage skipped.

Definition at line 555 of file string.cpp.

References IsInsideMM().

Referenced by strnatcmp().

void str_fix_scc_encoded | ( | char * | str,
|  | const char * | last
| ) |  |

Scan the string for old values of SCC_ENCODED and fix it to it's new, static value.

Parameters

  str | the string to scan
  last | the last valid character of str

Definition at line 158 of file string.cpp.

References Utf8Decode(), Utf8Encode(), and Utf8EncodedCharLen().

Referenced by SlString().

char* CDECL str_fmt | ( | const char * | str,
|  |  | ...
| ) |  |

Format, "printf", into a newly allocated string.

Parameters

  str | The formatting string.

Returns
  The formatted string. You must free this!

Definition at line 139 of file string.cpp.

References lastof, and vseprintf().

Referenced by DEF_CONSOLE_CMD(), DeterminePaths(), and BaseSet< T, Tnum_files, Tsearch_in_tars >::FillSetDetails().

void str_validate | ( | char * | str,
|  | const char * | last,
|  | StringValidationSettings | settings
| ) |  |

Scans the string for valid characters and if it finds invalid ones, replaces them with a question mark '?' (if not ignored)

Parameters

  str | the string to validate
  last | the last valid character of str
  settings | the settings for the string validation.

Definition at line 184 of file string.cpp.

References SVS_ALLOW_CONTROL_CODE, SVS_ALLOW_NEWLINE, SVS_REPLACE_WITH_QUESTION_MARK, Utf8Decode(), and Utf8EncodedCharLen().

Referenced by FiosFileScanner::AddFile(), FiosGetFileList(), GetFileTitle(), IConsolePrint(), IniGroup::IniGroup(), IniItem::IniItem(), IniLoadFile::LoadFromDisk(), LoadFromHighScore(), TextfileWindow::LoadTextfile(), Packet::Recv_string(), SlString(), and ValidateString().

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.

Note
  usage: strecat(dst, src, lastof(dst));
  lastof() applies only to fixed size arrays

Parameters

  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

Returns
  The pointer to the terminating null-character in the destination buffer

Definition at line 73 of file string.cpp.

References strecpy().

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.

Note
  usage: strecpy(dst, src, lastof(dst));
  lastof() applies only to fixed size arrays

Parameters

  dst | The destination buffer
  src | The buffer containing the string to copy
  last | The pointer to the last element of the destination buffer

Returns
  The pointer to the terminating null-character in the destination buffer

Definition at line 101 of file string.cpp.

References DEBUG, and error().

char* stredup | ( | const char * | s,
|  | const char * | last
| ) |  |

Create a duplicate of the given string.

Parameters

  s | The string to duplicate.
  last | The last character that is safe to duplicate. If NULL, the whole string is duplicated.

Note
  The maximum length of the resulting string might therefore be last - s + 1.

Returns
  The duplicate of the string.

Definition at line 126 of file string.cpp.

References ttd_strnlen().

Referenced by ScriptScanner::AddFile(), TarScanner::AddFile(), ScriptInfo::AddLabels(), ScriptInfo::AddSetting(), Subsidy::AwardTo(), ScriptConfig::Change(), NetworkChatWindow::ChatTabCompletion(), CheckForMissingGlyphs(), CloneVehicleName(), CmdAlterGroup(), CmdCreateGoal(), CmdCreateStoryPage(), CmdCustomNewsItem(), CmdFoundTown(), CmdPlaceSign(), CmdRenameCompany(), CmdRenameDepot(), CmdRenameEngine(), CmdRenamePresident(), CmdRenameSign(), CmdRenameStation(), CmdRenameTown(), CmdRenameVehicle(), CmdRenameWaypoint(), CmdSetGoalProgress(), CmdSetGoalText(), CmdSetStoryPageTitle(), CmdTownSetText(), AIInfo::Constructor(), ScriptInfo::Constructor(), BaseConsist::CopyConsistPropertiesFrom(), CopyFromOldName(), CopyOutDParam(), CopyTempEngineData(), DetermineBasePaths(), DeterminePaths(), DisableStaticNewGRFInfluencingNonStaticNewGRFs(), DriverFactoryBase::DriverFactoryBase(), AIInfo::DummyConstructor(), ErrorMessageData::ErrorMessageData(), FileWriter::FileWriter(), BaseSet< T, Tnum_files, Tsearch_in_tars >::FillSetDetails(), FioOpenFile(), UnmappedChoiceList::Flush(), FormatString(), GamelogSetting(), GetFontByFaceName(), GetGRFPresetList(), IniLoadFile::GetGroup(), GRFConfig::GRFConfig(), GRFError::GRFError(), GRFFile::GRFFile(), IConsoleAliasRegister(), IConsoleCmdRegister(), IConsoleHistoryAdd(), IConsolePrint(), IniGroup::IniGroup(), IniItem::IniItem(), IniLoadSettingList(), IniLoadSettings(), IniSaveSettings(), IsGoodGRFConfigList(), LanguageStrings::LanguageStrings(), IniLoadFile::LoadFromDisk(), NetworkServerKickOrBanIP(), AfterNewGRFScan::OnNewGRFsScanned(), openttd_main(), ReadRawLanguageStrings(), ScriptScanner::RegisterScript(), ScriptConfig::ScriptConfig(), ErrorMessageData::SetDParamStr(), GameOptionsWindow::SetMediaSet(), AIConfig::SetSetting(), ScriptConfig::SetSetting(), SetSettingValue(), IniItem::SetValue(), SlError(), MusicDriver_ExtMidi::Start(), ScriptConfig::StringToSettings(), TranslateGRFStrings(), UpdateElement(), ScanProgressWindow::UpdateNewGRFScanStatus(), UpdateOSKOriginalText(), and StringNameWriter::WriteStringID().

int strnatcmp | ( | const char * | s1,
|  | const char * | s2,
|  | bool | ignore_garbage_at_front
| ) |  |

Compares two strings using case insensitive natural sort.

Parameters

  s1 | First string to compare.
  s2 | Second string to compare.
  ignore_garbage_at_front | Skip punctuation characters in the front

Returns
  Less than zero if s1 < s2, zero if s1 == s2, greater than zero if s1 > s2.

Definition at line 569 of file string.cpp.

References SkipGarbage().

Referenced by CargoSpecNameSorter(), EngineNameSorter(), GRFSorter(), VehicleGroupWindow::GroupNameSorter(), IndustryDirectoryWindow::IndustryNameSorter(), IndustryTypeNameSorter(), NetworkContentListWindow::NameSorter(), NewGRFWindow::NameSorter(), DropDownListStringItem::NatSortFunc(), NetworkGameWindow::NGameNameSorter(), SignList::SignNameSorter(), CompanyStationsWindow::StationNameSorter(), TownDirectoryWindow::TownNameSorter(), NetworkContentListWindow::TypeSorter(), and VehicleNameSorter().

bool 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

Parameters

  str | string to convert

Returns
  String has changed.

Definition at line 320 of file string.cpp.

Referenced by GameScannerInfo::FindInfo(), AIScannerInfo::FindInfo(), GameScannerLibrary::FindLibrary(), AIScannerLibrary::FindLibrary(), FioFindFullPath(), FioFOpenFile(), FioOpenFile(), ScriptScanner::RegisterScript(), 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.

Parameters

  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().

size_t Utf8Decode | ( | WChar * | c,
|  | const char * | s
| ) |  |

Decode and consume the next UTF-8 encoded character.

Parameters

  c | Buffer to place decoded character.
  s | Character stream to retrieve character from.

Returns
  Number of characters in the sequence.

Definition at line 437 of file string.cpp.

References GB(), and HasBit().

Referenced by Textbuf::DeleteChar(), FormatString(), Layouter::GetCharPosition(), GlobalVarChangeInfo(), Layouter::Layouter(), DefaultStringIterator::Next(), DefaultStringIterator::Prev(), StringFilter::SetFilterTerm(), str_fix_scc_encoded(), 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.

Parameters

  buf | Buffer to place character.
  c | Unicode character to encode.

Returns
  Number of characters in the encoded sequence.

Definition at line 477 of file string.cpp.

References GB().

Referenced by CheckForMissingGlyphs(), CopyFromOldName(), DrawNewsString(), UnmappedChoiceList::Flush(), FormatString(), Textbuf::InsertChar(), str_fix_scc_encoded(), 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.

Parameters

  s | The string to get the length for.

Returns
  The length of the string in characters.

Definition at line 300 of file string.cpp.

Referenced by CmdAlterGroup(), CmdFoundTown(), CmdPlaceSign(), CmdRenameCompany(), CmdRenameDepot(), CmdRenameEngine(), CmdRenamePresident(), CmdRenameSign(), CmdRenameStation(), CmdRenameTown(), CmdRenameVehicle(), CmdRenameWaypoint(), GenerateCompanyName(), GeneratePresidentName(), and VerifyTownName().

size_t Utf8TrimString | ( | char * | s,
|  | size_t | maxlen
| ) |  |

Properly terminate an UTF8 string to some maximum length.

Parameters

  s | string to check if it needs additional trimming
  maxlen | the maximum length the buffer can have.

Returns
  the new length in bytes of the string (eg. strlen(new_string))

Note
  maxlen is the string length INCLUDING the terminating '\0'

Definition at line 511 of file string.cpp.

References Utf8EncodedCharLen().

Referenced by NetworkAddChatMessage().

void ValidateString | ( | const char * | str | ) |

Scans the string for valid characters and if it finds invalid ones, replaces them with a question mark '?'.

Parameters

  str | the string to validate

Definition at line 233 of file string.cpp.

References str_validate().

Referenced by ScriptInfo::AddLabels(), ScriptInfo::AddSetting(), and DetermineBasePaths().

int CDECL vseprintf | ( | char * | str,
|  | const char * | last,
|  | const char * | format,
|  | va_list | ap
| ) |  |

Safer implementation of vsnprintf; same as vsnprintf except:

- last instead of size, i.e. replace sizeof with lastof.
- return gives the amount of characters added, not what it would add.
  Parameters

    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

  Returns
    the number of added characters

Definition at line 50 of file string.cpp.

References min().

Referenced by CStrA::AddFormatL(), debug(), error(), Squirrel::ErrorPrintFunc(), grfmsg(), IConsolePrintF(), MidiSendCommand(), NetworkAddChatMessage(), Textbuf::Print(), Squirrel::PrintFunc(), seprintf(), ShowInfoF(), str_fmt(), and usererror().