Affichage des articles dont le libellé est StringFormat. Afficher tous les articles
Affichage des articles dont le libellé est StringFormat. Afficher tous les articles

jeudi 28 février 2019

MyString CPlusPlus Mes String en C++ StringFormat & StringSplit

StringFormat et StringSplit sont deux fonctions que l'on a toujours besoin d'avoir sous la main. Je me retrouve avec Embarcadero C++ Builder à devoir coder en C++, je tombe donc sur l'UnicodeString hérité de Delphi imaginez un peu mon désarrois.

Coder en C++ StringSplit et StrinFormat

Je l'avoue ça fait un peu mal, même si ce type est très utile sous Embarcadero, il vient du moyen âge ! Pour nous faciliter la vie, je souhaite donc vous livrer ici mes deux fonctions qui à mon goût, n'existent pas ni dans C++ Builder ni dans les STL.

Voici donc StringFormat et StringSplit les deux foncitons C++ qui manquent toujours à l'appel.

L'entête

#ifndef _MyStringH
#define _MyStringH

#include <iostream>
#include <conio.h>
#include <sstream>
#include <vector>
#include <string>


using namespace std;

// ---------------------------------------------------------------------------
// Mangement of Srting's Sizes

#define MAX_STRING 256
// ---------------------------------------------------------------------------
void StringFormat( UnicodeString &str, const char *format, ... );
vector<string> StringSplit( string s, string separator );
// ---------------------------------------------------------------------------
#endif

Corps des fonctions StringFormat et StringSplit en C++

//----------------------------------------------------------------------------
// Format String
//----------------------------------------------------------------------------

void StringFormat( UnicodeString &str, const char *format, ... )
{
   char buffer[ MAX_STRING ];
   va_list args;

   va_start( args, format );

   vsprintf( buffer, format, args );
   str = UnicodeString( buffer);
   perror( buffer );

   va_end( args );
}

//----------------------------------------------------------------------------
// Split string with separator
//----------------------------------------------------------------------------

vector<string> StringSplit( string s, string separator )
{
   size_t pos_start = 0, pos_end, delim_len = separator.length();
   string token;
   vector<string> res;
   while ( (pos_end = s.find( separator, pos_start )) != string::npos )
   {
      token = s.substr( pos_start, pos_end - pos_start );
      pos_start = pos_end + delim_len;
      res.push_back( token );
   }
   res.push_back( s.substr( pos_start ) );
   return res;
}

Utilisation des fonctions StringFormat et StringSplit en C++

UnicodeString b = "blabla";
double d = 2.033256;
StringFormat( b, "d = %.3f", d );
Edit1->Text = b;

//-----------------------------------------

int tab[ 4 ] = { 0, 0, 0, 0 };
string str = "0, 1,  2,  3";
string separator = ",";
vector<string> v = StringSplit( str, separator );
int j = 0;
for ( auto i : v )
{
  tab[ j++ ] = atoi( i.c_str() );
}

Conclusion

Vous pouvez adapter ces fonctions pour une autre chaîne de développement comme Qt ou Visual Studio C++ en utilisant CString à la place d'UnicodeString d'Embarcadero.

Voilà, la prochaine fois que je code en C++, je n'aurais pas à les réécrire ... ;-)