DATAKIT SDK  V2026.1
dtk_string.hpp
Go to the documentation of this file.
1 #ifdef A93938D6857511E1B78E58294824019B
2 # include "util/external/dtk_string_alt.hpp"
3 #else
4 
5 #ifndef __DTK_STRING_HPP__
6 #define __DTK_STRING_HPP__
7 
8 #ifdef _MSC_VER
9 # pragma warning(disable:4786)
10 # pragma warning(disable:4800)
11 #endif
12 
13 #include "def/define.h"
14 #include "util/util_stl_dtk.hpp"
15 #include <cstdio>
16 #include <string>
17 
18 
19 
20 #ifdef _MSC_VER
21 # if (_MSC_VER > 1200)
22 # define Dtk_defined__wchar_t
23 # endif
24 #endif
25 class Dtk_status;
26 #if ( defined(Dtk_defined__wchar_t) && defined(_NATIVE_WCHAR_T_DEFINED) )
27 //#pragma message("_NATIVE_WCHAR_T_DEFINED")
28 # define w_str _w_str_w
29 #else
30 # define w_str _w_str_us
31 #endif
32 
33 #define DTK_A Dtk_string(L"a")
34 #define DTK_AB Dtk_string(L"ab")
35 #define DTK_RB Dtk_string(L"rb")
36 #define DTK_RBP Dtk_string(L"rb+")
37 #define DTK_R Dtk_string(L"r")
38 #define DTK_W Dtk_string(L"w")
39 #define DTK_WP Dtk_string(L"w+")
40 #define DTK_WB Dtk_string(L"wb")
41 #define DTK_WBP Dtk_string(L"wb+")
42 #define DTK_RW Dtk_string(L"rw")
43 
50 
51 
53 {
54 public:
55  typedef size_t Size_t;
56 protected:
60  mutable char * m_CStr;
62  //This fields holds in memory the value of wcslen(m_Str). Caution : it may not be updated in case of user manipulation, if so, the Dtk_string is flagged as dirty (see isDirty()).
63  //Due to the use of the last byte to hold this flag, any Dtk_string of size > 2^8 will wrongly be considered dirty
64  mutable Size_t m_StrSize;
66  mutable Size_t m_CStrSize;
67 private:
69  void init();
70  void UpdateLen() const;
71 protected:
72  void _FillFromCharBuffer( const char* inBuffer, const Dtk_Size_t inSize = ( Dtk_Size_t )-1 );
73 
74 public:
81  Dtk_string( const char *s );
82  Dtk_string( const char* s, const Dtk_Size_t& inCount );
85 
86  Dtk_string( const Dtk_string & );
87 
88 #ifndef DTK_NO_CXX11_RVALUE_REFERENCES
89  Dtk_string( Dtk_string && s ) DTK_NOEXCEPT : m_Str( s.m_Str ), m_CStr( s.m_CStr ), m_StrSize( s.m_StrSize ), m_CStrSize( s.m_CStrSize )
92  {
93  s.m_Str = 0;
94  s.m_CStr = 0;
95  s.m_StrSize = 0;
96  s.m_CStrSize = 0;
97  }
98 
102  {
103  if( this != &s )
104  {
105  delete[] m_Str;
106  delete[] m_CStr;
107  m_Str = s.m_Str;
108  m_CStr = s.m_CStr;
109  m_StrSize = s.m_StrSize;
110  m_CStrSize = s.m_CStrSize;
111 
112  s.m_Str = 0;
113  s.m_CStr = 0;
114  s.m_StrSize = 0;
115  s.m_CStrSize = 0;
116  }
117 
118  return *this;
119  }
120 #endif
121  //Dtk_string(const int);
124  Dtk_string( const double dbl_val );
125  Dtk_string( const double dbl_val, Dtk_Int32 inWithMorePrecision );
128  Dtk_string( const float float_val );
129  void RawCopyFromASCII( const char * inStrToBeCopied, const Dtk_Size_t inCount = -1 );
130  /*************************************/
141  const char * c_str() const;
142 
160  void Split( const Dtk_string& inDelimiters, Dtk_tab<Dtk_string>& outResults ) const;
161  void Split( Dtk_WChar_t * inDelimiters, Dtk_tab<Dtk_string>& outResults ) const;
162 
173 #if ( defined(Dtk_defined__wchar_t))
174  const unsigned short * _w_str_us() const;
175  const __wchar_t * _w_str_w() const;
178  Dtk_string( const unsigned short *s );
181  Dtk_string( const __wchar_t *s );
194  void get_wchar( unsigned short * str ) const;
207  void get_wchar( __wchar_t * str ) const;
208 
209  int cmp( const unsigned short *s2 ) const;
210  int cmp( const __wchar_t *s2 ) const;
211  int ncmp( const unsigned short *s2, const int count ) const;
212  int ncmp( const __wchar_t *s2, const int count ) const;
213  int icmp( const unsigned short *s2 ) const;
214  int icmp( const __wchar_t *s2 ) const;
215  int nicmp( const unsigned short *s2, const size_t size ) const;
216  int nicmp( const __wchar_t *s2, const size_t size ) const;
217 #else
218  const Dtk_WChar_t * w_str() const;
219  Dtk_string( const Dtk_WChar_t *s );
232  void get_wchar( Dtk_WChar_t * str ) const;
233  int cmp( const Dtk_WChar_t *s2 ) const;
234  int ncmp( const Dtk_WChar_t *s2, const int count ) const;
235  int icmp( const Dtk_WChar_t *s2 ) const;
236  int nicmp( const Dtk_WChar_t *s2, const size_t size ) const;
237 #endif
238 
257  Dtk_string& operator = ( const char* s );
258 
267  Dtk_string& operator = ( const float fltval );
276  Dtk_string& operator = ( const double dbl_val );
277 
282  const Dtk_WChar_t& operator[] ( int i ) const;
283 
285  //SetAsDeprecated( "2021.4", "Use clear() method instead" )
286  void free_mem();
298  Dtk_status ToDtkInt32( Dtk_Int32& outRes ) const;
300 
310  char * ToUTF8String() const;
311 
312  Dtk_string Substring( const Dtk_Size_t& inStartIndex, const Dtk_Size_t& inLength ) const;
320  void clear();
321 
330  void convert_from_int( const int integer, int force_unsigned_int = 0 );
331  void ConvertFromUTF8String( const char * inUTF8String );
332 
333  /*utils functions*/
334  Dtk_bool is_NULL() const;
345  int len() const;
346 
347 
364  //SetAsDeprecated( "2021.4", "This method is insecure. Use get_char(char * inoutStr, const Dtk_Size_t inBufferSize ) method instead" )
365  Dtk_Size_t get_char( char * inoutStr ) const;
377  void cat( const Dtk_string &s2 );
378 
387  void ToUpper();
388 
397  void ToLower();
398 
413  int cmp( const Dtk_string &s2 ) const;
428  int ncmp( const Dtk_string &s2, const int count ) const;
442  int icmp( const Dtk_string &s2 ) const;
457  int nicmp( const Dtk_string &s2, const size_t size ) const;
469  void cpy( Dtk_string s2 );
480  void ncpy( const Dtk_string & s2, size_t size );
493  int find_substring( const Dtk_string &s1 ) const;
505  int find_first( int character ) const;
517  int find_last( int character ) const;
529  Dtk_string left( int pos ) const;
541  Dtk_string right( int pos ) const;
553  Dtk_string left_exclusive( int pos ) const;
554  void LeftExclusive( const int& pos, Dtk_string& outRes ) const;
566  Dtk_string right_exclusive( int pos ) const;
567  void RightExclusive( const int& pos, Dtk_string& outRes ) const;
568 
581  void TrimCharacters( const Dtk_WChar_t inCharToBeTrimmed,
582  const Dtk_bool inTrimLeadingCharacters = DTK_TRUE,
583  const Dtk_bool inTrimTrailingCharacters = DTK_TRUE );
594  inline void TrimLeadingCharacters( const Dtk_WChar_t inCharToBeTrimmed )
595  {
596  TrimCharacters( inCharToBeTrimmed, DTK_TRUE, DTK_FALSE );
597  }
608  inline void TrimTrailingCharacters( const Dtk_WChar_t inCharToBeTrimmed )
609  {
610  TrimCharacters( inCharToBeTrimmed, DTK_FALSE, DTK_TRUE );
611  }
622  inline void TrimLeadingAndTrailingCharacters( const Dtk_WChar_t inCharToBeTrimmed )
623  {
624  TrimCharacters( inCharToBeTrimmed, DTK_TRUE, DTK_TRUE );
625  }
639  int count_substring_occurrences( const Dtk_string &substring ) const;
651  int count_char_occurrences( const int car ) const;
652 
715  //SetAsDeprecated( "2024.2", "" )
717  Dtk_string * drive,
718  Dtk_string * path,
720  Dtk_string * extension ) const;
731  int replace( const int& old_char, const int& new_char );
741  int removechar( const int& removed_char );
742 
752  Dtk_string drive() const;
762  Dtk_string path() const;
783  int test_extension( const Dtk_string &ext ) const;
784 
795  void FullPath( Dtk_string& outFullPath ) const;
796 
806  void FileNameExtension( Dtk_string& outFileNameExtension ) const;
807 
808  //int test_extension(const char *ext);
809  //int test_extension(const wchar_t * ext);
824  FILE * ouvrir_fichier( const Dtk_string &droits ) const;
825 
840  FILE * OpenFile( const Dtk_string &inRights ) const;
841 
857  int unlink() const;
868  int mkdir() const;
869  int rmdir() const;
886  SetAsDeprecated( "2022.3", "Use FileSize() method instead" )
887  long taille_fichier() const;
888 
891  inline static const char PathSeparatorChar()
892  {
893 # if defined(_WIN32) || defined(__CYGWIN__) // Windows default, including MinGW and Cygwin
894  return '\\';
895 # else
896  return '/';
897 # endif
898  }
901  inline static const Dtk_WChar_t PathSeparatorWChar()
902  {
903 # if defined(_WIN32) || defined(__CYGWIN__) // Windows default, including MinGW and Cygwin
904  return L'\\';
905 # else
906  return L'/';
907 # endif
908  }
911  inline static const Dtk_string PathSeparatorString()
912  {
913 # if defined(_WIN32) || defined(__CYGWIN__) // Windows default, including MinGW and Cygwin
914  return L"\\";
915 # else
916  return "/";
917 # endif
918  }
919 
933 
946  friend Dtk_string operator + ( const Dtk_string &s1, const Dtk_string &s2 );
947 
959  void add_int( const int integer, int force_unsigned_int = 0 );
960 
961  void Merge( const Dtk_string &s2 );
963 
978  friend bool operator == ( const Dtk_string &s1, const Dtk_string &s2 );
993  friend bool operator > ( const Dtk_string &s1, const Dtk_string &s2 );
1008  friend bool operator < ( const Dtk_string &s1, const Dtk_string &s2 );
1023  friend bool operator >= ( const Dtk_string &s1, const Dtk_string &s2 );
1038  friend bool operator <= ( const Dtk_string &s1, const Dtk_string &s2 );
1053  friend bool operator != ( const Dtk_string &s1, const Dtk_string &s2 );
1054  friend std::ostream& operator<<( std::ostream&, const Dtk_string& );
1055 
1056 
1057 
1058 };
1059 
1061 {
1062  std::string ToUtf8( wchar_t const * );
1063  std::string ToUtf8( Dtk_string const & );
1064 
1065  Dtk_string FromUtf8( char const * );
1066 }
1067 
1068 
1069 #endif // __DTK_STRING_HPP__
1070 
1071 #endif // A93938D6857511E1B78E58294824019B
1072 
Dtk_string::ConvertFromUTF8String
void ConvertFromUTF8String(const char *inUTF8String)
Dtk_string::free_mem
void free_mem()
Dtk_string::rmdir
int rmdir() const
Dtk_string::Dtk_string
Dtk_string(const char *s)
constructor from a char * (ASCII string)
Dtk_string::operator==
friend bool operator==(const Dtk_string &s1, const Dtk_string &s2)
compare two Dtk_string
Dtk_string::cmp
int cmp(const Dtk_WChar_t *s2) const
Dtk_string::operator!=
friend bool operator!=(const Dtk_string &s1, const Dtk_string &s2)
compare two Dtk_string
Dtk_string::TrimTrailingCharacters
void TrimTrailingCharacters(const Dtk_WChar_t inCharToBeTrimmed)
Removes all occurrences of a given char at the end - trailing - into the Dtk_string.
Definition: dtk_string.hpp:608
Dtk_string::_FillFromCharBuffer
void _FillFromCharBuffer(const char *inBuffer, const Dtk_Size_t inSize=(Dtk_Size_t) -1)
Dtk_string::PathSeparatorChar
static const char PathSeparatorChar()
File Utility : retrieve The OS Path Separator as char.
Definition: dtk_string.hpp:891
Dtk_string::TrimCharacters
void TrimCharacters(const Dtk_WChar_t inCharToBeTrimmed, const Dtk_bool inTrimLeadingCharacters=DTK_TRUE, const Dtk_bool inTrimTrailingCharacters=DTK_TRUE)
Removes all occurrences of a given char at the beginning - leading - or the end - trailing - into the...
Dtk_string::ouvrir_fichier
FILE * ouvrir_fichier(const Dtk_string &droits) const
File Utility : Open a file with the given rights.
Dtk_string::TrimLeadingAndTrailingCharacters
void TrimLeadingAndTrailingCharacters(const Dtk_WChar_t inCharToBeTrimmed)
Removes all occurrences of a given char at the beginning - leading - and the end - trailing - into th...
Definition: dtk_string.hpp:622
Dtk_string::_w_str_us
const Dtk_WChar_t * _w_str_us() const
Retrieve the UNICODE string.
Dtk_string::count_substring_occurrences
int count_substring_occurrences(const Dtk_string &substring) const
Counts all the occurrences of a given substring into the Dtk_string.
DTK_TRUE
#define DTK_TRUE
Definition: define.h:730
Dtk_string::cat
void cat(const Dtk_string &s2)
concat the Dtk_string with the Dtk_string given in parameter
Dtk_string::count_char_occurrences
int count_char_occurrences(const int car) const
Counts all the occurrences of a given character into the Dtk_string.
Dtk_string::TrimLeadingCharacters
void TrimLeadingCharacters(const Dtk_WChar_t inCharToBeTrimmed)
Removes all occurrences of a given char at the beginning - leading - and the end - trailing - into th...
Definition: dtk_string.hpp:594
Dtk_string
This is a high level string class.
Definition: dtk_string.hpp:53
Dtk_Size_t
size_t Dtk_Size_t
Definition: define.h:715
Dtk_WChar_t
wchar_t Dtk_WChar_t
Definition: define.h:714
Dtk_string::unlink
int unlink() const
File Utility : Delete a file.
Dtk_string::convert_from_int
void convert_from_int(const int integer, int force_unsigned_int=0)
affectation operator from a int
Dtk_string::path
Dtk_string path() const
File Utility : Retrieves the path in Dtk_string form.
Dtk_string::operator<<
friend std::ostream & operator<<(std::ostream &, const Dtk_string &)
Dtk_string::LeftExclusive
void LeftExclusive(const int &pos, Dtk_string &outRes) const
Dtk_string::ncpy
void ncpy(const Dtk_string &s2, size_t size)
copy a number of characters of the Dtk_string in an other Dtk_string
Dtk_status
Definition: dtk_status.hpp:16
Dtk_string::operator>=
friend bool operator>=(const Dtk_string &s1, const Dtk_string &s2)
compare two Dtk_string
DTK_FALSE
#define DTK_FALSE
Definition: define.h:731
Dtk_string::ncmp
int ncmp(const Dtk_WChar_t *s2, const int count) const
Dtk_bool
char Dtk_bool
Definition: define.h:728
Dtk_string::OpenFile
FILE * OpenFile(const Dtk_string &inRights) const
File Utility : Open a file with the given rights.
Dtk_string::operator+=
Dtk_string & operator+=(const Dtk_string &s2)
Dtk_string::ToUpper
void ToUpper()
Converts the Dtk_string to Upper case.
Dtk_string::right
Dtk_string right(int pos) const
retrieve the right part of the Dtk_string from a position
Dtk_Double64
double Dtk_Double64
Definition: define.h:702
Dtk_string::icmp
int icmp(const Dtk_WChar_t *s2) const
Dtk_string::find_substring
int find_substring(const Dtk_string &s1) const
find the position of a substring into a Dtk_string
Dtk_string::Split
void Split(const Dtk_string &inDelimiters, Dtk_tab< Dtk_string > &outResults) const
Split a Dtk_string into an array of Dtk_string giving a char array.
Dtk_string::existe_fichier
Dtk_bool existe_fichier() const
File Utility : tests if a file exists.
Dtk_string::PathSeparatorString
static const Dtk_string PathSeparatorString()
File Utility : Retrieves The OS Path Separator as Dtk_string.
Definition: dtk_string.hpp:911
Dtk_string::add_int
void add_int(const int integer, int force_unsigned_int=0)
concat an int to the Dtk_string (convert the int to Dtk_string)
Dtk_string::m_CStrSize
Size_t m_CStrSize
size of m_CStr buffer
Definition: dtk_string.hpp:66
Dtk_string::is_not_NULL
Dtk_bool is_not_NULL() const
Dtk_string::SetAsDeprecated
SetAsDeprecated("2022.3", "Use FileSize() method instead") long taille_fichier() const
Dtk_StringUtility::ToUtf8
std::string ToUtf8(wchar_t const *)
Dtk_string::RightExclusive
void RightExclusive(const int &pos, Dtk_string &outRes) const
Dtk_string::replace
int replace(const int &old_char, const int &new_char)
Replaces all occurrences of a character in a string with a new character.
Dtk_string::filename
Dtk_string filename() const
File Utility : Retrieves the filename in Dtk_string form.
Dtk_string::operator<
friend bool operator<(const Dtk_string &s1, const Dtk_string &s2)
compare two Dtk_string
Dtk_string::ToDtkInt32
Dtk_status ToDtkInt32(Dtk_Int32 &outRes) const
Dtk_string::operator<=
friend bool operator<=(const Dtk_string &s1, const Dtk_string &s2)
compare two Dtk_string
Dtk_string::cmp
int cmp(const Dtk_string &s2) const
compare the Dtk_string with the string given in parameter
Dtk_string::ToUTF8String
char * ToUTF8String() const
Converts the Dtk_string to UTF8 string.
Dtk_string::FullPath
void FullPath(Dtk_string &outFullPath) const
File Utility : Retrieves the full path in Dtk_string form.
Dtk_string::Dtk_string
Dtk_string(const double dbl_val, Dtk_Int32 inWithMorePrecision)
Dtk_Int32
int32_t Dtk_Int32
Definition: define.h:690
Dtk_string::ToDtkDouble64
Dtk_status ToDtkDouble64(Dtk_Double64 &outRes) const
Dtk_string::operator=
Dtk_string & operator=(Dtk_string &&s) DTK_NOEXCEPT
Move assignment operator.
Definition: dtk_string.hpp:101
Dtk_string::Split
void Split(Dtk_WChar_t *inDelimiters, Dtk_tab< Dtk_string > &outResults) const
Dtk_string::cpy
void cpy(Dtk_string s2)
copy the Dtk_string given in parameter in the Dtk_string
Dtk_string::test_extension
int test_extension(const Dtk_string &ext) const
Dtk_string::get_char
Dtk_Size_t get_char(char *inoutStr) const
copy the Dtk_string in a char * (ASCII) string
Dtk_StringUtility
Definition: dtk_string.hpp:1061
Dtk_string::PathSeparatorWChar
static const Dtk_WChar_t PathSeparatorWChar()
File Utility : Retrieves The OS Path Separator as Dtk_WChar_t.
Definition: dtk_string.hpp:901
Dtk_string::get_wchar
void get_wchar(Dtk_WChar_t *str) const
copy the Dtk_string in a char * (UNICODE) string
Dtk_string::Dtk_string
Dtk_string(const Dtk_string &)
copy constructor
Dtk_string::operator[]
Dtk_WChar_t & operator[](int)
access to a specified letter in the Dtk_string
Dtk_string::traite_nom_fichier
int traite_nom_fichier(Dtk_string *drive, Dtk_string *path, Dtk_string *filename, Dtk_string *extension) const
File Utility: Splits Fullpath into drive/path/filename/extension.
Dtk_string::FileNameExtension
void FileNameExtension(Dtk_string &outFileNameExtension) const
File Utility : Retrieves the filename extension in Dtk_string form.
Dtk_string::is_NULL
Dtk_bool is_NULL() const
Dtk_string::Size_t
size_t Size_t
Definition: dtk_string.hpp:55
Dtk_string::left
Dtk_string left(int pos) const
retrieve the left part of the Dtk_string from a position
Dtk_string::c_str
const char * c_str() const
Retrieve the ASCII conversion string.
Dtk_string::nicmp
int nicmp(const Dtk_string &s2, const size_t size) const
Compare characters of two strings without regard to case.
Dtk_string::FileSize
Dtk_Size_t FileSize() const
File Utility : retrieve the file size.
Dtk_string::extension
Dtk_string extension() const
File Utility : Retrieves the extension in Dtk_string form.
Dtk_string::find_first
int find_first(int character) const
find the position of a character into a Dtk_string
Dtk_string::mkdir
int mkdir() const
File Utility : Create a Directory.
Dtk_string::clear
void clear()
clear string data
Dtk_string::left_exclusive
Dtk_string left_exclusive(int pos) const
retrieve the left part of the Dtk_string from a position
w_str
#define w_str
Definition: dtk_string.hpp:30
Dtk_string::Dtk_string
Dtk_string(const double dbl_val)
constructor from a double
Dtk_string::RawCopyFromASCII
void RawCopyFromASCII(const char *inStrToBeCopied, const Dtk_Size_t inCount=-1)
Dtk_string::operator+
friend Dtk_string operator+(const Dtk_string &s1, const Dtk_string &s2)
concat two Dtk_string
Dtk_string::FixPathSeparator
void FixPathSeparator()
File Utility : Fixes path separator consistency. It lets you replace the '\' or '/' by the OS needed ...
util_stl_dtk.hpp
Dtk_string::drive
Dtk_string drive() const
File Utility : Retrieves the drive in Dtk_string form.
define.h
Dtk_string::Dtk_string
Dtk_string(const char *s, const Dtk_Size_t &inCount)
Dtk_string::find_last
int find_last(int character) const
find the position of a character into a Dtk_string
Dtk_tab< Dtk_string >
Dtk_string::~Dtk_string
~Dtk_string()
Default destructors.
Dtk_string::ncmp
int ncmp(const Dtk_string &s2, const int count) const
compare the count first character of the Dtk_string with the string given in parameter
Dtk_string::nicmp
int nicmp(const Dtk_WChar_t *s2, const size_t size) const
Dtk_string::Substring
Dtk_string Substring(const Dtk_Size_t &inStartIndex, const Dtk_Size_t &inLength) const
Dtk_string::right_exclusive
Dtk_string right_exclusive(int pos) const
retrieve the right part of the Dtk_string from a position
Dtk_string::Merge
void Merge(const Dtk_string &s2)
Dtk_string::m_StrSize
Size_t m_StrSize
size of m_Str buffer
Definition: dtk_string.hpp:64
Dtk_StringUtility::FromUtf8
Dtk_string FromUtf8(char const *)
Dtk_string::len
int len() const
Retrieve the length of the Dtk_string.
Dtk_string::icmp
int icmp(const Dtk_string &s2) const
Perform a lowercase comparison of strings.
Dtk_string::m_Str
Dtk_WChar_t * m_Str
Internal representation of the string.
Definition: dtk_string.hpp:58
Dtk_string::Dtk_string
Dtk_string(const Dtk_WChar_t *s)
Dtk_string::Dtk_string
Dtk_string(const float float_val)
constructor from a float
Dtk_string::Dtk_string
Dtk_string()
Default constructor.
Dtk_string::operator>
friend bool operator>(const Dtk_string &s1, const Dtk_string &s2)
compare two Dtk_string
Dtk_string::ToLower
void ToLower()
Converts the Dtk_string to Lower case.
Dtk_string::m_CStr
char * m_CStr
compatibility ASCII representation. WARNING this field is not always up to date.
Definition: dtk_string.hpp:60
DTK_NOEXCEPT
#define DTK_NOEXCEPT
Definition: config.hpp:30
Dtk_string::removechar
int removechar(const int &removed_char)
Removes all occurrences of a character in a string.