rename `NativeString` to `CString`
[nit.git] / contrib / sort_downloads / src / sort_downloads.nit
index b3ae759..9b70e83 100755 (executable)
@@ -26,13 +26,18 @@ module sort_downloads
 import opts
 
 `{
-#include <errno.h>
+       #include <errno.h>
+       #include <string.h>
 `}
 
 # Local config
 # Modify these according to your needs. It is also possible to have alternates
 # using class refinment and a main calling to super.
 class Config
+       # Is it configured? Change to "true" when the configuration has been
+       # adapted to your needs and setup
+       var is_configured = false
+
        # Source directory where are the files to be sorted.
        var source_dir = "~/Downloads/"
 
@@ -58,11 +63,11 @@ redef class String
        end
 
        # Returns null on success
-       fun file_rename_to(dest: String): nullable String import String::to_cstring,
-       String::from_cstring, String as nullable `{
-               int res = rename(String_to_cstring(recv), String_to_cstring(dest));
+       fun file_rename_to(dest: String): nullable String import String.to_cstring,
+       CString.to_s, String.as nullable `{
+               int res = rename(String_to_cstring(self), String_to_cstring(dest));
                if (res == 0) return null_String();
-               return String_as_nullable(new_String_from_cstring(strerror(errno)));
+               return String_as_nullable(CString_to_s(strerror(errno)));
        `}
 
        # Replace `~` by the path to the home diretory
@@ -76,9 +81,11 @@ redef class String
        end
 end
 
-# Hack to keep track of the real directory name associated to this pattern
-redef class BM_Pattern
-       var dir: String
+# Keeps track of the real directory name associated to this pattern
+class PatternWithDir
+       super BM_Pattern
+
+       var dir: String is noinit
 
        init with_dir(motif, dir: String)
        do
@@ -138,11 +145,11 @@ class XySorter
                end
 
                # build regexxes
-               var patterns = new HashSet[BM_Pattern]
+               var patterns = new HashSet[PatternWithDir]
                for dir in dirs_name do
-                       patterns.add new BM_Pattern.with_dir(dir, dir)
-                       patterns.add new BM_Pattern.with_dir(dir.replace(' ', "."), dir)
-                       patterns.add new BM_Pattern.with_dir(dir.replace(' ', "_"), dir)
+                       patterns.add new PatternWithDir.with_dir(dir, dir)
+                       patterns.add new PatternWithDir.with_dir(dir.replace(' ', "."), dir)
+                       patterns.add new PatternWithDir.with_dir(dir.replace(' ', "_"), dir)
                end
 
                # compare source files with patterns and sort
@@ -186,6 +193,10 @@ class XySorter
 end
 
 var config = new Config
+if not config.is_configured then
+       print "Not configured, make sure you modify the script and set is_configured to true"
+       exit 1
+end
 var sorter = new XySorter
 
 # calculate cut off time using `elapsed_days` compared to now