Board index » cppbuilder » Paths into URL

Paths into URL


2005-06-15 09:50:05 PM
cppbuilder96
Hello NG,
How can I translate a File Path into an URL?
I mean something like that:
C:\Program files\MyApp\Data\File.txt
into:
file://C|:/Program files/My App/Data/File.txt
--
Michele Santucci
===================
Celin Avio S.r.l.
Software Dev. Manager
--------------------------------------
tel. +390187564080
Via Fontevivo 21/M
19126 La Spezia
===================
 
 

Re:Paths into URL

Michele Santucci wrote:
Quote
Hello NG,

How can I translate a File Path into an URL?
I mean something like that:
C:\Program files\MyApp\Data\File.txt
into:
file://C|:/Program files/My App/Data/File.txt

This is not an internet problem but standard c++.
And mind the spaces in file names:
#include <string>
#include <functional>
#include <algorithm>
#include <iostream>
class same : public std::unary_function<char, bool>
{
char rc;
public:
same(const char& c) : rc(c) { }
bool operator() (const char& x) { return x == rc; }
};
int main()
{
std::string s("C:\\Program Files\\My App\\Data\\File.txt");
std::string::size_type n = s.find(" ");
while(n != std::string::npos)
{
s.replace(n, 1, "%20");
n = s.find(" ", n + 3);
}
std::replace_if(s.begin(), s.end(), same('\\'), '/');
s = "file:///" + s;
std::cout << s << std::endl;
}
 

Re:Paths into URL

"Michele Santucci" < XXXX@XXXXX.COM >wrote in message
Quote
How can I translate a File Path into an URL?
Look at StringReplace(), ie:
AnsiString Path = "C:\\Program Files\\MyApp\\Data\\File.txt";
TReplaceFlags flags = TReplaceFlags() << rfReplaceAll;
AnsiString Temp = StringReplace(Path.SubString(4, MaxInt), "\\", "/",
flags);
Temp = StringReplace(Temp, " ", "%20", flags);
// other conversions as needed..
AnsiString URL;
URL.sprintf("file://%c|/%s", Path[1], Temp.c_str());
With that said, why do you need to translate it? HTTP browsers already
recognize normal file paths.
Gambit
 

{smallsort}