gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > strings and directory listings?

#140453 - spinal_cord - Sun Sep 16, 2007 4:28 pm

This is bugging me a bit, I'm trying to navigate a directory list but I'm getting nowhere.
I have the filename in a string icon[file_to_load].filename[255] and im using a second variable to say if it is a folder or not.

Code:

         if(icon[file_to_load].is_folder)
         {
            sprintf(folder,"/%s",folder,icon[file_to_load].filename);
            get_file_list();
         }


get_file_list(); creates a list of files and folders in the 'folder' directory.
I assube i'm doing this wrong as it doesnt work properly.
If I was doing this in BASIC, i would

Code:

if icon[file_to_load].is_folder then
  if icon[file_to_load].filename <>".." then
    folder$=left$(folder$,len(folder$)-len(icon[file_to_load].filename)
    get_file_list()
  else
    folder$=folder$+"/"+icon[file_to_load].filename
    get_file_list()
  end if
end if


But im lost in C, please help!
_________________
I'm not a boring person, it's just that boring things keep happening to me.
Homepage

#140458 - tepples - Sun Sep 16, 2007 5:29 pm

spinal_cord wrote:
Code:
sprintf(folder,"/%s",folder,icon[file_to_load].filename);

I see two problems with this statement:
  • You have specified only one formatting argument in the format string "/%s", but you have specified a different number of arguments (two) in the list of values to be formatted.
  • I don't think it's wise to use the same variable 'folder' as both the output and an input. Unlike string operations in BASIC, Java, and Python, which create new string objects that are garbage collected, C's sprintf() modifies the output in place, and modifying things in place tends to cause problems if the function that you are calling does not expect you to use the same variable for input and output.

Quote:
Code:

if icon[file_to_load].is_folder then
  if icon[file_to_load].filename <>".." then
    folder$=left$(folder$,len(folder$)-len(icon[file_to_load].filename)
    get_file_list()
  else
    folder$=folder$+"/"+icon[file_to_load].filename
    get_file_list()
  end if
end if

Can you describe in English what this code in BASIC is supposed to do? What does folder$ represent at the start of this code, and what does it represent at the end?
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#140482 - spinal_cord - Sun Sep 16, 2007 8:43 pm

OK, in english, I have a list of filenames and weather they are folder or not. If they are folder, I want to check if it is ".." so I can then remove the current folder from the path, or if it is a folder name, then append it to the folder string.

eg.
_pseudo code_
folder="/games/lemmings"
if user selects ".." folder="/games"
else if user selects "data" folder="/games/lemmings/data"

as I said, I would like to append or remove a directory name to/from the string.
_________________
I'm not a boring person, it's just that boring things keep happening to me.
Homepage

#140514 - tepples - Sun Sep 16, 2007 11:22 pm

See strrchr.

The following code in the C language is untested, optimized for understandability (not for runtime efficiency), and subject to a potential buffer overflow if paths get really long:
Code:
#include <string.h>

/**
 * Modifies a path in place to add a name at the end.
 * If the name is "..", modifies the path to remove the last item.
 * @param path a list of items separated by '/' (slash) characters
 * @param filename the name to be added to path
 */
void addFolderToPath(char *restrict path, const char *restrict filename) {
  /* "restrict" is a new keyword added to the 1999 version of
     the C language.  Roughly, pointers passed to a function in
     restricted arguments must not point to overlapping memory. */

  /* If two strings are compared and the result is 0,
     the strings are equal. */
  if (strcmp(filename, "..") == 0) {

    /* When one tries to add the name ".." to a path, remove
       the last name in the path instead of adding "..". */

    /* Search for the last slash character in a string. */
    char *lastSlash = strrchr(path, '/');

    /* If a slash was found, chop off the end by
       replacing the slash with a NUL byte (0). */
    if (lastSlash) {
      *lastSlash = 0;
    }
  } else {
    /* When adding any name other than "..",
       add a slash followed by the name. */
    strcat(path, "/");
    strcat(path, filename);
  }
}

_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.