Last active
November 3, 2015 16:30
-
-
Save lobeck/627d8bd5735f29d7a630 to your computer and use it in GitHub Desktop.
port_http parse test case
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string.h> | |
#include<stdio.h> | |
#include <stdlib.h> | |
char *mumu_string_replace(char *source, int *length, int can_realloc, char *toreplace, char *replacement) | |
{ | |
char *pospattern; | |
char *reallocresult; | |
char *tempstring=NULL; | |
int lengthpattern; | |
int lengthreplacment; | |
int lengthtempstring; | |
int lengthsource; | |
pospattern=strstr(source,toreplace); | |
if(pospattern==NULL) | |
return source; | |
lengthpattern=strlen(toreplace); | |
lengthreplacment=strlen(replacement); | |
lengthsource=strlen(source); | |
lengthtempstring=lengthsource+1; | |
tempstring=malloc(sizeof(char)*lengthtempstring); | |
if(tempstring==NULL) | |
{ | |
return NULL; | |
} | |
strcpy(tempstring,source); | |
pospattern=strstr(tempstring,toreplace); | |
while(pospattern!=NULL) | |
{ | |
if(lengthreplacment>lengthpattern) | |
{ | |
tempstring=realloc(tempstring,sizeof(char)*(lengthtempstring+lengthreplacment-lengthpattern+1)); | |
if(tempstring==NULL) | |
{ | |
return NULL; | |
} | |
pospattern=strstr(tempstring,toreplace); | |
} | |
memmove(pospattern+lengthreplacment,pospattern+lengthpattern,lengthtempstring-((int)(pospattern-tempstring))-lengthpattern); | |
memcpy(pospattern,replacement,lengthreplacment); | |
lengthtempstring+=lengthreplacment-lengthpattern; | |
pospattern=strstr(tempstring,toreplace); | |
} | |
tempstring[lengthtempstring-1]='\0'; | |
if(can_realloc) | |
{ | |
if(lengthtempstring>*length) | |
{ | |
reallocresult=realloc(source,sizeof(char)*(lengthtempstring)); | |
if(reallocresult==NULL) | |
{ | |
return NULL; | |
} | |
source=reallocresult; | |
*length=lengthtempstring; | |
} | |
strcpy(source,tempstring); | |
} | |
else if(lengthtempstring<=*length) | |
{ | |
strcpy(source,tempstring); | |
} | |
else | |
{ | |
strncpy(source,tempstring,*length-1); | |
source[*length-1]='\0'; | |
} | |
free(tempstring); | |
return source; | |
} | |
int string_mult(char *string) | |
{ | |
int number1,len; | |
char *multpos=NULL; | |
char *tempchar; | |
multpos=strchr(string,'*'); | |
if(multpos==NULL) | |
return atoi(string); | |
len=multpos-string; | |
tempchar=malloc(sizeof(char)*(len+1)); | |
strncpy(tempchar,string,len); | |
tempchar[len]='\0'; | |
number1=atoi(tempchar); | |
free(tempchar); | |
if(strchr(multpos+1,'*')!=NULL) | |
return number1*string_mult(multpos+1); | |
return number1*atoi(multpos+1); | |
} | |
int string_comput(char *string) | |
{ | |
int number1,len; | |
char *pluspos=NULL; | |
char *tempchar; | |
if(string==NULL) | |
return 0; | |
pluspos=strchr(string,'+'); | |
if(pluspos==NULL) | |
{ | |
len=strlen(string); | |
} | |
else | |
{ | |
len=pluspos-string; | |
} | |
tempchar=malloc(sizeof(char)*(len+1)); | |
strncpy(tempchar,string,len); | |
tempchar[len]='\0'; | |
number1=string_mult(tempchar); | |
free(tempchar); | |
if(pluspos==NULL) | |
return number1; | |
if(strchr(pluspos+1,'+')!=NULL) | |
return number1+string_comput(pluspos+1); | |
return number1+string_mult(pluspos+1); | |
} | |
main() | |
{ | |
#define CONFIG_FILE_SEPARATOR " =" | |
char delimiteurs[] = CONFIG_FILE_SEPARATOR; | |
//char foo[] = "port_http = 2000+%card*100"; | |
//char foo[] = "port_http = 2000 + %card"; | |
//char foo[] = "port_http=2000+%card"; | |
//char foo[] = " port_http = 2000+%card"; | |
char foo[] = " port_http = 2000 + %card"; | |
//char foo[] = " port_http = 2000+%card"; | |
char *foop = foo; | |
printf("%s\n", foop); | |
// begin unchangeable part / would have global impact | |
char *tok1 = strtok(foop, " ="); | |
printf("Tok1: %s\n", tok1); | |
printf("setting: %i\n", !strcmp (tok1, "port_http")); | |
printf("foop: %s\n", foop); | |
// from here it's changeable | |
char *tok2 = strtok(NULL, "="); | |
int len = strlen(tok2)+1; | |
char *rep = mumu_string_replace(tok2, &len, 1," ", ""); | |
printf("replace: \"%s\"\n", rep); | |
printf("length: %zu\n", strlen(rep)); | |
if (strlen(rep) == 0) { | |
tok2 = strtok(NULL, "="); | |
} | |
printf("Tok2: \"%s\"\n", tok2); | |
len=strlen(tok2)+1; | |
char *replaced = mumu_string_replace(tok2, &len, 1, "%card", "10"); | |
printf("replaced: %s\n", replaced); | |
int computed = string_comput(replaced); | |
printf("computed: %i\n", computed); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment