2
\$\begingroup\$

For following input string:

"VISITOR_INFO1_LIVE=USv90B-7CzI; LOGIN_INFO=e486e37a395be3f0e3b3237d090a6829c1oAAAB7IjQiOiAiREVMRUdBVEVEIiwgIjciOiAwLCAiMSI6IDEsICIzIjogMjAxMTk0MTMwNiwgIjgiOiA2MDgwMTg0NTEzNjQsICIxMCI6IDIzOTYyMTEyODczNH0=; PREF=f5=30;HSID=AHuJQBOVR0lQoRt_3; APISID=QaParXGsQcEPCzKg/A1smCfYrfMjxvfEPT; YSC=Vm3Amq5loFM";

I want to remove all the patterns that contains *SID (HSID,APISID here) upto ';'. I also want to remove substring "LOGIN_INFO= ....;"

So, output string should be:

"VISITOR_INFO1_LIVE=L80EDuHCEF8; PREF=f5=30";`
 const char *str ="VISITOR_INFO1_LIVE=USv90B-7CzI; LOGIN_INFO=e486e37a395be3f0e3b3237d090a6829c1oAAAB7IjQiOiAiREVMRUdBVEVEIiwgIjciOiAwLCAiMSI6IDEsICIzIjogMjAxMTk0MTMwNiwgIjgiOiA2MDgwMTg0NTEzNjQsICIxMCI6IDIzOTYyMTEyODczNH0=; PREF=f5=30;HSID=AHuJQBOVR0lQoRt_3; APISID=QaParXGsQcEPCzKg/A1smCfYrfMjxvfEPT; YSC=Vm3Amq5loFM"; 
 char *Cookie = NULL;
 cout << "original string is:\n" << str << "\n";
 int len = strlen(str)+1;
 cout << "length of original string is : " << len << "\n";
 Cookie = new char[strlen(str)];
 strncpy(Cookie,str,len);

 char *p1 = strstr(Cookie,"LOGIN_INFO");
 char *p2 = NULL;
 if(p1){
      p2 = strstr(p1,";")+1;
      while(*p2 == ' ') p2++;
 }
 if(p1 && p2)
      memmove(p1,p2,strlen(p2)+1);

 char *ID = strstr(Cookie,"SID"); 
 while( ID != NULL){
      char *start_pos = NULL, *end_pos = NULL;    
      while((*ID != ';') && (*ID != Cookie[0]) && (*ID != ' ')){
           --ID;
      }
      if(*ID == Cookie[0]) start_pos = ID;
      else start_pos = ID+1;
      end_pos = strstr(start_pos,";")+1;
      while(*end_pos == ' ')
           end_pos++;
      memmove(start_pos,end_pos,strlen(end_pos)+1);                                   
    //  } 
      /*else
           std::cout << "does not find substr " << "\n";*/

    //  cout << "modified string is :" << Cookie << "\n";
      ID = strstr(Cookie,"SID");
 }
      //cout << "final modified string is : " << Cookie << "\n";
 char *Cookie_modified = NULL;
 const char *pch = strstr(Cookie,"PREF");
 if(pch != NULL){
      const char *append = "&f2=8000000";
      int len = strlen(Cookie) + strlen(append) + 1;
      Cookie_modified = new char[len];
      strncpy(Cookie_modified,Cookie,len);
      Cookie_modified[len-1] = '\0';
      char *p = strstr(Cookie_modified,"PREF");
      strncpy(p+(strlen(p)),append,strlen(append));
      cout << "modified Cookie is : " << Cookie_modified << "\n";
  //    cout << "length of modified cookie is : " << strlen(Cookie_modified) << "\n";
 }

 else{
      cout << "do not find reference: " << "\n";
      const char *append = ";PERF=f2=8000000";
      int len = strlen(Cookie) + strlen(append) + 1;
      Cookie_modified = new char[len];
      Cookie_modified[len-1] = '\0';
      strcat(Cookie_modified,Cookie);
      strcat(Cookie_modified,append);
      cout << "case 2: modified Cookie is: " << Cookie_modified << "\n";

 }
 delete[] Cookie;
 delete[] Cookie_modified;     
 return 0;
\$\endgroup\$
  • 2
    \$\begingroup\$ Welcome to Code Review! This question is incomplete. To help reviewers give you better answers, please add sufficient context to your question. The more you tell us about what your code does and what the purpose of doing that is, the easier it will be for reviewers to help you. See also this meta question. \$\endgroup\$ – SuperBiasedMan Feb 3 '16 at 9:31
  • \$\begingroup\$ The first obvious review comment here will be, why are you using C string handling in C++? And you have a null terminator bug here: Cookie = new char[strlen(str)];. \$\endgroup\$ – Lundin Feb 3 '16 at 15:12
  • \$\begingroup\$ echo "<String here> | awk -F; '{printf("%s %s\n",$1,3);}' \$\endgroup\$ – Martin York Feb 3 '16 at 21:43
1
\$\begingroup\$

Comment on Question

Can tell if your code matches the problem statement.
Because I can't understand the problem statement.

Input

"VISITOR_INFO1_LIVE=USv90B-7CzI; LOGIN_INFO=e486e37a395be3f0e3b3237d090a6829c1oAAAB7IjQiOiAiREVMRUdBVEVEIiwgIjciOiAwLCAiMSI6IDEsICIzIjogMjAxMTk0MTMwNiwgIjgiOiA2MDgwMTg0NTEzNjQsICIxMCI6IDIzOTYyMTEyODczNH0=; PREF=f5=30;HSID=AHuJQBOVR0lQoRt_3; APISID=QaParXGsQcEPCzKg/A1smCfYrfMjxvfEPT; YSC=Vm3Amq5loFM";

Output

"VISITOR_INFO1_LIVE=L80EDuHCEF8; PREF=f5=30";

Questions

  1. Is the first and last quote and the last semi colon part of the input or just your formatting?
  2. *SID (HSID,APISID here) upto ';'
    I see none of these things in the output. there is no SID no HSID no APISID
  3. Whey is 'VISITOR_INFO1_LIVE' in the output?
    It is not specified as something you want in the output.
  4. Why is PREF in the output.
  5. This PREF=f5=30 seems like an illegal parameter. is = allowed in the parameter.

Review of code

OK. I assume there is a function wrapper here that defines str!! What is the type of str? I am going to assume char const* as you seem to be coding using C (rather than real C++). Types are super important in C++ please make sure we know all the types of all identifiers.

Why split up the declaration from the initialization?

 char *Cookie = NULL;
 ......
 Cookie = new char[strlen(str)];
 strncpy(Cookie,str,len);
 ......
 delete[] Cookie;

This just gives you the opportunity to misuse Cookie in the code. If you declare and initialize in the same statement then no misuse.

Also C++ code should never contain a delete (especially when you are learning). It should have very few calls (if any calls) to new. Dynamic memory management should be controled via an object. This is usually a smart pointer or a container depending on your usage.

Thirdly why use the C-String API. C++ has its own string that handles memory management for you and does a better job. Currently your code is not exception safe. An exception thrown in your code will result in leaked memory.

Thirdly you actually did it wrong and did not allocate enough space (you forgot to allocate space for the '\0' character that terminates a C-String). I know you used the strncpy to compensate for this. But all the other string functions are expecting it so something is likely to go wrong.

Forgot to check the result of a string function.

      p2 = strstr(p1,";")+1;

If strstr() returns NULL then we add 1 to it and now it is no longer NULL and your next test will always work. Even if it is not supposed too.

Are you sure the second test id correct?

      while((*ID != ';') && (*ID != Cookie[0]) && (*ID != ' ')){
           --ID;
      }

I think you are testing here *ID != Cookie[0] to make sure that you don't fall off the front of the string. But you are checking the content of the characters not the position of the pointers. A better test is (ID > Cookie)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.