954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Split / explode-like function

Hello,
I need to decode certain packets which I need to split first. They are splitted using '@@@' (in this example I only use one '@').

I'm having this code now: (It's basic C code for a NDS platform)

char buf2[256] = "test1@test2@test3@";
char packets[30][256];

int main(int argc, char ** argv)
{
	split(buf2);
	//here show code for NDS
}

void split(char *original)
{
  int x = 0;
  int i = 0;

  while (original[i] != '\0')
  {
        if (original[i] == '@')
        {
           packets[x][i] = '\0';
           x++;
        }
        else
        {
           packets[x][i] = original[i];
        }
     	i++;
      PA_WaitForVBL();
	}   
}


Result:
packets[0] = 'test1'
packets[1] = (nothing)
packets[2] = (nothing)

What am I doing wrong? [1] and [2] should be filled too I guess!

Thanks in advance.

Darkmystery
Newbie Poster
3 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

You might want to take a look at strtok .

mike_g
Newbie Poster
14 posts since Jul 2008
Reputation Points: 12
Solved Threads: 3
 

or, you at least need another index for the characters you put into packets. When you switch to a new x index, you need to reset the output index to 0.

void split(char *original)
{
  int x = 0;
  int i = 0;
  int jj = 0;

  while (original[i] != '\0')
  {
        if (original[i] == '@')
        {
           packets[x][jj] = '\0';
           x++;
           jj = 0;
        }
        else
        {
           packets[x][jj++] = original[i];
        }
        i++;
  }   
}


if the PA_WaitForVBL(); is necessary, what does it do?

Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116
 

And don't forget to terminate the last token when break the loop...

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 
if the PA_WaitForVBL(); is necessary, what does it do?

Well, I'm programming on the Nintendo DS. ANd if I don’t put it, the program will function, MUCH faster than the screen rate and many things just won’t work at all.

And thanks guys, I got i now. :)

I still got another question:
I load the packet command in a string, but how do I compare it? (I'm still quite new at C)
Since this piece of code is not working:

char cmd[3];
memcpy(cmd,packets,3); // cmd contains 'MSG' (confirmed)
if (cmd == "MSG") // not working
{
       PA_OutputText(1, 1, lineNum, packets); //show on DS screen
}

// not working either
if ((cmd[0] == "M") && (cmd[1] == "S") && (cmd[3] == "G"))
{
       PA_OutputText(1, 1, lineNum, packets); //show on DS screen
}


Thanks in advance again.

Darkmystery
Newbie Poster
3 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 
char cmd[3]; // 3 byte array [0] [1] [2]
memcpy(cmd,packets,3); // cmd contains 'MSG' (confirmed)
if (cmd == "MSG") // not working
// try memcmp(cmd, "MSG", 3) == 0
{
       PA_OutputText(1, 1, lineNum, packets); //show on DS screen
}

// not working either
// if the last one was cmd[2] it might work
if ((cmd[0] == "M") && (cmd[1] == "S") && (cmd[3] == "G"))
{
       PA_OutputText(1, 1, lineNum, packets); //show on DS screen
}
Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116
 
//... memcpy...
//... use memcmp(...,...,...);

//...
//...to compare two string :
int s_compare(const char *str1, const char *str2)
{
        if (strlen(str1) != strlen(str2))
		return 1;
	for (;*str1!='\0';*str1++,*str2++)
		if (*str1 != *str2)
			return 1;
	return 0;
}

// what's this...??
if ((cmd[0] == "M") && (cmd[1] == "S") && (cmd[3] == "G"))
{
       PA_OutputText(1, 1, lineNum, packets); //show on DS screen
}

// maybe, if (cmd[0] == 'M')
cikara21
Posting Whiz
340 posts since Jul 2008
Reputation Points: 47
Solved Threads: 69
 

Thanks alot guys, got it all working now. :)

Darkmystery
Newbie Poster
3 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You