1 /**************************************************************
2  *         _____    __                       _____            *
3  *        /  _  \  |  |    ____  ___  ___   /  |  |           *
4  *       /  /_\  \ |  |  _/ __ \ \  \/  /  /   |  |_          *
5  *      /    |    \|  |__\  ___/  >    <  /    ^   /          *
6  *      \____|__  /|____/ \___  >/__/\_ \ \____   |           *
7  *              \/            \/       \/      |__|           *
8  *                                                            *
9  **************************************************************
10  *    (c) Free Lunch Design 2003                              *
11  *    Written by Johan Peitz                                  *
12  *    http://www.freelunchdesign.com                          *
13  **************************************************************
14  *    This source code is released under the The GNU          *
15  *    General Public License (GPL). Please refer to the       *
16  *    document license.txt in the source directory or         *
17  *    http://www.gnu.org for license information.             *
18  **************************************************************/
19 
20 
21 
22 
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include "token.h"
27 #include "main.h"
28 
29 
30 ////////////////////////////////////////////////////////////////
31 // token handling
32 ////////////////////////////////////////////////////////////////
33 
34 // creates a new token
create_token(char * word)35 Ttoken *create_token(char *word) {
36     Ttoken *tok = malloc(sizeof(Ttoken));
37     if (tok != NULL) {
38         tok->word = strdup(word);
39         tok->next = NULL;
40     }
41     return tok;
42 }
43 
44 
45 // frees a token
destroy_token(Ttoken * t)46 void destroy_token(Ttoken *t) {
47     free(t->word);
48     free(t);
49 }
50 
51 
52 // frees all tokens
flush_tokens(Ttoken * head)53 void flush_tokens(Ttoken *head) {
54 	if (head == NULL) return;
55     if (head->next != NULL) flush_tokens((Ttoken *)head->next);
56     destroy_token(head);
57 }
58 
59 
60 // inserts a token last in the list
insert_token(Ttoken * list,Ttoken * t)61 void insert_token(Ttoken *list, Ttoken *t) {
62     if (list->next == NULL)
63         list->next = (struct Ttoken *)t;
64     else insert_token((Ttoken *)list->next, t);
65 }
66 
67 
68 // tokenizes the string str
tokenize(char * str)69 Ttoken *tokenize(char *str) {
70     Ttoken *tok_list, *tok_tmp = NULL;
71     char word[256];
72     int a, b, c;
73     int i = 0;
74 
75     tok_list = create_token("head");
76 
77     while(str[i] != '\0') {
78         // skip leading whitespace
79 		while(str[i] == ' ' || str[i] == '\t') i++;
80 
81 		if (str[i] == '"') { // find end of string part
82 			i ++;
83 			a = i;
84 			while(str[i] != '"') i++;
85 			b = i;
86 			i ++;
87 		}
88 		else { 		// find end of word
89 			a = i;
90 			while(str[i] != ' ' && str[i] != '\t' && str[i] != '\0') i++;
91 			b = i;
92 		}
93 
94 		// copy string to word
95         for(c=0;c<b-a;c++) word[c] = str[a+c];
96 		word[c] = '\0';
97 
98         // make token and put it in the list
99 		tok_tmp = create_token(word);
100 		insert_token(tok_list, tok_tmp);
101     }
102 
103 	tok_tmp->word[strlen(tok_tmp->word) - 1] = '\0';
104     tok_tmp = (Ttoken *)tok_list->next;
105     destroy_token(tok_list);
106 
107     return tok_tmp;
108 }
109 
110 
111 // returns the work of the token after token
get_next_word(Ttoken * t)112 char *get_next_word(Ttoken *t) {
113 	Ttoken *next = (Ttoken *)t->next;
114 	if (next == NULL) {
115 		char buf[80];
116 		sprintf(buf, "<%s> has no next", t->word);
117 		msg_box(buf);
118 	}
119 	return next->word;
120 }
121 
122 
123 
124 // prints a token list
print_token(Ttoken * t)125 void print_token(Ttoken *t) {
126 	Ttoken *ptr = t;
127 
128 	while(ptr != NULL) {
129 		printf("%s ", ptr->word);
130 		ptr = (Ttoken *)ptr->next;
131 	}
132 }
133 
134 
135