Date: 2011jun25
Language: C/C++
Q. How do I encode a file name to be a URL?
A. Here are two routines that can do that:
// Helper function
inline char * Shuffle(char * dest, const char * src)
{
return (char *) memmove(dest, src, strlen(src) + sizeof(char));
}
void EncodeForUrl(char * s)
{
char szHex[] = "%??"; // We depend on the percent symbol at szHex[0]
const int lenHex = strlen(szHex);
if (s == NULL) return;
for (char * p = s; *p;)
{
if (isalnum(*p)))
{
p++;
}
else
{
Shuffle(p+lenHex, p+1);
sprintf(szHex+1, "%02X", *p);
memcpy(p, szHex, lenHex);
p += lenHex;
}
}
}
// Turn %3A into colon, and %5C into backslash, etc
void DecodeFromUrl(char * s)
{
char szHex[] = "??"; // We depend on the NUL character at szHex[2]
char * dummy;
if (s == NULL) return;
for (char * p = s; *p; p++)
{
if (*p == '%')
{
szHex[0] = p[1];
szHex[1] = p[2];
*p = (char) strtol(szHex, &dummy, 16);
Shuffle(p+1, p+3);
}
}
}
void ExampleUse()
{
// Make a URL with a filename
char filename[MAX_PATH] = "c:\\windows\\fonts\\arial.ttf";
char url[MAX_PATH*3];
EncodeForUrl(filename);
_snprintf(url, sizeof(url), "dosomething.cgi?filename=%s", filename);
}
| What this info useful to you? You can donate to say thanks |
Add a comment
Sign in to add a comment