String vs string (regex and GePrint) [SOLVED]



  • On 29/10/2015 at 12:18, xxxxxxxx wrote:

    User Information:
    Cinema 4D Version:    
    Platform:      
    Language(s) :

    ---------
    Hi, one more question, maybe i'm doing it wrong (best choice !!)

    When I'm trying to get back value from a STRING resource i have to push it into a String object...
    When I'm trying to print some thing with GePrint("my life"), i have to push it from a String object...

    Nothing wrong ? But if i want to use regex or to_string() std::stoi("002") it can be painfull. I have to convert and juggle withs 2 different type of string.

    "String" came from C4D with #include "c4d_string"
    and
    "string" inside std library with #include <string> ....

    Finally my fist question is simple. GePrint() just want C4D String. So how did you do to print Int32, Float ? Can't it can convert it on the way ?
     
    And how did you do when you have to use std string for other library (my example is regex) ?

    Maybe my arm is in one of my eyes ?? I'm doing it wrong ?

    Thank in advance !! ;-)



  • On 29/10/2015 at 15:21, xxxxxxxx wrote:

    FloatToString() and IntToString().

    The String type in C4D is only useful in C4D.  In order to do other things (like regex), you need first to get a C-style string (CHAR* ) using GetCString() and then set that to std string.  For Unicode, it is even better.  You get a unicode encoded Int16 block using GetUcBlock().  Have no idea how that block gets converted into a std lib wide-char string - might be system dependent as well.



  • On 30/10/2015 at 02:58, xxxxxxxx wrote:

    Hello,

    as Robert already showed there are some convenience functions to convert basic datatypes to Strings: String::FloatToString(), String::IntToString() etc. You can also use FormatNumber().

    We would like to remind all that one should not use classes from the standard library if possible. If it is needed to convert a String to a std::string this can be done with GetCStringCopy():

      
    // This example converts the given String into a C-String to use it with the standard library.  
      
    const String string = "foobar";  
      
    // convert to c string and insert into std string.  
    Char* cstring = string.GetCStringCopy();  
    std::string stdString(cstring);  
      
    DeleteMem(cstring);  
    

    But if you want to use regular expressions you could also use the build-in class RegularExprParser.

    Best wishes,
    Sebastian



  • On 30/10/2015 at 04:01, xxxxxxxx wrote:

    Many thanks for the code snippet. I'll keep in mind. Maybe you have the inversed one. I can't make work String::SetCString() ?

    I did one myself to convert String to string... Beware it's ugly.

      
    std::string timer_visibility::myToStringC(String& str){  
      
     std::string buffer;  
     buffer.clear();  
      
     UInt32 j = str.GetLength() - 1;  
     for (UInt32 i = 0; i <= j; i++)  
         buffer.push_back(str);  
      
     return buffer;  
    }  
    

    This is what I want to do, this is simple. I would like to enter frames number in a STRING resources and parse it like a printer dialog. So you can display and hide an object with this string 12,15,20-25,40

    This way your object, with the selected tag, will be visible on the frame 12 , 15 and range from 20 to 25 and finally only visible on frame 40.

    So i need to verify value from user (to be sure of what he put in) with this regex "([0-9]+)([,-]{1}[0-9]+)*"

    And i need to split out my visibility string by coma(,) with a sregex_token_iterator. With all this groups i can find easily if the group is only one number. So it only one frame. Or if it's multi number with hyphen and this mean a range of frames.

    Maybe I'm creating a new wheel. Useful or not. My code is a bit dirty for the moment but almost working. The last split with hyphen (-) not work but I'll figure it out.

    I'll optimize my whole code after with your recommendations.



  • On 30/10/2015 at 06:12, xxxxxxxx wrote:

    And to roll back i do this. Ugly too.

      
    String timer_visibility::myToStringC4D(std::string& str){  
      
      String output;  
      char buffer;  
      UInt32 l = str.length();  
      
      for (UInt32 i = 0; i < l; i++)  
          output.Insert(i, Utf32Char(str _) );  
      
      return output;  
    }  
    _  
    


  • On 30/10/2015 at 10:08, xxxxxxxx wrote:

    Hello,

    this code seems to work perfectly fine:

      
    std::string stdString("foo bar");  
      
    String str;  
    str.SetCString(stdString.c_str(), stdString.length());  
      
    GePrint(str);  
    

    best wishes,
    Sebastian



  • On 30/10/2015 at 10:12, xxxxxxxx wrote:

    Hi,

    > So how did you do to print Int32, Float ?
    here is my older code for C4D String that allow to print Int, Float and other types.

    https://github.com/PluginCafe/utilities-and-snippets/blob/master/C%2B%2B/Remo/C4DPrintPublic.h



  • On 02/11/2015 at 04:29, xxxxxxxx wrote:

    Hi,

    Remotion4D. Thanks, your link and making this code public helped me to understand many things. I added it to my library

    S_Bach, greatful snippet ! I replace my own with your ! It's seems work perfectly.

    And Kuroyume0161 thanks your write. It just take me one day to remember to use String:: namespace...

    So String::FloatToString(); and String::IntToString(); are now my best friends.

    Thanks all, I'm a bit more confortable with C++ and the API now. So I see now how my question was simple. But it could be useful for anyone like me who didn't make c++ from many time and who discover in same time this API.


Log in to reply