Split / explode-like function

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Dec 2008
Posts: 3
Reputation: Darkmystery is an unknown quantity at this point 
Solved Threads: 0
Darkmystery Darkmystery is offline Offline
Newbie Poster

Split / explode-like function

 
0
  #1
Dec 23rd, 2008
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)

  1. char buf2[256] = "test1@test2@test3@";
  2. char packets[30][256];
  3.  
  4. int main(int argc, char ** argv)
  5. {
  6. split(buf2);
  7. //here show code for NDS
  8. }
  9.  
  10. void split(char *original)
  11. {
  12. int x = 0;
  13. int i = 0;
  14.  
  15. while (original[i] != '\0')
  16. {
  17. if (original[i] == '@')
  18. {
  19. packets[x][i] = '\0';
  20. x++;
  21. }
  22. else
  23. {
  24. packets[x][i] = original[i];
  25. }
  26. i++;
  27. PA_WaitForVBL();
  28. }
  29. }

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.
Last edited by Darkmystery; Dec 23rd, 2008 at 12:00 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 14
Reputation: mike_g is an unknown quantity at this point 
Solved Threads: 3
mike_g mike_g is offline Offline
Newbie Poster

Re: Split / explode-like function

 
0
  #2
Dec 23rd, 2008
You might want to take a look at strtok.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 639
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 104
Murtan Murtan is offline Offline
Practically a Master Poster

Re: Split / explode-like function

 
0
  #3
Dec 23rd, 2008
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.

  1. void split(char *original)
  2. {
  3. int x = 0;
  4. int i = 0;
  5. int jj = 0;
  6.  
  7. while (original[i] != '\0')
  8. {
  9. if (original[i] == '@')
  10. {
  11. packets[x][jj] = '\0';
  12. x++;
  13. jj = 0;
  14. }
  15. else
  16. {
  17. packets[x][jj++] = original[i];
  18. }
  19. i++;
  20. }
  21. }

if the PA_WaitForVBL(); is necessary, what does it do?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Split / explode-like function

 
0
  #4
Dec 23rd, 2008
And don't forget to terminate the last token when break the loop...
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 3
Reputation: Darkmystery is an unknown quantity at this point 
Solved Threads: 0
Darkmystery Darkmystery is offline Offline
Newbie Poster

Re: Split / explode-like function

 
0
  #5
Dec 23rd, 2008
Originally Posted by Murtan View Post
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:
  1. char cmd[3];
  2. memcpy(cmd,packets,3); // cmd contains 'MSG' (confirmed)
  3. if (cmd == "MSG") // not working
  4. {
  5. PA_OutputText(1, 1, lineNum, packets); //show on DS screen
  6. }
  7.  
  8. // not working either
  9. if ((cmd[0] == "M") && (cmd[1] == "S") && (cmd[3] == "G"))
  10. {
  11. PA_OutputText(1, 1, lineNum, packets); //show on DS screen
  12. }

Thanks in advance again.
Last edited by Darkmystery; Dec 23rd, 2008 at 3:34 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 639
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 104
Murtan Murtan is offline Offline
Practically a Master Poster

Re: Split / explode-like function

 
0
  #6
Dec 23rd, 2008
  1. char cmd[3]; // 3 byte array [0] [1] [2]
  2. memcpy(cmd,packets,3); // cmd contains 'MSG' (confirmed)
  3. if (cmd == "MSG") // not working
  4. // try memcmp(cmd, "MSG", 3) == 0
  5. {
  6. PA_OutputText(1, 1, lineNum, packets); //show on DS screen
  7. }
  8.  
  9. // not working either
  10. // if the last one was cmd[2] it might work
  11. if ((cmd[0] == "M") && (cmd[1] == "S") && (cmd[3] == "G"))
  12. {
  13. PA_OutputText(1, 1, lineNum, packets); //show on DS screen
  14. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 338
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 66
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: Split / explode-like function

 
0
  #7
Dec 23rd, 2008
  1. //... memcpy...
  2. //... use memcmp(...,...,...);
  3.  
  4. //...
  5. //...to compare two string :
  6. int s_compare(const char *str1, const char *str2)
  7. {
  8. if (strlen(str1) != strlen(str2))
  9. return 1;
  10. for (;*str1!='\0';*str1++,*str2++)
  11. if (*str1 != *str2)
  12. return 1;
  13. return 0;
  14. }
  15.  
  16. // what's this...??
  17. if ((cmd[0] == "M") && (cmd[1] == "S") && (cmd[3] == "G"))
  18. {
  19. PA_OutputText(1, 1, lineNum, packets); //show on DS screen
  20. }
  21.  
  22. // maybe, if (cmd[0] == 'M')
Last edited by cikara21; Dec 23rd, 2008 at 4:29 pm.
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 3
Reputation: Darkmystery is an unknown quantity at this point 
Solved Threads: 0
Darkmystery Darkmystery is offline Offline
Newbie Poster

Re: Split / explode-like function

 
0
  #8
Dec 23rd, 2008
Thanks alot guys, got it all working now.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 840 | Replies: 7
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC