Problems with function to canonicalize a path.
Posted: Thu Nov 02, 2017 2:30 pm
EDIT: I already fixed the problem.
Hi, i'm trying to make a in-memory filesystem, everthing was great, until i try to make subdirectories, i debugged a little, and i find the problem, it was in my function to canonicalize the path, that was putting strange symbols in the final string, can some one help me with this?
Here is the function:
Hi, i'm trying to make a in-memory filesystem, everthing was great, until i try to make subdirectories, i debugged a little, and i find the problem, it was in my function to canonicalize the path, that was putting strange symbols in the final string, can some one help me with this?
Here is the function:
Code: Select all
PChar RAMFSJoinPath(PChar src, PChar incr)
{
if ((!src) && (!incr)) // We need, or src, or incr
return Null;
PList list = ListNew();
PChar roff = Null;
PChar ret = Null;
UInt32 lsize = 0;
if (incr[0] != '\0' && incr[0] != '\\') // Copy the src?
{
PChar path = StringDump(src); // YES! So lets make a StringDump() of src, as StringTokenize() change the pointer
PChar save = Null;
PChar pch = StringTokenize(path, "\\", &save);
while (pch)
{
if (StringCompare(pch, ".")) // Current directory
;
else if (StringCompare(pch, "..")) // Parent directory
{
if (list -> length) // If the list have any entry,
MemoryFree((UIntPtr)(ListRemove(list, list -> length - 1))); // Pop the last one
}
else
{
PChar str = StringDump(pch);
ListAdd(list, str);
}
pch = StringTokenize(Null, "\\", &save);
}
MemoryFree((UIntPtr)path);
}
PChar path = StringDump(incr); // Lets make a StringDump() of incr, as StringTokenize() change the pointer
PChar save = Null;
PChar pch = StringTokenize(path, "\\", &save);
while (pch)
{
if (StringCompare(pch, ".")) // Current directory
;
else if (StringCompare(pch, "..")) // Parent directory
{
if (list -> length) // If the list have any entry,
MemoryFree((UIntPtr)(ListRemove(list, list -> length - 1))); // Pop the last one
}
else
{
PChar str = StringDump(pch);
ListAdd(list, str);
}
pch = StringTokenize(Null, "\\", &save);
}
MemoryFree((UIntPtr)path);
if (list -> length == 0) // We wanna the root directory?
{
ListFree(list); // Yes!
ret = (PChar)MemoryAlloc(2);
ret[0] = '\\';
ret[1] = '\0';
return ret;
}
ListForeach(node, list) // Let's get the size of the final string
lsize = StringGetLength((PChar)node -> data);
roff = ret = (PChar)MemoryAlloc(lsize + 1);
ListForeach(node, list) // And copy everthing to it
{
StringCopy(roff, "\\");
roff++;
StringCopy(roff, (PChar)node -> data);
roff += StringGetLength((PChar)node -> data);
}
ListFree(list);
return ret;
}