/*
CLASS {NATIVE_File_stream|_CSharp}, NATIVE :

NATIVE: "///COPY NATIVE_File_stream.cs" .

*/

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


using AL4;

namespace AL4
{
    public class NATIVE_File_stream
    {
        /*
        // {File_stream} NATIVE functions code:
        */
        
		public const int BufferSize = 16384;
        public static List<BufferedStream> All_streams = new List<BufferedStream>();
        
        /*
        CONST STR :
            NATIVE_File_stream_import = @
            "using System.IO;"
            "using System.Threading;"
            
			NATIVE_All_streams = ""
            
            NATIVE_File_stream_open = @
            "if (NATIVE_File_stream.All_streams.Count() == 0)"
            "   NATIVE_File_stream.All_streams.Add(null);"
            "int h = NATIVE_File_stream.All_streams.LastIndexOf(null);"
            "if (h <= 0)"
            "{"
            "   h = NATIVE_File_stream.All_streams.Count();"
            "   NATIVE_File_stream.All_streams.Add(null);"
            "}"
            "FileAccess fa = FileAccess.Read;"
            "FileMode fm = FileMode.Open;"
            "FileShare fsh = FileShare.Read;"     
            "switch (X_Mode)"
            "{"
            "   case 1: fa = FileAccess.Write;"
            "           fm = FileMode.Create;"
            "           break;"
            "   case 2: fa = FileAccess.ReadWrite;"
            "           fm = FileMode.OpenOrCreate;"
            "			fsh = FileShare.None;"     
            "           break;"
            "	case 3: fsh = FileShare.ReadWrite | FileShare.Delete;"    
            "			break;"
            "	case 4: fa = FileAccess.ReadWrite;"     
            "			fsh = FileShare.ReadWrite | FileShare.Delete;" 
            "           fm = FileMode.OpenOrCreate;"     
            "			break;"
            "}"
            "bool ok = false;"
            "FileStream fs = null;"     
            "for (int attempts= 5; !ok && attempts >= 1; attempts-=1)"
            "{"
            "   try {"
			"        fs = new FileStream(X_Path, fm, fa, fsh);"
			"        if (fs.Handle == (IntPtr)(-1))"
			"   	    fs = new FileStream("
			"   		    System_functions.Short_path(X_Path),  fm, fa, fsh);"
        	"		 ok = fs.Handle != (IntPtr)(-1);"
        	"		 if (!ok) Thread.Sleep(1);"
        	"		 else "     
			"			NATIVE_File_stream.All_streams[h] = new BufferedStream("
			"   		fs, NATIVE_File_stream.BufferSize);"
            "   }"
            "   catch (Exception E) {"
            "		if (attempts <= 1)"     
            "      		AL4_system.Any_exception(E);"
            "   }"
            "}"     
            "if (ok) RESULT = h;"
            "else RESULT = 0;"     
            
            NATIVE_File_stream_destroy = @
            "    if (X_Handle < 1 || "
            "       X_Handle >= NATIVE_File_stream.All_streams.Count() ||"
            "       NATIVE_File_stream.All_streams[(int)X_Handle] == null)"
            "           return;"
            "    NATIVE_File_stream.All_streams[(int)X_Handle] = null;"
            
            NATIVE_File_stream_size = @
            "try {"
            "    if (X_Handle < 1 || "
            "       X_Handle >= NATIVE_File_stream.All_streams.Count() ||"
            "       NATIVE_File_stream.All_streams[(int)X_Handle] == null ||"
            "       X_Is_closed) { }"
            "    else {"
            "	 BufferedStream fs = NATIVE_File_stream.All_streams["
            "			(int)X_Handle];"
            "	 if (!fs.CanRead) return;"
            "	 X_lo_hi.set_int(0, (int)fs.Length);"
            "	 X_lo_hi.set_int(1, (int)(fs.Length >> 32));"
            "	 }"
            "}"
            "catch(Exception ex) {}"
            
            NATIVE_File_stream_set_size = @
            "try {"
            "    if (X_Handle < 1 || "
            "       X_Handle >= NATIVE_File_stream.All_streams.Count() ||"
            "       NATIVE_File_stream.All_streams[(int)X_Handle] == null ||"
            "       X_Is_closed) { }"
            "    else {"
            "	 BufferedStream fs = NATIVE_File_stream.All_streams["
            "			(int)X_Handle];"
            "	 if (!fs.CanWrite) return;"
            "	 long np = X_lo_hi.get_int(0) | ((X_lo_hi.get_int(1)<<16)<<16);"     
            "	 fs.SetLength(np);"     
            "	 }"
            "}"
            "catch(Exception ex) {}"
            
            NATIVE_File_stream_position = @
            "    if (X_Handle < 1 || "
            "       X_Handle >= NATIVE_File_stream.All_streams.Count() ||"
            "       NATIVE_File_stream.All_streams[(int)X_Handle] == null ||"
            "       X_Is_closed) { }"
            "    else {"
            "		long pos "
            "		= NATIVE_File_stream.All_streams[(int)X_Handle].Position;"
            "		X_lo_hi.set_int(0, (int)pos);"     
            "		X_lo_hi.set_int(1, (int)(pos >> 32));"
            "		}"     
            
            NATIVE_File_stream_set_pos = @
            "try {"
            "    if (X_Handle < 1 || "
            "       X_Handle >= NATIVE_File_stream.All_streams.Count() ||"
            "       NATIVE_File_stream.All_streams[(int)X_Handle] == null) "
            "			return;"
            "    BufferedStream fs = NATIVE_File_stream.All_streams["
            "			(int)X_Handle];"
            "	 if (!fs.CanSeek) return;"
            "    if (fs.Position > fs.Length)"
            "        fs.SetLength(fs.Position);"
            "    fs.Flush();"
            "    Int64 To = ((Int64)X_New_lo) | (((Int64)X_New_hi) << 32);"
            "    switch (X_Disp)" 
            "    {"
            "       case 1: To += fs.Position; break;"
            "       case 2: To = fs.Length + To; break;"
            "    }"
            "    if (To < 0) To = 0;"
            "    if (To >= fs.Length) To = fs.Length;"
            "    fs.Seek(To, SeekOrigin.Begin);" 
            "}"
            "catch(Exception ex) {}"
            
            NATIVE_File_stream_close = @
            "try {"
            "    if (X_Handle < 1 || "
            "       X_Handle >= NATIVE_File_stream.All_streams.Count() ||"
            "       NATIVE_File_stream.All_streams[(int)X_Handle] == null) "
            "			return;"
            "	 BufferedStream fs = NATIVE_File_stream.All_streams["
            "			(int)X_Handle];"
            "	 if (fs != null) fs.Close();"
            "}"
            "catch (Exception ex) {}"
            
            NATIVE_File_stream_read_bytes = @
            "    if (X_Handle < 1 || "
            "       X_Handle >= NATIVE_File_stream.All_streams.Count() ||"
            "       NATIVE_File_stream.All_streams[(int)X_Handle] == null)"
            "       return 0;"
            "    BufferedStream fs = NATIVE_File_stream.All_streams["
            "		(int)X_Handle];"
            "    if (!fs.CanRead) return 0;"
            "    try {"
            "        byte[] read_buf = new byte[X_Dest.Count];"
            "		 int to_read = X_Dest.Count;"
            "		 if ((long)to_read > fs.Length - fs.Position)"
            "			to_read = (int)(fs.Length - fs.Position);"
            "        RESULT = fs.Read(read_buf, 0, to_read);"
            "        for (int i = 0; i < RESULT; i++)"
            "            X_Dest[i] = read_buf[i];"
            "    }"
            "    catch (Exception E) {"
            "       AL4_system.Any_exception(E);"
            "    }"
            
            NATIVE_File_stream_write_bytes = @
            "    if (X_Handle < 1 || "
            "       X_Handle >= NATIVE_File_stream.All_streams.Count() ||"
            "       NATIVE_File_stream.All_streams[(int)X_Handle] == null)"
            "       return 0;"
            "    BufferedStream fs = NATIVE_File_stream.All_streams["
            "		(int)X_Handle];"
            "    try {"
            "        Int64 pos = fs.Position;"
            "    	 byte[] buffer = new byte[X_Source.Count];"
        	"	 	 X_Source.ToBytes(buffer, 0, X_Source.Count);"
            "        fs.Write(buffer, 0, X_Source.Count);"
            "        RESULT = (int)(fs.Position - pos);"
            "    }"
            "    catch (Exception E) {"
            "       AL4_system.Any_exception(E);"
            "    }"
            
            .
            
            
        */


    }
}
/*
END
*/

