DATAKIT API  V2025.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 <math.h>
14 #include "def/define.h"
15 #include "util/util_stl_dtk.hpp"
16 
17 #include "dtk/config.hpp"
18 
19 #include <cstdio>
20 #include <iosfwd>
21 #include <string>
22 
23 
24 
25 #ifdef _MSC_VER
26 # if (_MSC_VER > 1200)
27 # define Dtk_defined__wchar_t
28 # endif
29 #endif
30 class Dtk_status;
31 #if ( defined(Dtk_defined__wchar_t) && defined(_NATIVE_WCHAR_T_DEFINED) )
32 //#pragma message("_NATIVE_WCHAR_T_DEFINED")
33 # define w_str _w_str_w
34 #else
35 # define w_str _w_str_us
36 #endif
37 
38 #define DTK_A Dtk_string(L"a")
39 #define DTK_AB Dtk_string(L"ab")
40 #define DTK_RB Dtk_string(L"rb")
41 #define DTK_RBP Dtk_string(L"rb+")
42 #define DTK_R Dtk_string(L"r")
43 #define DTK_W Dtk_string(L"w")
44 #define DTK_WP Dtk_string(L"w+")
45 #define DTK_WB Dtk_string(L"wb")
46 #define DTK_WBP Dtk_string(L"wb+")
47 #define DTK_RW Dtk_string(L"rw")
48 
55 
56 
58 {
59 public:
60  typedef size_t Size_t;
61 protected:
65  mutable char * m_CStr;
67  //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()).
68  //Due to the use of the last byte to hold this flag, any Dtk_string of size > 2^8 will wrongly be considered dirty
69  mutable Size_t m_StrSize;
71  mutable Size_t m_CStrSize;
72 private:
74  void init();
75  void UpdateLen() const;
76 protected:
77  void _FillFromCharBuffer( const char* inBuffer, const Dtk_Size_t inSize = ( Dtk_Size_t )-1 );
78 
79 public:
86  Dtk_string( const char *s );
87  Dtk_string( const char* s, const Dtk_Size_t& inCount );
90 
91  Dtk_string( const Dtk_string & );
92 
93 #ifndef DTK_NO_CXX11_RVALUE_REFERENCES
94  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 )
97  {
98  s.m_Str = 0;
99  s.m_CStr = 0;
100  s.m_StrSize = 0;
101  s.m_CStrSize = 0;
102  }
103 
107  {
108  if( this != &s )
109  {
110  delete[] m_Str;
111  delete[] m_CStr;
112  m_Str = s.m_Str;
113  m_CStr = s.m_CStr;
114  m_StrSize = s.m_StrSize;
115  m_CStrSize = s.m_CStrSize;
116 
117  s.m_Str = 0;
118  s.m_CStr = 0;
119  s.m_StrSize = 0;
120  s.m_CStrSize = 0;
121  }
122 
123  return *this;
124  }
125 #endif
126  //Dtk_string(const int);
129  Dtk_string( const double dbl_val );
130  Dtk_string( const double dbl_val, Dtk_Int32 inWithMorePrecision );
133  Dtk_string( const float float_val );
134  void RawCopyFromASCII( const char * inStrToBeCopied, const Dtk_Size_t inCount = -1 );
135  /*************************************/
146  const char * c_str() const;
147 
165  void Split( const Dtk_string& inDelimiters, Dtk_tab<Dtk_string>& outResults ) const;
166  void Split( Dtk_WChar_t * inDelimiters, Dtk_tab<Dtk_string>& outResults ) const;
167 
178 #if ( defined(Dtk_defined__wchar_t))
179  const unsigned short * _w_str_us() const;
180  const __wchar_t * _w_str_w() const;
183  Dtk_string( const unsigned short *s );
186  Dtk_string( const __wchar_t *s );
199  void get_wchar( unsigned short * str ) const;
212  void get_wchar( __wchar_t * str ) const;
213 
214  int cmp( const unsigned short *s2 ) const;
215  int cmp( const __wchar_t *s2 ) const;
216  int ncmp( const unsigned short *s2, const int count ) const;
217  int ncmp( const __wchar_t *s2, const int count ) const;
218  int icmp( const unsigned short *s2 ) const;
219  int icmp( const __wchar_t *s2 ) const;
220  int nicmp( const unsigned short *s2, const size_t size ) const;
221  int nicmp( const __wchar_t *s2, const size_t size ) const;
222 #else
223  const Dtk_WChar_t * w_str() const;
224  Dtk_string( const Dtk_WChar_t *s );
237  void get_wchar( Dtk_WChar_t * str ) const;
238  int cmp( const Dtk_WChar_t *s2 ) const;
239  int ncmp( const Dtk_WChar_t *s2, const int count ) const;
240  int icmp( const Dtk_WChar_t *s2 ) const;
241  int nicmp( const Dtk_WChar_t *s2, const size_t size ) const;
242 #endif
243 
262  Dtk_string& operator = ( const char* s );
263 
272  Dtk_string& operator = ( const float fltval );
281  Dtk_string& operator = ( const double dbl_val );
282 
287  const Dtk_WChar_t& operator[] ( int i ) const;
288 
290  //SetAsDeprecated( "2021.4", "Use clear() method instead" )
291  void free_mem();
303  Dtk_status ToDtkInt32( Dtk_Int32& outRes ) const;
305 
315  char * ToUTF8String() const;
316 
317  Dtk_string Substring( const Dtk_Size_t& inStartIndex, const Dtk_Size_t& inLength ) const;
325  void clear();
326 
335  void convert_from_int( const int integer, int force_unsigned_int = 0 );
336  void ConvertFromUTF8String( const char * inUTF8String );
337 
338  /*utils functions*/
339  Dtk_bool is_NULL() const;
350  int len() const;
351 
352 
369  //SetAsDeprecated( "2021.4", "This method is insecure. Use get_char(char * inoutStr, const Dtk_Size_t inBufferSize ) method instead" )
370  Dtk_Size_t get_char( char * inoutStr ) const;
382  void cat( const Dtk_string &s2 );
383 
392  void ToUpper();
393 
402  void ToLower();
403 
418  int cmp( const Dtk_string &s2 ) const;
433  int ncmp( const Dtk_string &s2, const int count ) const;
447  int icmp( const Dtk_string &s2 ) const;
462  int nicmp( const Dtk_string &s2, const size_t size ) const;
474  void cpy( Dtk_string s2 );
485  void ncpy( const Dtk_string & s2, size_t size );
498  int find_substring( const Dtk_string &s1 ) const;
510  int find_first( int character ) const;
522  int find_last( int character ) const;
534  Dtk_string left( int pos ) const;
546  Dtk_string right( int pos ) const;
558  Dtk_string left_exclusive( int pos ) const;
559  void LeftExclusive( const int& pos, Dtk_string& outRes ) const;
571  Dtk_string right_exclusive( int pos ) const;
572  void RightExclusive( const int& pos, Dtk_string& outRes ) const;
573 
586  void TrimCharacters( const Dtk_WChar_t inCharToBeTrimmed,
587  const Dtk_bool inTrimLeadingCharacters = DTK_TRUE,
588  const Dtk_bool inTrimTrailingCharacters = DTK_TRUE );
599  inline void TrimLeadingCharacters( const Dtk_WChar_t inCharToBeTrimmed )
600  {
601  TrimCharacters( inCharToBeTrimmed, DTK_TRUE, DTK_FALSE );
602  }
613  inline void TrimTrailingCharacters( const Dtk_WChar_t inCharToBeTrimmed )
614  {
615  TrimCharacters( inCharToBeTrimmed, DTK_FALSE, DTK_TRUE );
616  }
627  inline void TrimLeadingAndTrailingCharacters( const Dtk_WChar_t inCharToBeTrimmed )
628  {
629  TrimCharacters( inCharToBeTrimmed, DTK_TRUE, DTK_TRUE );
630  }
644  int count_substring_occurrences( const Dtk_string &substring ) const;
656  int count_char_occurrences( const int car ) const;
657 
720  //SetAsDeprecated( "2024.2", "" )
722  Dtk_string * drive,
723  Dtk_string * path,
725  Dtk_string * extension ) const;
736  int replace( const int& old_char, const int& new_char );
746  int removechar( const int& removed_char );
747 
757  Dtk_string drive() const;
767  Dtk_string path() const;
788  int test_extension( const Dtk_string &ext ) const;
789 
800  void FullPath( Dtk_string& outFullPath ) const;
801 
811  void FileNameExtension( Dtk_string& outFileNameExtension ) const;
812 
813  //int test_extension(const char *ext);
814  //int test_extension(const wchar_t * ext);
829  FILE * ouvrir_fichier( const Dtk_string &droits ) const;
830 
845  FILE * OpenFile( const Dtk_string &inRights ) const;
846 
862  int unlink() const;
873  int mkdir() const;
874  int rmdir() const;
890  SetAsDeprecated( "2022.3", "Use FileSize() method instead" )
891  long taille_fichier() const;
892 
895  inline static const char PathSeparatorChar()
896  {
897 # if defined(_WIN32) || defined(__CYGWIN__) // Windows default, including MinGW and Cygwin
898  return '\\';
899 # else
900  return '/';
901 # endif
902  }
905  inline static const Dtk_WChar_t PathSeparatorWChar()
906  {
907 # if defined(_WIN32) || defined(__CYGWIN__) // Windows default, including MinGW and Cygwin
908  return L'\\';
909 # else
910  return L'/';
911 # endif
912  }
915  inline static const Dtk_string PathSeparatorString()
916  {
917 # if defined(_WIN32) || defined(__CYGWIN__) // Windows default, including MinGW and Cygwin
918  return L"\\";
919 # else
920  return "/";
921 # endif
922  }
923 
937 
950  friend Dtk_string operator + ( const Dtk_string &s1, const Dtk_string &s2 );
951 
963  void add_int( const int integer, int force_unsigned_int = 0 );
964 
965  void Merge( const Dtk_string &s2 );
967 
982  friend bool operator == ( const Dtk_string &s1, const Dtk_string &s2 );
997  friend bool operator > ( const Dtk_string &s1, const Dtk_string &s2 );
1012  friend bool operator < ( const Dtk_string &s1, const Dtk_string &s2 );
1027  friend bool operator >= ( const Dtk_string &s1, const Dtk_string &s2 );
1042  friend bool operator <= ( const Dtk_string &s1, const Dtk_string &s2 );
1057  friend bool operator != ( const Dtk_string &s1, const Dtk_string &s2 );
1058  friend std::ostream& operator<<( std::ostream&, const Dtk_string& );
1059 
1060 
1061 
1062 };
1063 
1065 {
1066  std::string ToUtf8( Dtk_string const & );
1067 
1068  Dtk_string FromUtf8( char const * );
1069 }
1070 
1071 
1072 #endif // __DTK_STRING_HPP__
1073 
1074 #endif // A93938D6857511E1B78E58294824019B
1075 
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:613
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:895
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.
config.hpp
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:627
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:727
Dtk_string::cat
void cat(const Dtk_string &s2)
concat the Dtk_string with the Dtk_string given in parameter
Dtk_StringUtility::ToUtf8
std::string ToUtf8(Dtk_string const &)
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:599
Dtk_string
This is a high level string class.
Definition: dtk_string.hpp:58
Dtk_Size_t
size_t Dtk_Size_t
Definition: define.h:712
Dtk_WChar_t
wchar_t Dtk_WChar_t
Definition: define.h:711
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:728
Dtk_string::ncmp
int ncmp(const Dtk_WChar_t *s2, const int count) const
Dtk_bool
char Dtk_bool
Definition: define.h:725
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:699
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:915
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:71
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_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:687
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:106
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:1065
Dtk_string::PathSeparatorWChar
static const Dtk_WChar_t PathSeparatorWChar()
File Utility : Retrieves The OS Path Separator as Dtk_WChar_t.
Definition: dtk_string.hpp:905
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:60
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:35
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:69
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:63
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:65
DTK_NOEXCEPT
#define DTK_NOEXCEPT
Definition: config.hpp:26
Dtk_string::removechar
int removechar(const int &removed_char)
Removes all occurrences of a character in a string.