Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <malloc.h>
- #include <string.h>
- #ifndef DEBUG_MODE
- #define DEBUG_MODE 0
- #endif
- #define DEBUG(format, ...) \
- do { \
- if (DEBUG_MODE==1) { \
- printf( format "\n", ##__VA_ARGS__); \
- } \
- } while (0)
- void sub_str( char *srcTemp, char *dstTemp, int chrStart, int strLength)
- {
- int strCount;
- strCount = 0;
- while ( (strLength != 0) || (*srcTemp != '\0')) {
- if (strCount >= chrStart)
- {
- *dstTemp = *srcTemp;
- // DEBUG("%s[%d]: s- %s d- %s ", __FUNCTION__,
- // __LINE__, *srcTemp, *dstTemp);
- }
- DEBUG("%s[%d]: LOOP(%d)", __FUNCTION__, __LINE__, strCount);
- strCount++;
- *srcTemp++;
- *dstTemp++;
- --strLength;
- }
- *dstTemp = '\0';
- }
- void token_get( char *srcTemp, char *dstTemp, char *tokChr, int tokNum )
- {
- int tokCnt;
- while ( (*srcTemp != '\0') || (tokNum != 0))
- {
- if (*srcTemp == *tokChr)
- { tokCnt++;
- tokNum--;
- }
- if (tokNum > 0) {
- DEBUG("%s[%d]: s- %s d- %s ", __FUNCTION__,
- __LINE__, *srcTemp, *dstTemp);
- *dstTemp = *srcTemp;
- *dstTemp++;
- }
- *srcTemp++;
- }
- *dstTemp = '\0';
- }
- int main(void) {
- // Tokenizer demonstation
- char tok0_src[] = "abcdef123gijkl";
- char *tok0_dest = malloc( 4 );
- char tok1_src[] = "!SUBSTRINGS RULE!";
- char *tok1_dest = malloc( strlen( tok1_src)-1 );
- char *t = "!";
- sub_str(tok0_src, tok0_dest, 7, 9);
- printf( "%s[%d]: %s\n", __FILE__, __LINE__, tok0_dest);
- token_get( tok1_src, tok1_dest, t, 1);
- printf( "%s[%d]: T: %s\n", __FILE__, __LINE__, tok1_dest);
- }
Add Comment
Please, Sign In to add comment