Guest User

Untitled

a guest
Feb 20th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <string.h>
  4.  
  5. #ifndef DEBUG_MODE
  6. #define DEBUG_MODE 0
  7. #endif
  8.  
  9. #define DEBUG(format, ...) \
  10. do { \
  11. if (DEBUG_MODE==1) { \
  12. printf( format "\n", ##__VA_ARGS__); \
  13. } \
  14. } while (0)
  15.  
  16. void sub_str( char *srcTemp, char *dstTemp, int chrStart, int strLength)
  17. {
  18. int strCount;
  19. strCount = 0;
  20. while ( (strLength != 0) || (*srcTemp != '\0')) {
  21. if (strCount >= chrStart)
  22. {
  23. *dstTemp = *srcTemp;
  24. // DEBUG("%s[%d]: s- %s d- %s ", __FUNCTION__,
  25. // __LINE__, *srcTemp, *dstTemp);
  26. }
  27. DEBUG("%s[%d]: LOOP(%d)", __FUNCTION__, __LINE__, strCount);
  28.  
  29. strCount++;
  30. *srcTemp++;
  31. *dstTemp++;
  32. --strLength;
  33.  
  34. }
  35. *dstTemp = '\0';
  36. }
  37.  
  38. void token_get( char *srcTemp, char *dstTemp, char *tokChr, int tokNum )
  39. {
  40. int tokCnt;
  41. while ( (*srcTemp != '\0') || (tokNum != 0))
  42. {
  43. if (*srcTemp == *tokChr)
  44. { tokCnt++;
  45. tokNum--;
  46. }
  47.  
  48. if (tokNum > 0) {
  49. DEBUG("%s[%d]: s- %s d- %s ", __FUNCTION__,
  50. __LINE__, *srcTemp, *dstTemp);
  51. *dstTemp = *srcTemp;
  52. *dstTemp++;
  53. }
  54.  
  55. *srcTemp++;
  56. }
  57.  
  58.  
  59. *dstTemp = '\0';
  60. }
  61.  
  62. int main(void) {
  63.  
  64. // Tokenizer demonstation
  65. char tok0_src[] = "abcdef123gijkl";
  66. char *tok0_dest = malloc( 4 );
  67. char tok1_src[] = "!SUBSTRINGS RULE!";
  68. char *tok1_dest = malloc( strlen( tok1_src)-1 );
  69. char *t = "!";
  70.  
  71. sub_str(tok0_src, tok0_dest, 7, 9);
  72. printf( "%s[%d]: %s\n", __FILE__, __LINE__, tok0_dest);
  73.  
  74. token_get( tok1_src, tok1_dest, t, 1);
  75. printf( "%s[%d]: T: %s\n", __FILE__, __LINE__, tok1_dest);
  76.  
  77. }
Add Comment
Please, Sign In to add comment