Difference between revisions of "Visual Studio C++ Libraries CharString"
(No difference)
|
Latest revision as of 19:11, 23 April 2020
Visual Studio C++ Libraries CharString
Visual Studio CString with some added methods.
CharString.h
#ifndef _CHARSTRING_ #define _CHARSTRING_ #include <SDKDDKVer.h> #include "stdafx.h" using namespace std; #include <string> #include <cstring> #include <afxcoll.h> #include <strsafe.h> #pragma once class CharString { public: CharString(void); virtual ~CharString(void); static int Count_CString( CString input_cs, CString deliminator_cs ); static bool IsDecimal( CString input_cs ); static bool IsInteger( CString input_cs ); static bool IsInteger( char* input_cs ); static void Token_CString( CStringArray & ca, CString input_cs, CString deliminator_cs ); static void Token_CString( CStringArray & ca, CString input_cs, int charcount_int ); static char* CopyBuff( const char* const strA, const char* const strB ); static char* CopyBuff (const char* const s); static bool FindBegin( char* buf, const char* const s); static bool FindBegin( const char* buf, const char* const s); static bool FindBegin( std::string buf, std::string s); static bool FindIBegin (const char* const buf,const char* const s); // tf: Return "true" for true, "false" for false. static char* bool_tf ( bool b); static bool streat (const char** buf, const char* s); static bool streat ( char** buf, const char* s); static bool strcaseeat (const char** buf,const char* const s); // Repeatedly Remove the trailing character static void RemoveTrailingSlash( CString input_cs ); static CString PathNormalize( CString ); private: CString this_cs; }; #endif // _CHARSTRING_
CharString.cpp
#include "CharString.h" CharString::CharString(void) { } CharString::~CharString(void) { } //////////////////////////////////////////////////////////////////////////////// // // // // // int CharString::Count_CString( CString input_cs, CString deliminator_cs ) { if( input_cs.IsEmpty() || deliminator_cs.IsEmpty() ) return -1; return input_cs.Replace( deliminator_cs, deliminator_cs); } bool CharString::IsDecimal( CString input_cs ) { // trim whitespace input_cs.Trim(); // length test, must have leading zeros if( input_cs.GetLength() == 0 ) return false; double result_d = -1.0; char *stopstring = nullptr; CT2A ascii_num( input_cs ); // convert to a char string // strtol ignores whitespace front and back. // returns 0 if no conversion can be performed // returns LONG_MAX or LONG_MIN // in an alphanumeric string, stopstring points to // the first non-number characters. result_d = strtod( ascii_num, &stopstring ); int stopstring_len = strlen( stopstring ); // If alphanumeric, return false if( stopstring_len > 0 ) return false; return true; } bool CharString::IsInteger( CString input_cs ) { // trim whitespace input_cs.Trim(); // length test, must have leading zeros if( input_cs.GetLength() == 0 ) return false; long result_l = -1; char *stopstring = nullptr; CT2A ascii_num( input_cs ); // convert to a char string // strtol ignores whitespace front and back. // returns 0 if no conversion can be performed // returns LONG_MAX or LONG_MIN // in an alphanumeric string, stopstring points to // the first non-number characters. result_l = strtol( ascii_num, &stopstring, 10 ); int stopstring_len = strlen( stopstring ); // If alphanumeric, return false if( stopstring_len > 0 ) return false; return true; } bool CharString::IsInteger( char* input_cs ) { CString str = CString( input_cs ); return CharString::IsInteger( str ); } void CharString::Token_CString( CStringArray & ca, CString input_cs, CString deliminator_cs ) { int index_int = 0; CString field; // Trim leading and trailing whitespace int replace_count = input_cs.Replace('\n',' ' ); input_cs.Trim(); // Test if the input CString is empty if( input_cs.IsEmpty() ) { return; } field = input_cs.Tokenize( deliminator_cs, index_int ); field.Trim(); ca.Add(field); while ( field.IsEmpty() == false ) { field = input_cs.Tokenize( deliminator_cs, index_int ); field.Trim(); if( field.IsEmpty() == false ) ca.Add(field); } } void CharString::Token_CString( CStringArray & ca, CString input_cs, int charcount_int ) { int index_int = 0; CString field; while ( index_int < input_cs.GetLength() ) { field = input_cs.Mid(index_int,charcount_int ); ca.Add(field); index_int += charcount_int; } } char* CharString::CopyBuff( const char* const strA, const char* const strB ){ // test the pointers if( strA == nullptr || strB == nullptr ) return nullptr; rsize_t buffsize_size_t = strlen( strA ) + strlen( strB ) + 1; char* return_p = new char[ buffsize_size_t ]; errno_t copy_result; // First stage copy, copy_result = strcpy_s( return_p, buffsize_size_t, strA ); if( copy_result != 0 ) { // copy failed delete[] return_p; return nullptr; } // Second stage copy, copy_result = strcat_s( return_p, buffsize_size_t, strB ); if( copy_result != 0 ) { // copy failed delete[] return_p; return nullptr; } return return_p; } char* CharString::CopyBuff (const char* const s) { size_t n=strlen(s)+1; char* const str=new char[n]; strcpy_s(str,n,s); return str; } bool CharString::FindBegin( char* buf, const char* const str2 ){ std::string strA = std::string( buf ); std::string strB = std::string( str2 ); return CharString::FindBegin( strA, strB ); } bool CharString::FindBegin( const char* buf, const char* const str2 ){ std::string strA = std::string( buf ); std::string strB = std::string( str2 ); return CharString::FindBegin( strA, strB ); } // buf starts with s, case insensitive bool CharString::FindIBegin (const char* const buf,const char* const s) { for (int i=0;s[i]!=0;i++) // Loop through all characters in s. if (tolower(buf[i])!=tolower(s[i])) // Does this one match? return false; // No, return false if no match. return true; // Return true if all match. } bool CharString::FindBegin( std::string buf, std::string str2 ){ std::size_t found = buf.find(str2); if( found == 0 ) return true; return false; } char* CharString::bool_tf ( bool b) { return (b?"true":"false"); } bool CharString::streat ( char** buf, const char* s) { for (int i=0;s[i]!=0;i++) // Loop through all characters in s. if ((*buf)[i]!=s[i]) // Does this one match? return false; // No, return false if no match. (*buf)+=strlen(s); // Increment source string if match. return true; // Return true if all match. } bool CharString::streat (const char** buf, const char* s) { for (int i=0;s[i]!=0;i++) // Loop through all characters in s. if ((*buf)[i]!=s[i]) // Does this one match? return false; // No, return false if no match. (*buf)+=strlen(s); // Increment source string if match. return true; // Return true if all match. } bool CharString::strcaseeat (const char** buf,const char* const s) { for (int i=0;s[i]!=0;i++) // Loop through all characters in s. if (tolower((*buf)[i])!=tolower(s[i]))// Does this one match? return false; // No, return false if no match. (*buf)+=strlen(s); // Increment source string if match. return true; // Return true if all match. } void CharString::RemoveTrailingSlash( CString input_cs) { // Repeatedly strip trailing slashes while( '\\' == input_cs[ input_cs.GetLength() - 1 ] || '/' == input_cs[ input_cs.GetLength() - 1 ]) { input_cs.Delete( input_cs.GetLength() - 1 ); } } // Returns LONG_MAX if convertion failed. // Returns signed long otherwise. long CharString::ConvertLong( CString Int_cs ) { long result_l = -1; char *stopstring = nullptr; CT2A ascii_num( Int_cs ); // convert to a char string // strtol ignores whitespace front and back. // returns 0 if no conversion can be performed // returns LONG_MAX or LONG_MIN // in an alphanumeric string, stopstring points to // the first non-number characters. result_l = strtol( ascii_num, &stopstring, 10 ); int stopstring_len = strlen( stopstring ); // If alphanumeric, return LONG_MAX if( stopstring_len > 0 ) return LONG_MAX; return result_l; } // Returns DBL_MAX if convertion failed. // Returns signed double otherwise. double CharString::ConvertDouble( CString Double_cs ) { double result_d = -1; char *stopstring = nullptr; CT2A ascii_num( Double_cs ); // convert to a char string // strtol ignores whitespace front and back. // returns 0 if no conversion can be performed // returns LONG_MAX or LONG_MIN // in an alphanumeric string, stopstring points to // the first non-number characters. result_d = strtod( ascii_num, &stopstring ); int stopstring_len = strlen( stopstring ); // If alphanumeric, return LONG_MAX if( stopstring_len > 0 ) return DBL_MAX; return result_d; } // Normalize the given path. // Replace "//" with "/" // Replace "/" with "\" // Replace "\" with "\\" CString CharString::PathNormalize( CString path_cs ) { CString Cpath; CString DoubleBackslash_cs = "\\"; while ( path_cs.Replace("/", "\\") ); // while ( path_cs.Replace("\\\\", @"\\" ) ); return path_cs; }
Internal Links
Parent Article: Visual Studio C++ Libraries