Hi, maybe it could help you:

static char ** Tokenizer (char* input, char token)
{
    int i = 0, j = 0, offset = 0;
    int cnSLASH = 0, cnLEN[100];
    char extracted[255];
    char ** returnV;
    
    //Variable Initialization
    for (i = 0; i < 100; i++)
        cnLEN[i] = 0;
    
    //It counts how much '/' your string have and
    //the size of each substring
    for (i = 0; i < StrLen(input); i++)
        if (token == input[i])
                cnSLASH++;
        else
                cnLEN[cnSLASH]++;
    
    //it allocates the memory for the array of strings
    returnV = (char**)MemPtrNew(sizeof(char*) * (cnSLASH + 1));

        for (i = 0; i < cnSLASH+1; i++)
        {
                returnV[i] = (char*)MemPtrNew(cnLEN[i]+1);
                
                for (j = 0; j < cnLEN[i]; j++)
                        returnV[i][j] = input[j+offset];

                returnV[i][j] = '\0';
                offset += cnLEN[i] + 1;
        }
        
        return returnV;
 }

EXAMPLE:

Char * test = "sdlkfslkdj/gowrsdn/sfgmbdf�sdfg/sdfgfdglk";
char search ='/';
char ** temp;

temp = Tokenizer (test, search);

DON�T FORGET TO FREE THE TEMP VARIABLE AFTER USE IT!!!!!!

This function does not need know how sub-string you will have, it makes all
by itself automaticly. I don�t know if it is the best solution but I think
it could help you.

if you have any doubt mail me.

Ricardo - Brazil. 

-----Mensagem original-----
De: Pit [mailto:[EMAIL PROTECTED]
Enviada em: quinta-feira, 8 de abril de 2004 10:22
Para: Palm Developer Forum
Assunto: Re: char* [] and String-Tokenizing - Problem


Hey,
first thank you for your help.

There are some point i want to mention (to ask):

I terminate the strings with the '\0'-sign, this is correct!!!

What should i use instead of MemHandleNew() ? Perhaps malloc?
Or how would you solve this?

Thanks

"Jan Slodicka" <[EMAIL PROTECTED]> schrieb im Newsbeitrag
news:[EMAIL PROTECTED]
> Hello,
>
> Here are just a few points:
>
> - Do you really want string separators? If not, then the parameter token
> should be char. If yes, then you should compare all characters of the
> string. Or you want to support multiple separators? Then check them all.
(By
> the way, "token" means something else.)
>
> - How the caller will know how many strings are in the output array?
>
> - Take out the invariants out of the cycle. (StrLen)
>
> - Strings are terminated by 0. You don't take this into account.
>
> - Remarkable construction:
>     MemHandleLock (MemHandleNew (StrLen(extracted)))
>   a) inefficient
>   b) Are you sure you want blocks from the storage memory? And should they
> be locked?
>   c) What about if the allocation fails?
>
> - If you want a general solution, then you cannot rely on assumptions such
> as max. token size 255 or sufficient length of the output array.
>
> CW is not the fault - for sure.
> Also, it shouldn't be any problem to find ready-to-use tokenizers on the
> web.
>
> With best regards,
>     Jan Slodicka
>
>
>
> ----- Original Message -----
> From: "Pit" <[EMAIL PROTECTED]>
> Newsgroups: palm-dev-forum
> To: "Palm Developer Forum" <[EMAIL PROTECTED]>
> Sent: Thursday, April 08, 2004 12:18 PM
> Subject: char* [] and String-Tokenizing - Problem
>
>
> > Hi all,
> >
> > i am working on a function which separates a given string in multiple
> > substrings.
> >
> > Example:
> >     given is the following string:
> >     Char * temp = "sdlkfslkdj/gowrsdn/sfgmbdf�sdfg/sdfgfdglk";
> >
> >     i want then following:
> >     Char * temp1[] = {"sdlkfslkdj", "gowrsdn", "sfgmbdf�sdfg",
> "sdfgfdglk"};
> >
> > Therfore i wrote a function who should handle this!
> >
> > static void Tokenizer (char* input, char* token, char* returnV[]) {
> >         int i, j = 0, k = 0;
> >         char extracted[255];
> >
> >         for (i = 0; i <= StrLen(input); i++)
> >         {
> >              if (*token == input[i] || i == StrLen(input) )
> > // terminate if '/' found or end of string reached
> >              {
> >                   extracted[j] = '\0';
> > // terminate extracted String
> >                   returnV[k] = MemHandleLock (MemHandleNew (StrLen
> > (extracted)));         // Allocate memory for new String
> >                   StrNCopy (returnV[k], extracted, StrLen (extracted));
> > // Copy String into allocated memory
> >                   j = 0;
> > // skip to start of extracted-string
> >                   i++;
> > // skip '/'
> >                   k++;
> > // skip one position in returnV-Array
> >              }
> >              extracted[j] = input[i];
> >              j++;
> >         }
> > }
> >
> > i call this function with the following code:
> >
> >  char* test =
> > "1peterg/dewrrdfg3/wesjrhsdhf/ksdhfjk/eritjeklgjlsdkg";
> >  char* search ="/";
> >  char* temp[] = {NULL, NULL, NULL, NULL, NULL};
> >  Tokenizer (test, search, temp);
> >
> > after the call of Tokenizer, temp shows the following values:
> >
> > temp[] = {"sdlkfslkdj", "gowrsdn", "sfgmbdf�sdfg", "sdfgfdglk�" ��" �",
> > NULL};
> >
> > As you can see is the 4. value not correct, there seems to be some
memory
> > bullshit.
> > It occurs form times to times, and also with other bullshit.
> > Is this perhaps a problem of CW or am i doing somethins wron with the
> > memory-allocation?
> >
> > Thanks, for any suggestions
> > Best Regards
> > Peter
> >
> >
> >
> > --
> > For information on using the Palm Developer Forums, or to unsubscribe,
> please see http://www.palmos.com/dev/support/forums/
>
>



-- 
For information on using the Palm Developer Forums, or to unsubscribe,
please see http://www.palmos.com/dev/support/forums/

--
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/support/forums/

Reply via email to