/*

CLASS {File_path|_for_primary_compiler_only}, NATIVE, UNTESTED, STOPPING :

IMPORT: {String_functions}, {System_functions} ;

*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

/****************)
NATIVE: "using System.IO;" .
(****************/
using System.IO;
using AL4;
using AL4Compiler;

namespace AL4Compiler
{
    public class File_path
    {
        /*
        
        FUNCTION Exists|_file(
        STR Path|_of_file) ==> BOOL, STATIC, NATIVE :
	        CASE Path == "" ? ==> ;
	        NATIVE     
           @"    if (File.Exists(X_Path)) return true;"
           	"" // "	 if (File.Exists(Short_path(X_Path))) return true;"
            "    try {"
            "        string[] ff = Directory.GetFiles("
            "            System.IO.Path.GetDirectoryName(X_Path));"
            "        X_Path = X_Path.ToLower();"
            "        for (int i = ff.Length-1; i >= 0; i--)"
            "        {"
            "            string s = ff[i].ToLower();"
            "            if (s == X_Path) return true;"
            "        }"
            "    }"
            "    catch { }"
            "    return false;" .
        */
        public static bool Exists(string Path)
        {
            if (File.Exists(Path)) return true;
            try
            {
                string[] ff = Directory.GetFiles(System.IO.Path.GetDirectoryName(Path));
                Path = Path.ToLower();
                for (int i = ff.Length - 1; i >= 0; i--)
                {
                    string s = ff[i].ToLower();
                    if (s == Path) return true;
                }
            }
            catch { }
            return false;
        }

        /*
        
        FUN Delete_file(STR Path|_file), STATIC :
            CASE !Path.Exists ? ==> ;
            CASE TEST && "test" !IN Path.Lower ?
                 STOP "In test mode, only TEST files can be deleted" ;
            Path.delete .

        FUN delete|_file(STR Path|_file), STATIC, NATIVE :
           @"try {"
            "   File.Delete(X_Path);"
            "} catch {"
            "}" .
        */

        /*
        
        FUN Rename|_file(
        STR Path|_file, STR New|_name), STATIC :
            CASE !Path.Exists ? ==> ;
            CASE TEST && ("test" !IN Path.Lower || "test" !IN New.Lower)?
                 STOP "In TEST mode only TEST files can be renamed "
                      "and only to TEST places" ;
            rename(Path, New) .

        FUN rename|_file(
        STR Path|_file, STR New|_name), STATIC, NATIVE :
           @"try {"
            "    if (!File.Exists(X_Path)) return;"
            "    if (File.Exists(X_New)) File.Delete(X_New);"
            "    if (File.Exists(X_Path)) File.Move(X_Path, X_New);"
            "} catch {"
            "}" .
        */

        /*
        
        FUN Move|_file(STR Path|_file, STR New|_path), STATIC :
            CASE TEST && ("test" !IN Path.Lower || "test" !IN New.Lower)?
                 STOP "In TEST mode, only TEST files can be moved, "
                      "and only to test places" ;
            rename(Path, New) .

        FUN Copy_file(
        STR S|ource_file_path, STR D|estination), STATIC :
            CASE !S.Exists ? ==> ;
            CASE !D.Extract_path.Directory_exists ? ==> ;
            Stop_if_test(S)
            Stop_if_test(D)
            do_copy_file(S, D) .

        FUN do_copy_file(STR S|ource_file_path, STR D|estination),
                STATIC, NATIVE :
           @"    try {"
            "        File.Delete(X_D);"
            "        File.Copy(X_S, X_D);"
            "    } catch { }" .

        */

        /*
        
        FUNCTION Directory_exists(
        STR Path|_of_dir) ==> BOOL, STATIC, NATIVE :
            "    return Directory.Exists(X_Path);" .
        */

        /*
        
        FUNCTION Create_dir|ectory(
        STR Path|_to_new_dir), STATIC, NATIVE :
            "    Directory.CreateDirectory(X_Path);" .
        */
        public static void Create_dir(string path)
        {
            Directory.CreateDirectory(path);
        }

        /*
        
        FUN Catalog|ue_of_directory(
        STR Dir|ectory_path,
                STR Cat|alogue_scanned[]), STATIC, NATIVE :
           @"    if (!Directory.Exists(X_Dir)) return;"
            "    string[] dirs = Directory.GetDirectories(X_Dir);"
            "    string[] files = Directory.GetFiles(X_Dir, "#QU"*.*"#QU","
            "        SearchOption.TopDirectoryOnly);"
            "    foreach (string d in dirs)"
            "        X_Cat.Add(Path.GetFileName(d) + "#QU"\\"#QU");"
            "    foreach (string f in files)"
            "        X_Cat.Add(Path.GetFileName(f));" .
        */
        public static void Catalog(string dir, List<string> cat)
        {
            string[] dirs = Directory.GetDirectories(dir);
            string[] files = Directory.GetFiles(dir, "*.*", SearchOption.TopDirectoryOnly);
            foreach (string d in dirs)
                    cat.Add(Path.GetFileName(d) + "\\");
            foreach (string f in files)
                    cat.Add(Path.GetFileName(f));
        }

        /*
        
        FUNCTION Change_ext|ension(
        STR Path|_of_file,
                STR New_ex|tension) ==> STR, STATIC :
            FOR i IN [Path.Len-1 DOWNTO 0] :
                CASE Path[i] ?
                    ["."]: RESULT = Path[0 TO i-1] New_ex ==>
                    ["\", "/"]: BREAK i ;
            ;
            RESULT = Path New_ex .
            
        FUN Ext|ension_of_file(STR Path|_of_file) ==> STR :
        	STR no_ext|ension = Change_ext(Path, "")
        	RESULT = Path[no_ext.Len TO *] .
            
        */
        public static string Change_Ext(string Path, string New_ext)
        {
            bool break_i = false;
            for (int i = Path.Length - 1; i >= 0 && !break_i; i--)
            {
                switch (Path[i])
                {
                    case '.': return Path.Substring(0, i - 1) + New_ext;
                    case '\\':
                    case '/': break_i = true; break;
                }
            }
            return Path + New_ext;
        }

        /*
        
        FUNCTION Extract_path(
        STR Path|_of_file_full) ==> STR, STATIC, NATIVE :
            "    return Path.GetDirectoryName(X_Path);" .

        FUNCTION Extract_file_name(
        STR Path|_file) ==> STR, STATIC, NATIVE :
            "    return Path.GetFileName(X_Path);" .
            
		FUNCTION Get_file_size(
		STR Path|_file) ==> REAL, NATIVE :
		@	"var f = new FileInfo(X_Path);"
			"return (double)f.Length;" .
			
		FUNCTION Get_file_time_age_seconds(
		STR Path|_file) ==> REAL, NATIVE:
		@	"var f = new FileInfo(X_Path);"
			"return DateTime.Now.Subtract(f.LastWriteTime).TotalSeconds;" .
			
		FUNCTION Get_file_modified_time_formatted(
		STR Path|_file) ==> STR, NATIVE:
		@	"var f = new FileInfo(X_Path);"
			"return f.LastWriteTime.ToString("#QU"yyyyMMdd HHmmss"#QU");" .
			

        */

    }
}
/*
END
*/

