bool XMLStringTokenizer::hasMoreTokens() { if (countTokens() > 0) return true; return false; }
int main(int argc,char** argv){ int used_stdin = 0; /* If no arguments are supplied, check stdin for any. */ if(*(argv+1) == NULL){ char line[MAX_LINE]; if(isatty(fileno(stdin))) fprintf(stderr,"Enter the names of one or more environment variables, seperated by spaces.\n"); if(fgets(line,MAX_LINE,stdin)){ used_stdin = 1; line[strlen(line)-1] = '\0'; int num_tokens = countTokens(line); argv = gettokens(line); argc = num_tokens + 1; } } if(argc < 2){ fprintf(stderr,"Must have at least 1 argument.\n"); fprintf(stderr,"%s\n",USAGE); exit(1); } if(!used_stdin) argv++; //increment past ./showenv showEnv(argv); exit(0); }
int getTokens(const char *instring, char ***tokens) { char currentToken[MAX_TOKEN_LENGTH]; int currentTokenNumber = 0; int tokenSize = 1; int tokenNumber = countTokens(instring); int i = 0; memset(currentToken, '\0', sizeof(currentToken)); tokens[0] = (char **)malloc(sizeof(char *) * tokenNumber+1); for(i = 0; instring[i] != '\n' && instring[i] != '\0'; i++) { if(instring[i] == ' ') { while(instring[i + 1] == ' ') i++; currentToken[tokenSize] = '\0'; tokens[0][currentTokenNumber] = (char *)malloc(sizeof(char) * (tokenSize+1)); strncpy(tokens[0][currentTokenNumber], currentToken, (tokenSize+1)); memset(currentToken, '\0', sizeof(currentToken)); currentTokenNumber++; tokenSize = 1; } else { currentToken[tokenSize - 1] = instring[i]; tokenSize++; } } currentToken[tokenSize] = '\0'; tokens[0][currentTokenNumber] = (char *)malloc(sizeof(char) * (tokenSize+1)); strncpy(tokens[0][currentTokenNumber], currentToken, (tokenSize+1)); tokens[0][tokenNumber] = NULL; return tokenNumber; }
int main (int argc, char** argv, char** envp){ /* Record the directory where the executable files are. */ init_dir = (char *)malloc(sizeof(char)*MAX_LINE); getcwd(init_dir,sizeof(char)*MAX_LINE); init_dir[strlen(init_dir)] = '\0'; /* Prompt for input until ctrl-d or exit have been entered */ char *line = (char *)malloc(sizeof(char)*MAX_LINE); fprintf(stdout,"$>"); while(fgets(line,MAX_LINE,stdin)){ line[strlen(line)-1] = '\0'; // Nip the buf int num_tokens = countTokens(line); char **tokens = (char **) malloc(sizeof(char *) * num_tokens); tokens = gettokens(line); /* exit was entered into the terminal so free heap space and quit. */ if (*tokens != NULL && strcmp("exit",tokens[0]) == 0){ freeStringArray(tokens); free(line); free(init_dir); exit(0); } /* Nothing entered in stdin. Continue prompting */ if(*tokens == NULL){ fprintf(stdout,"$> "); continue; } if (DEBUG) fprintf(stderr,"about to eval.\n"); evaluateCommand(tokens); /* free memory */ line = (char *)realloc(line,sizeof(char)*MAX_LINE); freeStringArray(tokens); fprintf(stdout,"$> "); } return 0; }
/* Parse a single net mask into an AclEntry */ static int parseAclEntry(const char* line, AclEntry* acl) { int result = 1; char* stash = NULL; char* text_; char* token; char* endptr; long octet = 0; uint8_t net_octets[4]; uint8_t mask_octets[4]; if(line == NULL || acl == NULL) return -1; if(countTokens(line,"/") == 0) return -1; text_=strdup(line); token=strtok_r(text_,"/",&stash); if((countTokens(token,".") > 4) || (countTokens(token,".") < 1) || (ipToArray(token,net_octets,4,0) < 0)) { result=-1; goto end; } acl->network = (net_octets[0] << 24) | (net_octets[1] << 16) | ( net_octets[2] << 8) | net_octets[3]; token=strtok_r(NULL,"/",&stash); if(token == NULL) { acl->netmask=32; acl->bitmask=~0; } else if(countTokens(token,".") == 1) { errno = 0; octet = strtol(token,&endptr,10); if(errno != 0 || !IN_RANGE(octet,0,32)) { result = -1; goto end; } if(octet == 0) acl->bitmask = 0; else acl->bitmask = ~0 << (32 - octet); acl->netmask = (uint16_t)octet; } else if((countTokens(token,".") > 4) || (ipToArray(token,mask_octets,4,1) < 0)) { result=-1; goto end; } else { acl->bitmask = (mask_octets[0] << 24) | (mask_octets[1] << 16) | ( mask_octets[2] << 8) | mask_octets[3]; uint32_t tmp = acl->bitmask; int count = 0; for(tmp = acl->bitmask;tmp<<count;count++); acl->netmask=count; } end: free(text_); acl->network &= acl->bitmask; return result; }
int Tokenizer::getTokenNumber( std::string str ) { setString(str); return countTokens(); }