Plugin Cafe Homepage
Forum Home Forum Home > Plugin Cafe > SDK Help
  New Posts New Posts
  FAQ FAQ  Forum Search

String character testing issue

Author
Message
  Topic Search Topic Search
Cactus Dan View Drop Down
Member
Member
Avatar

Joined: 2003 Apr 18
Location: United States
Online Status: Offline
Posts: 1053
Direct Link To This Post Topic: String character testing issue
    Posted: 2012 Jun 11 at 7:29am

User Information:

Cinema 4D Version:   R9.6+ 
Platform:   Windows  ; Mac  ;  Mac OSX  ; 
Language(s):     C++  ;   

---------

Howdy,

I had an issue with this code:

Bool IsUsableCharacter(const String s)

{

if(s >= "a" && s <= "z") return TRUE;

if(s >= "A" && s <= "Z") return TRUE;

if(s >= "0" && s <= "9") return TRUE;

if(s == "_") return TRUE;

return FALSE;

}


... where it returns FALSE for "z", "Z" and "9" if those characters are in the middle of the string but not if they are the last character in the string.

The fix was to change the code to this:

Bool IsUsableCharacter(const String s)

{

if(s > "`" && s < "{") return TRUE;

if(s > "@" && s < "[") return TRUE;

if(s > "/" && s < ":") return TRUE;

if(s > "^" && s < "`") return TRUE;

return FALSE;

}


Why doesn't the first code work?

Adios,
Cactus Dan
Back to Top
Matthias Bober View Drop Down
Forum Moderator
Forum Moderator


Joined: 2006 Oct 16
Location: Germany
Online Status: Offline
Posts: 2204
Direct Link To This Post Posted: 2012 Jun 12 at 12:58am
Sorry, I can't reproduce this problem. My test code worked fine. Can you post some code where you pass strings to the functions?

here my test code, small async dialog:

#define ID_ASYNCTEST 1000955

#include "c4d.h"
#include "c4d_symbols.h"


class AsyncDialog : public GeDialog
{
    private:
    public:
        AsyncDialog(void);
        virtual Bool CreateLayout(void);
        virtual Bool InitValues(void);
        virtual Bool Command(LONG id,const BaseContainer &msg);
};

enum
{
    IDC_TEXT = 1000
};

AsyncDialog::AsyncDialog(void)
{
}

Bool AsyncDialog::CreateLayout(void)
{
    // first call the parent instance
    Bool res = GeDialog::CreateLayout();

    SetTitle("GuiDemo C++");

    GroupBegin(0,BFH_SCALEFIT,1,1,"",0);
    {
        GroupBorderSpace(4,4,4,4);
        AddEditText(IDC_TEXT, BFH_SCALEFIT, 400, 0, 0);
    }
    GroupEnd();

    return res;
}

Bool AsyncDialog::InitValues(void)
{
    // first call the parent instance
    if (!GeDialog::InitValues()) return FALSE;

    //SetString(IDC_TEXT, String("hello"));

    return TRUE;
}

Bool IsUsableCharacter(const String s)
{
    if(s >= "a" && s <= "z") return TRUE;
    if(s >= "A" && s <= "Z") return TRUE;
    if(s >= "0" && s <= "9") return TRUE;
    if(s == "_") return TRUE;

    return FALSE;
}

Bool AsyncDialog::Command(LONG id,const BaseContainer &msg)
{
    switch (id)
    {
        case IDC_TEXT:
            String text;
            GetString(IDC_TEXT, text);

            LONG len = text.GetLength();

            for (LONG i=0; i<len; i++)
            {
                String subtext = text.SubStr(i, 1);

                if (IsUsableCharacter(subtext))
                    GePrint(subtext);
                else
                    GePrint("fail");
            }

            break;
    }
    return TRUE;
}

class AsyncTest : public CommandData
{
    private:
        AsyncDialog dlg;
    public:
        virtual Bool Execute(BaseDocument *doc);
        virtual LONG GetState(BaseDocument *doc);
        virtual Bool RestoreLayout(void *secret);
};

LONG AsyncTest::GetState(BaseDocument *doc)
{
    return CMD_ENABLED;
}

Bool AsyncTest::Execute(BaseDocument *doc)
{
    return dlg.Open(DLG_TYPE_ASYNC, ID_ASYNCTEST, 0, 0);
}

Bool AsyncTest::RestoreLayout(void *secret)
{
    return dlg.RestoreLayout(ID_ASYNCTEST,0,secret);
}


Bool RegisterAsyncTest(void)
{
    return RegisterCommandPlugin(ID_ASYNCTEST,GeLoadString(IDS_ASYNCTEST),0,NULL,String("C++ SDK Menu Test Plugin"),gNew AsyncTest);
}


cheers,
Matthias

MAXON
developer support
Back to Top
Cactus Dan View Drop Down
Member
Member
Avatar

Joined: 2003 Apr 18
Location: United States
Online Status: Offline
Posts: 1053
Direct Link To This Post Posted: 2012 Jun 12 at 5:48am
Howdy,

AHA! I was doing this:
String subtext = text.SubStr(i, len);

... instead of like your code:
String subtext = text.SubStr(i, 1);

No wonder it wasn't working. Embarrassed

Adios,
Cactus Dan
Back to Top
Matthias Bober View Drop Down
Forum Moderator
Forum Moderator


Joined: 2006 Oct 16
Location: Germany
Online Status: Offline
Posts: 2204
Direct Link To This Post Posted: 2012 Jun 12 at 7:16am
Ah, good to hear it was a simple mistake.

cheers,
Matthias

MAXON
developer support
Back to Top

Forum Jump Forum Permissions View Drop Down

Bulletin Board Software by Web Wiz Forums® version 9.61 [Free Express Edition]
Copyright ©2001-2009 Web Wiz

This page was generated in 0.109 seconds.