0

After exploring the internet, i've wrote the following script, but still got errors, anyone knows what the errors are?( the line with //err)

typedef struct name_value_set {
    char name[250]; 
    char value[250]; 
} nv_set;

char * getInput2(char *param) {
    char *my_data=0;
    int data_len;
    char *tmp_ptr, *tmp;
    int i;
    nv_set *nv;

    data_len=atoi(getenv("CONTENT_LENGTH"));
    char field[data_len]; 

    my_data=(char*)malloc(sizeof(char)*(data_len+1));
    fread(my_data,1,data_len,stdin);

    i=0;
    while (my_data[0]!='\0') {
     tmp=split(my_data,'='); // err
     makespace(tmp); 
     tmp=convert(tmp); // err
     strcpy(nv[i].name,tmp); // 
     tmp=split(my_data,'&'); //err 
     makespace(tmp); // 
     tmp=convert(tmp); // err
     strcpy(nv[i].value,tmp); // 
     i++;
    }
    i--; // 

    int j=0;
    for (j=0; j<i; j++) {
    if(nv[j].name == param) {
        return nv[j].value;
    }


}


void makespace(char *s)
{
    int i,len;
    len=strlen(s);

    for (i=0;i<len;i++) {
      if (s[i]=='+')
        s[i]=' ';
    }
}

char *split(char *s, char stop)
{
    char *data;
    char *tmp;
    int i,len,j;

    len=strlen(s);
    tmp=s;
    data=(char*)malloc(sizeof(char)*(len+1));

    for (i=0;i<len;i++) {
       if (s[i]!=stop)
         data[i]=s[i]; // 
      else {
        i+=1; // 
          break;
       } 
    }
    data[i]='\0';
    for (j=i;j<len;j++)
      s[j-i]=tmp[j];
      s[len-i]='\0';
     return data;
}


char *convert(char *s)
{
    int x,y,len;
    char *data;

    len=strlen(s);
    data=(char*)malloc(sizeof(char)*(len+1));
    y=0;
    for (x=0;x<len;x++) {
    if (s[x]!='%') {
        data[y]=s[x]; // 
          y++;
      }
    else {

        data[y]=(char)(16*hexa(s[x+1])+hexa(s[x+2]));
        y++;
        x=x+2;
      }
    }
     data[y]='\0';
     return data;
}


int hexa(char c)
{
    switch(c) {
    case '0':return 0;
    case '1':return 1;
    case '2':return 2;
    case '3':return 3;
    case '4':return 4;
    case '5':return 5;
    case '6':return 6;
    case '7':return 7;
    case '8':return 8;
    case '9':return 9;
    case 'A':return 10;
    case 'B':return 11;
    case 'C':return 12;
    case 'D':return 13;
    case 'E':return 14;
    case 'F':return 15;
    }
    return 0;
}
6
  • You don't say what the error is, but you may just need to forward declare your functions. The compiler parses your file in order and when it's compiling your first function it hasn't "seen" split() yet. So put extern char *split(char *, char); at the top and see if that helps. (repeat for other functions, of course) Commented Nov 1, 2010 at 5:52
  • 1
    Don't be vague, be an ace; write a proper test-case! tinyurl.com/so-hints sscce.org xs4all.nl/~weegen/eelis/iso-c++/testcase.xhtml Commented Nov 1, 2010 at 5:52
  • 1
    "still got errors" Well, what kind of errors? Are they compiler errors? Does it compile? If so, can you run it? Does it crash? Does it not work correctly? Have you tried attaching a debugger to see what is wrong with it? Commented Nov 1, 2010 at 5:52
  • all errors are "assignment makes pointer from integer without a cast" Commented Nov 1, 2010 at 5:54
  • 1
    What is this "script" supposed to do and what errors are you seeing? Commented Nov 1, 2010 at 5:54

1 Answer 1

1

Try adding prototypes for the functions "makespace", "split", "convert" and "hexa" at the beginning of your code; I expect what you're seeing is the result of implicitly declared functions conflicting with the function definitions. Also, just to be sure, you're including stdio.h and string.h in your source file, right?

Sign up to request clarification or add additional context in comments.

2 Comments

i've find out my_data got nothing~ have any ideas? data_len=atoi(getenv("CONTENT_LENGTH")); my_data=(char*)malloc(sizeof(char)*(data_len+1)); fread(my_data,1,data_len,stdin);
Okay, "my_data got nothing" is a little ambiguous, and I think it is entirely unrelated to the original question, but let's have at it anyway. I'm going to assume you mean 'my_data' is NULL after the 'malloc'. You may wish to confirm that the environment variable CONTENT_LENGTH is in fact set to a sane value, or even set at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.