Backslash in HBuf using Copy()
| Thu, 2005-02-17 22:57 | |
|
MatD Hi, in relation to a former post [url]http://forum.newlc.com/viewtopic.php?t=4690[/url] I've made concerning \\, I would like to know if someone ever experienced problems when trying to use the Copy() method of a HBufC Descriptor, in order to receive a copy of a _LIT or TDesC& containing \\ ? It's pretty annoying when trying to pass application pathes (eg: :\\system\\Apps\\myApp\\ ) into methods. Could someone tell me for instance, if your reading a path (eg: C:\\myApp) from a file it will be return as a HBufC* and then if it's copied to an another HBufC, what the Symbian internal result will be ? (ok i already know the stuff with the escape chars). Look : if my prog reads from a file the following string : \\system\\Apps\\myText.txt then passes it to a HBuf* fileName (through Copy()) ( fileName will contain \system\Apps\myText.txt) and when trying to take this HBuf* to open a second file Open(fileName). It won't be able to open the file, because in fileName there will be (\system\Apps\myText.tx) and in this case '\' is detected as an escape char How should it be made correctly ? Thx a lot in advance ! MatD |
|






Forum posts: 192
Forum posts: 93
but just tell me one thing : is the Copy() method internaly cutting slashes or not ?
MatD
Forum posts: 1379
I don't get why this problem is this complicated though. Why escape \'s anyway?? It's not like say \n - where "\n" is different to \n. \ means \, regardless. The only reason you need to escape it when you write it in code is because someone also chose \ to mean escape the next characher.
In your XML, you could just put:
":\system\Apps\myApp\"
And that would work fine.
Where as you couldn't do:
"Some text ending with a newline\n"
And expect the \n to be seen as a newline when its read in because what it really says is \\n - i.e. "\n", a slash followed by an n.
If you want to be able to write either \\ or \ in your XML, just do something like this:
// Swap "\n" for '\n' and "\\" for "\"
HBufC* CParseManager::UnescapeXMLStringL(const TDesC& aDes)
{
_LIT(KFakeCr, "\\n");
HBufC* pRet = aDes.AllocL();
TPtr ptr(pRet->Des());
TInt nPos = 0;
while((nPos = pRet->Find(KFakeCr)) >= 0)
{
ptr.Delete(nPos, 2);
ptr.Insert(nPos, KCr);
}
_LIT(KFakeSlash, "\\\\");
_LIT(KSlash, "\\");
nPos = 0;
while((nPos = pRet->Find(KFakeSlash)) >= 0)
{
ptr.Delete(nPos, 2);
ptr.Insert(nPos, KSlash);
}
return pRet;
}
Which will swap any "\\n" for \n and any \\ for \. Thats what I use in an XML parser I developed ages ago, and it works fine.
didster