Solved

C++ CGI Script replace space with %20

Posted on 2004-09-26
3
655 Views
Last Modified: 2012-05-05
I'm looking for a way to replace a space with %20 in a link (string) that my script will output. This is one of my first attempts at using C++ for CGI so it's probably a relatively simple problem. I have looked at URL encoding functions and I really am not going to have anything else that needs to be url encoded other than a space character so I thought simply replacing the space with %20 would be faster than implementing a complete url encoding function. Speed is an issue here since the script will be getting well over two million views a day. Also, the space can not be replaced by a + since the site that I am linking to can't seem to parse the query string with a + in it.
0
Comment
Question by:BuickFreak
3 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 12155186
If you are using VC++, you could use 'InternetCanonicalizeUrl()' to do that for you. If not, you could use

#include <string>

using namespace std;

string strURL = "http://www.somesite.com/url with spaces/index.html";

int npos = 0;
while ( -1 != ( npos = strURL.find ( ' ', npos))) { //as long as we find spaces

    strURL.replace ( npos, 1, "%20"); // replace them with %20
}
0
 
LVL 3

Accepted Solution

by:
CmdrRickHunter earned 125 total points
ID: 12155210
string or char array?

string solution:
string in = "hello world";
string out = "";
for (int inPos = 0; inPos < in.size(); inPos++) {
  if (in[inPos] != ' ')
    out += in[inPos];
  else
    out += "%20";
}

char solution:
char* in;
char* out;
#define INVALID_CHAR(c) ((c) == ' ')   /* define this to be whatever chars are invalid */
#define REPLACEMENT_STRING(c) "%20"
/* replacement string is, right now, a constant, because you wanted it to be just for spaces,
    the code, however, is capable of working with anything, rather quickly... just make
    replacment string do the "right thing".
    We will assume, for now, that all replacment strings are 3 characters
*/

char* inP = in;
int outLength = 0;
// determine how long the output string will be
while (inP) {
  if (INVALID_CHAR(*inP))
    outLength += 3;
  else
    outLength++;
  inP++;
}

// do whatever allocation code you wish, just make sure out is at least outLength +1 characters long

char* outP = out;
inP = in;
while (inP) {
  if (INVALID_CHAR(*inP)) {
     strcpy(outP, REPLACEMENT_STRING(*inP));
     outP += 3;
     inP++;
   } else {
      *outP++ = *inP++; // copy the char
   }
}


ignoring everything above, this kind of speed is not an issue for something getting hit 2 million times per day.  Thats only 23 hits per second, you've got 4ms to deliver a result.  I have coded complete URL encoders that can do this in a few microseconds per character (on average), running on a 500Mhz machine.  Remember, even if you hit memory at 60ns every single instruction, you still have 17 instructions per microsecond to work with.  And its actually difficult to make code miss caches that much.

If you're really worried about speed, use a profiler.  Find out where your slow-downs occur.  It probably wont be the URL encoding part.
0
 

Author Comment

by:BuickFreak
ID: 12155246
Thanks. That worked like a charm. I guess I am just nervous about server load. The script is looking through a monster DB to get results and I have tried several solutions from PHP to mod_perl. With my traffic increasing as much as it has over the last month, even my mod_perl solution was having trouble during peak hours.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
c++ syntax question 9 52
max float value 3 53
How do i run a c++ file? 15 47
C++ help/ Toy problem 19 31
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question