Beispiel #1
0
TokenizerT *TKCreate(char *separators, char *ts) {
	//the character arrays are malloced
	TokenizerT *temp = (malloc(sizeof(struct TokenizerT_)));
    temp->seps = (char*)(malloc(sizeof(char)*(strlen(separators)+1)));
    temp->stream = (char*)(malloc(sizeof(char)*(strlen(ts)+1)));
    temp->segment = (char*)(malloc(sizeof(char)*(strlen(ts)+1))); 
    
    //the separator characters are copied over to the TokenizerT seps character array
    int i = 0;
    int p;
    int is = 0;
    char tempc;
    for(; separators[i] != '\0'; i++){
    	if(separators[i]=='\\'){
    		p = i+1;
    		tempc = asciiValue(separators[p]);
    		temp->seps[is]= tempc;
    		i=1+i;
    		is++;
    	}
    	else{
        	temp->seps[is] = separators[i];
        	is++;
        }
    }
    temp->seps[is] = '\0';
    
    
    i = 0;
    is = 0;
    p=0;
    //the string that needs to seperated is copied over to the character array stream
    for(; ts[i] != '\0'; i++){
        if(ts[i]=='\\'){
        	p = i+1;
        	tempc = asciiValue(ts[p]);
    		temp->stream[is]= tempc;
    		i++;
    		is++; 
    	}
    	else{
        	temp->stream[is] = ts[i];
        	is++;
        }
    }
    
    temp->stream[is] = '\0';
    
    temp->curr = -1;
    temp->delim = -1;
    
  return temp;
}
Beispiel #2
0
/* Post: Return the value if it is a valid number 
		 Return -1, if the input is invalid */
int ascii2Int(char c[]) 
{
	int first, last; // position of first and last digits
	int value;       // value of number

	/* Have first point to the first digit if it exists */
	first = 0;
	while (asciiValue(c[first]) == -1 && c[first] != '\0') first++;
	if (c[first] == '\0') return -1;

	/* Have last point to the last digit */
	last = first;
	while (asciiValue(c[last]) != -1 && c[last] != '\0') last++;
	last--; 

	/* Compute the value */
	value = 0;
	for(value = 0; first <= last; first++) 
		value = 10* value + asciiValue(c[first]);

	return value;
}