[Vala] Simple filewalker code. Comments?



This is working filewalker code, but I'm hoping someone will tell me if this is the correct approach.

// valac --pkg gio-2.0 listdir.vala
// ./listdir

//TODO   rewrite the code to use this to specify the action.
delegate void ProcessFile (string fileName);

// Walk the paths descendant from the given position, applying an action
// to each regular file encountered.
void  walker   (string path)
{
   var   filD  =  File.new_for_path(path);
   if (!filD.query_exists())
{ stderr.printf ("ProcessFile: Dir does not exist \"%s\"\n", path); }

   Dir   d;
   string?  name;
   try   {  d     =  Dir.open(path);   }
   catch
   {  stderr.printf ("Dir %s was not openable.\n", path);
      return;
   }
   while ((name = d.read_name()) != null)
   {  var   fil   =  File.new_for_path(path);
      if (!fil.query_exists())
{ stderr.printf ("ProcessFile: File does not exist \"%s\"\n", path);
         continue;
      }
      string   here  =  Path.build_filename(path, name);
      if (FileUtils.test(here, FileTest.IS_SYMLINK) )
      {  stdout.printf ("Symlink %s was not followed.\n", here);  }
      else if (FileUtils.test(here, FileTest.IS_REGULAR) )
      {  walkerAct (here); }
      else if (FileUtils.test (here, FileTest.IS_DIR) )
      {  walker (here); }
   }

}  //end void  walker   (string path)

// The thing to do to each regular file
void  walkerAct(string path)
{  stdout.printf("%s\n", path);  }

// List current directory
void  main()
{ var   fil   =  File.new_for_path(".");
   string   here  =  fil.resolve_relative_path(".").get_path();
   stdout.printf("At: %s\n", here);
   walker (here);
}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]