# 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 "table/control_codes.h"`
`#include <stdarg.h>`
`#include <ctype.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: last instead of size, i.e.
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).
void | str_strip_colours (char *str)
| Scans the string for colour codes and strips them.
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: last instead of size, i.e.
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)
size_t | Utf8Encode (char *buf, WChar c)
size_t | Utf8TrimString (char *s, size_t maxlen)
| Properly terminate an UTF8 string to some maximum length.
char * | strndup (const char *s, size_t len)
char * | strcasestr (const char *haystack, const char *needle)

## 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 194 of file string.cpp.

References CS_ALPHA, CS_ALPHANUMERAL, CS_NUMERAL, and CS_NUMERAL_SPACE.

Referenced by IConsoleCmdExec().

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

Convert the md5sum to a hexadecimal string representation.

Convert the md5sum number to a 'hexadecimal' string, return next pos in buffer.

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 263 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:

- 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 247 of file string.cpp.

References vseprintf().

Referenced by CheckExternalFiles(), CloneVehicleName(), CrashLogOSX::DisplayCrashDialog(), CrashLog::FillCrashLog(), FormatBytes(), CrashLog::GamelogFillCrashLog(), BaseMedia< Tbase_set >::GetSetsList(), HandleSavegameLoadCrash(), ini_save_settings(), 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(), make_intlist(), make_manyofmany(), make_oneofmany(), md5sumToString(), ShowHelp(), CrashLog::WriteCrashLog(), and CrashLog::WriteSavegame().

void str_validate | ( | char * | str,
|  | const char * | last,
|  | bool | allow_newlines = `false`,
|  | bool | ignore = `false` |
| ) |  |  |

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
  | allow_newlines | whether newlines should be allowed or ignored
  | ignore | whether to ignore or replace with a question mark

Definition at line 107 of file string.cpp.

References Utf8EncodedCharLen().

Referenced by FiosFileScanner::AddFile(), FiosGetFileList(), GetFileTitle(), IConsolePrint(), 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.

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 62 of file string.cpp.

References strecpy().

Referenced by CloneVehicleName(), DoAutosave(), DrawRoadVehDetails(), GetDebugString(), GetFileTitle(), 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.

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 74 of file string.cpp.

References error().

Referenced by FiosFileScanner::AddFile(), CloneVehicleName(), DrawString(), DrawStringMultiLine(), MusicSet::FillSetDetails(), FioFOpenFile(), FiosGetFileList(), GamelogRevision(), GenerateTownNameString(), GetFileTitle(), GetKeyboardLayout(), IConsoleCopyInParams(), ini_save_settings(), MakeAustrianTownName(), MakeCatalanTownName(), MakeCzechTownName(), MakeDanishTownName(), MakeDutchTownName(), MakeEnglishAdditionalTownName(), MakeEnglishOriginalTownName(), MakeFinnishTownName(), MakeFrenchTownName(), MakeGermanTownName(), MakeHungarianTownName(), MakeItalianTownName(), MakeNorwegianTownName(), MakePolishTownName(), MakeRomanianTownName(), MakeScreenshot(), MakeSillyTownName(), MakeSlovakTownName(), MakeSpanishTownName(), MakeSwedishTownName(), MakeSwissTownName(), MakeTurkishTownName(), IniFile::SaveToDisk(), FileScanner::Scan(), ShowHelp(), ShowRefitOptionsList(), strecat(), TarListAddFile(), and CrashLog::WriteScreenshot().

void strtolower | ( | char * | str | ) |

Convert a given ASCII string to lowercase.

Convert the given string to lowercase, only works with ASCII!

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

Definition at line 182 of file string.cpp.

Referenced by FioFOpenFile(), and SimplifyFileName().

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.

Note:
  usage ttd_strlcat(dst, src, lengthof(dst));

  lengthof() applies only to fixed size arrays

Parameters:

  | 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 40 of file string.cpp.

References ttd_strlcpy().

Referenced by BuildWithFullPath().

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.

Note:
  usage ttd_strlcpy(dst, src, lengthof(dst));

  lengthof() applies only to fixed size arrays

Parameters:

  | dst | The destination buffer
  | src | The buffer containing the string to copy
  | size | The maximum size of the destination buffer

Definition at line 52 of file string.cpp.

Referenced by BuildWithFullPath(), FioCreateDirectory(), GetClipboardContents(), IConsoleHistoryNavigate(), IConsoleVarSetStringvalue(), ini_load_settings(), AIController::LoadedLibrary(), mkpath(), NewGRFWindow::OnClick(), SetSettingValue(), and ttd_strlcat().

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)) maxlen is the string length _INCLUDING_ the terminating ''

Definition at line 356 of file string.cpp.

References Utf8EncodedCharLen().

static int CDECL vseprintf | ( | char * | str,
|  | const char * | last,
|  | const char * | format,
|  | va_list | ap |
| ) |  |  | `[static]`

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 33 of file string.cpp.

References min().

Referenced by seprintf().