void freeStack(stack_type *stack) { if (stack == NULL) return; while (!Sempty(stack)) free(pop(stack)); free(stack); }
/* * Copies the source string to the destination, converting lowercase letters * to uppercase letters in the process. */ void Stoupper(char *destination, char *source) { int i; int length; if(destination==0) { return; } if(source==0) { Sempty(destination); return; } if(destination!=source) { (void)strcpy(destination,source); } length=strlen(destination); for (i=0; i<length; i++) { if (islower(destination[i])) destination[i] = toupper(destination[i]); } }
void *pop(stack_type *stack) { SNode *p; void *val; if (Sempty(stack)) { printf("Stack overflow\n"); exit(0); } val = stack->top->element; p = stack->top; stack->top = stack->top->link; free(p); return val; }
int SnoBlank(char *result, char *source) { int got; if(result==0) return -1; else if(source==0) { Sempty(result); return 0; } got=Sextract(source,1," \t","","",0,result); if(got==1) return 0; else return -1; }
/* * Extract the word in the specified position. * Uses the same parameters as Swords() and follows * the same parsing rules. */ int Sextract(char *text, int condense, char *separator, char *grouper, char *escaper, int position, char *word) { int nword; /* number of words already found */ int wplace; /* set if we are in the middle of a word */ int tplace; int tlength; int nparen; char paren[MAX_PAREN]; int group; int ngrouper; int nescaper; int nseparator; Sempty(word); if(text==0) return -1; tplace=0; wplace=0; nword=0; nparen=0; tlength=Slength(text); ngrouper=Slength(grouper)/2; nseparator=Slength(separator); nescaper=Slength(escaper); for(tplace=0; tplace<tlength; tplace++) { /* * are we inside a group? If so, see if this is the end. */ if(nparen>0) { if(GrouperStop(ngrouper,grouper,paren[nparen-1],text[tplace])>=0) { if(nword==position) { word[wplace]=text[tplace]; } wplace++; nparen--; continue; } } /* * Is this the start of a group? */ group=GrouperStart(ngrouper,grouper,text[tplace]); if(nparen<MAX_PAREN && group>=0) { if(nword==position) { word[wplace]=text[tplace]; } wplace++; paren[nparen]=text[tplace]; nparen++; continue; } /* * Is this an escape character? */ if(nparen<=0 && Sisoneof(escaper,text[tplace])>=0) { tplace++; if(nword==position) { word[wplace]=text[tplace]; } wplace++; continue; } /* * Is this a separator character? */ if(nparen<=0 && Sisoneof(separator,text[tplace])>=0) { if((!condense) || (wplace!=0)) { if(nword==position) word[wplace]=0; nword++; if(nword>position) break; wplace=0; } continue; } /* * Just a regular old character. Put it in the output word. */ if(nword==position) { word[wplace]=text[tplace]; } wplace++; } if(wplace>0) { if(nword==position) word[wplace]=0; nword++; } if(nword>position) return 0; else return -1; }