OSDev.org
https://forum.osdev.org/

Problems with function to canonicalize a path.
https://forum.osdev.org/viewtopic.php?f=1&t=32533
Page 1 of 1

Author:  yuuma [ Thu Nov 02, 2017 2:30 pm ]
Post subject:  Problems with function to canonicalize a path.

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:

Code:
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;
}

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/