Subversion Repositories Kolibri OS

Rev

Rev 5646 | Rev 5660 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. //IO library
  2. #ifndef INCLUDE_IO_H
  3. #define INCLUDE_IO_H
  4.  
  5. #ifndef INCLUDE_DATE_H
  6. #include "../lib/date.h"
  7. #endif
  8.  
  9. #ifdef LANG_RUS
  10.         #define __T__GB "ƒ¡"
  11.         #define __T__MB "Œ¡"
  12.         #define __T__KB "Š¡"
  13.         #define __T___B ""
  14. #else
  15.         #define __T__GB "Gb"
  16.         #define __T__MB "Mb"
  17.         #define __T__KB "Kb"
  18.         #define __T___B "B"
  19. #endif
  20.  
  21. #define ATR_READONLY   000001b
  22. #define ATR_HIDDEN     000100b
  23. #define ATR_SYSTEM     010000b
  24.  
  25. #define ATR_NOREADONLY 000010b
  26. #define ATR_NOHIDDEN   001000b
  27. #define ATR_NOSYSTEM   100000b
  28.  
  29. :enum
  30. {
  31.         DIR_ALL,
  32.         DIR_NOROOT,
  33.         DIR_ONLYREAL
  34. };
  35.  
  36. :struct ___f70{
  37.         dword   func;
  38.         dword   param1;
  39.         dword   param2;
  40.         dword   param3;
  41.         dword   param4;
  42.         char    rezerv;
  43.         dword   name;
  44. }__file_F70;
  45.  
  46. :int ___ReadDir(dword file_count, read_buffer, dir_path)
  47. {
  48.         __file_F70.func = 1;
  49.         __file_F70.param1 =
  50.         __file_F70.param2 =
  51.         __file_F70.rezerv = 0;
  52.         __file_F70.param3 = file_count;
  53.         __file_F70.param4 = read_buffer;
  54.         __file_F70.name = io.path.path(dir_path);
  55.         $mov eax,70
  56.         $mov ebx,#__file_F70.func
  57.         $int 0x40
  58. }
  59.  
  60. :dword ___GetFileInfo(dword file_path, bdvk_struct)
  61. {    
  62.     __file_F70.func = 5;
  63.     __file_F70.param1 =
  64.     __file_F70.param2 =
  65.     __file_F70.param3 = 0;
  66.     __file_F70.param4 = bdvk_struct;
  67.     __file_F70.rezerv = 0;
  68.     __file_F70.name = io.path.path(file_path);
  69.     $mov eax,70
  70.     $mov ebx,#__file_F70.func
  71.     $int 0x40
  72. }
  73.  
  74. :struct ____BDVK {
  75.         dword   readonly:1, hidden:1, system:1, volume_label:1, isfolder:1, notarchived:1, :0;
  76.         byte    type_name;
  77.         byte    rez1, rez2, selected;
  78.         dword   timecreate;
  79.         date    datecreate;
  80.         dword   timelastaccess;
  81.         date    datelastaccess;
  82.         dword   timelastedit;
  83.         date    datelastedit;
  84.         dword   sizelo;
  85.         dword   sizehi;
  86.         char    name[518];
  87. };
  88.  
  89. :struct __FILE
  90. {
  91.         dword count;
  92.         int del(...);
  93.         int read(...);
  94.         int write(...);
  95.         dword set(...);
  96. };
  97. :dword __FILE::set(dword file_path)
  98. {    
  99.     __file_F70.func = 6;
  100.     __file_F70.param1 =
  101.     __file_F70.param2 =
  102.     __file_F70.param3 = 0;
  103.     __file_F70.param4 = #io.BDVK;
  104.     __file_F70.rezerv = 0;
  105.     __file_F70.name = io.path.path(file_path);
  106.     $mov eax,70
  107.     $mov ebx,#__file_F70.func
  108.     $int 0x40
  109. }
  110. :int __FILE::del(dword PATH)
  111. {
  112.         __file_F70.func = 8;
  113.         __file_F70.param1 =
  114.         __file_F70.param2 =
  115.         __file_F70.param3 =
  116.         __file_F70.param4 =
  117.         __file_F70.rezerv = 0;
  118.         __file_F70.name = io.path.path(PATH);
  119.         $mov eax,70
  120.         $mov ebx,#__file_F70.func
  121.         $int 0x40
  122. }
  123. :int __FILE::read(dword read_pos, read_file_size, read_buffer, read_file_path)
  124. {
  125.         __file_F70.func = 0;
  126.         __file_F70.param1 = read_pos;
  127.         __file_F70.param2 = 0;
  128.         __file_F70.param3 = read_file_size;
  129.         __file_F70.param4 = read_buffer;
  130.         __file_F70.rezerv = 0;
  131.         __file_F70.name = io.path.path(read_file_path);
  132.         $mov eax,70
  133.         $mov ebx,#__file_F70.func
  134.         $int 0x40
  135. }
  136. :int __FILE::write(dword write_file_size, write_buffer, write_file_path)
  137. {
  138.         __file_F70.func = 2;
  139.         __file_F70.param1 = 0;
  140.         __file_F70.param2 = 0;
  141.         __file_F70.param3 = write_file_size;
  142.         __file_F70.param4 = write_buffer;
  143.         __file_F70.rezerv = 0;
  144.         __file_F70.name = io.path.path(write_file_path);
  145.         $mov eax,70
  146.         $mov ebx,#__file_F70.func
  147.         $int 0x40
  148. }
  149. :struct __DIR
  150. {
  151.         int make(dword name);
  152.         dword load(...);
  153.         dword position(dword i);
  154.         dword buffer;
  155.         signed count;
  156. };
  157. :dword __DIR::position(dword i)
  158. {
  159.         return i*304+buffer+72;
  160. }
  161. :int __DIR::make(dword new_folder_path)
  162. {
  163.         __file_F70.func = 9;
  164.         __file_F70.param1 =
  165.         __file_F70.param2 =
  166.         __file_F70.param3 =
  167.         __file_F70.param4 =
  168.         __file_F70.rezerv = 0;
  169.         __file_F70.name = io.path.path(new_folder_path);
  170.         $mov eax,70
  171.         $mov ebx,#__file_F70.func
  172.         $int 0x40
  173. }
  174.  
  175. :dword __DIR::load(dword PATH;byte options)
  176. {
  177.         io.count(PATH);
  178.         if(count!=-1)
  179.         {      
  180.                 buffer = malloc(count+1*304+32);
  181.                 ___ReadDir(count, buffer, PATH);
  182.                 if (options == DIR_ONLYREAL)
  183.                 {
  184.                         if (!strcmp(".",buffer+72)){count--; memmov(buffer,buffer+304,count*304);}
  185.                         if (!strcmp("..",buffer+72)){count--; memmov(buffer,buffer+304,count*304);}
  186.                         return buffer;
  187.                 }
  188.                 if (options == DIR_NOROOT)
  189.                 {
  190.                         if (!strcmp(".",buffer+72)) memmov(buffer,buffer+304,count*304-304);
  191.                         return buffer;
  192.                 }
  193.                 return buffer;
  194.         }
  195.         return NULL;
  196. }
  197.  
  198. :struct __PATH
  199. {
  200.         dword file(...);
  201.         dword path(...);
  202. };
  203.  
  204. :char __PATH_NEW[4096];
  205. :dword __PATH::path(dword PATH)
  206. {
  207.         dword pos;
  208.         if(!PATH) return self.dir;
  209.         pos = PATH;
  210.         if(DSBYTE[pos]=='/')
  211.         {
  212.                 pos++;
  213.                 if(!strncmp(pos,"sys/",4)) return PATH;
  214.                 if(!strncmp(pos,"hd/",3)) return PATH;
  215.                 if(!strncmp(pos,"fd/",3)) return PATH;
  216.                 if(!strncmp(pos,"rd/",3)) return PATH;
  217.                 if(!strncmp(pos,"tmp/",4)) return PATH;
  218.                 if(!strncmp(pos,"cd/",3)) return PATH;
  219.                 if(!strncmp(pos,"bd/",3)) return PATH;
  220.                 if(!strncmp(pos,"usbhd/",6)) return PATH;
  221.                 sprintf(#__PATH_NEW,"/sys%s",PATH);
  222.                 return #__PATH_NEW;
  223.         }
  224.         if(!strncmp(PATH,"./",2)) return PATH;
  225.         sprintf(#__PATH_NEW,"%s/%s",self.dir,PATH);
  226.         return #__PATH_NEW;
  227. }
  228.  
  229. :dword __PATH::file(dword name)
  230. {
  231.         dword ret;
  232.         ret = name;
  233.         while(DSBYTE[name])
  234.         {
  235.                 if(DSBYTE[name]=='/')ret = name+1;
  236.                 name++;
  237.         }
  238.         return ret;
  239. }
  240.  
  241. :struct IO
  242. {
  243.         dword buffer_data;
  244.         dword size_dir;
  245.         dword count_dirs,count_files;
  246.         signed FILES_SIZE;
  247.         dword file_name;
  248.         double size(...);
  249.         dword get_size_dir(dword name);
  250.         signed count(dword path);
  251.         signed int run(dword path,param);
  252.         byte del(...);
  253.         dword read(...);
  254.         int write(...);
  255.         byte copy(...);
  256.         byte move(...);
  257.         dword set(...);
  258.         dword convert_size();
  259.         __DIR dir;
  260.         __PATH path;
  261.         __FILE file;
  262.         ____BDVK BDVK;
  263. }io;
  264.  
  265. :byte __ConvertSize_size_prefix[8];
  266. :dword IO::convert_size()
  267. {
  268.         byte size_nm[3];
  269.         dword bytes;
  270.         bytes = FILES_SIZE;
  271.         if (bytes>=1073741824) strlcpy(#size_nm, __T__GB,2);
  272.         else if (bytes>=1048576) strlcpy(#size_nm, __T__MB,2);
  273.         else if (bytes>=1024) strlcpy(#size_nm, __T__KB,2);
  274.         else strlcpy(#size_nm, __T___B,1);
  275.         while (bytes>1023) bytes/=1024;
  276.         sprintf(#__ConvertSize_size_prefix,"%d %s",bytes,#size_nm);
  277.         return #__ConvertSize_size_prefix;
  278. }
  279.        
  280. :int IO::write(dword PATH,data)
  281. {
  282.         file.write(0,strlen(data),data,PATH);
  283. }
  284. :dword IO::read(dword PATH)
  285. {
  286.         ___GetFileInfo(PATH, #BDVK);
  287.         if(BDVK.isfolder)return 0;
  288.         FILES_SIZE = BDVK.sizelo;
  289.         buffer_data = malloc(FILES_SIZE+1);
  290.         file.read(0,FILES_SIZE,buffer_data,PATH);
  291.         return buffer_data;
  292. }
  293.  
  294. :signed int IO::run(dword rpath,rparam)
  295. {
  296.         __file_F70.func = 7;
  297.     __file_F70.param1 =
  298.     __file_F70.param3 =
  299.     __file_F70.param4 =
  300.     __file_F70.rezerv = 0;
  301.     __file_F70.param2 = rparam;
  302.     __file_F70.name = path.path(rpath);
  303.     $mov eax,70
  304.     $mov ebx,#__file_F70.func
  305.     $int 0x40
  306. }
  307. :signed IO::count(dword PATH)
  308. {
  309.         byte buf[32];
  310.         if(!___ReadDir(0, #buf, PATH))
  311.         {
  312.                 dir.count = ESDWORD[#buf+8];
  313.                 return dir.count;
  314.         }
  315.         return -1;
  316. }
  317.  
  318. :double IO::size(dword PATH)
  319. {
  320.         dword i,tmp_buf,count_dir,count_file;
  321.         dword filename;
  322.         double size_tmp;
  323.         double tmp;
  324.         if(!PATH)return 0;
  325.         if(___GetFileInfo(PATH, #BDVK))return -1;
  326.         if(BDVK.isfolder)
  327.         {
  328.                 tmp_buf = dir.load(PATH,DIR_ONLYREAL);
  329.                 if(dir.count<1)return 0;
  330.                 count_dir = dir.count;
  331.                 i = 0;
  332.                 size_tmp = 0;
  333.                 count_file = malloc(4096);
  334.                 while(i<count_dir)
  335.                 {
  336.                         filename = i*304+tmp_buf+72;
  337.                         sprintf(count_file,"%s/%s",PATH,filename);
  338.                         tmp = size(count_file);
  339.                         if(tmp==-1)return -1;
  340.                         size_tmp += tmp;
  341.                         i++;
  342.                         if (TestBit(ESDWORD[filename-40], 4))count_dirs++;
  343.                         else count_files++;
  344.                 }
  345.                
  346.                 free(tmp_buf);
  347.                 free(count_file);
  348.                 FILES_SIZE = size_tmp;
  349.                 return FILES_SIZE;
  350.         }
  351.         FILES_SIZE = BDVK.sizelo;
  352.         count_files++;
  353.         return FILES_SIZE;
  354. }
  355. :byte IO::del(dword PATH)
  356. {
  357.         dword i,tmp_buf,count_dir,count_file;
  358.         if(!PATH)return 0;
  359.         if(___GetFileInfo(PATH, #BDVK))return false;
  360.         if(BDVK.isfolder)
  361.         {
  362.                 tmp_buf = dir.load(PATH,DIR_ONLYREAL);
  363.                 count_dir = dir.count;
  364.                 i = 0;
  365.                 count_file = malloc(4096);
  366.                 while(i<count_dir)
  367.                 {
  368.                         sprintf(count_file,"%s/%s",PATH,i*304+tmp_buf+72);
  369.                         if(!del(count_file))return false;
  370.                         i++;
  371.                 }
  372.                 free(tmp_buf);
  373.                 free(count_file);
  374.         }
  375.         file.del(PATH);
  376.         return true;
  377. }
  378. :dword IO::set(dword PATH,atr)
  379. {
  380.         dword i,tmp_buf,count_dir,count_file;
  381.         byte cmd_read,cmd_hide,cmd_system;
  382.         if(!PATH)return 0;
  383.         if(___GetFileInfo(PATH, #BDVK))return false;
  384.         cmd_read   = atr&11b;
  385.         atr>>=2;
  386.         cmd_hide   = atr&11b;
  387.         atr>>=2;
  388.         cmd_system = atr&11b;
  389.         if(BDVK.isfolder)
  390.         {
  391.                 tmp_buf = dir.load(PATH,DIR_ONLYREAL);
  392.                 count_dir = dir.count;
  393.                 i = 0;
  394.                 count_file = malloc(4096);
  395.                 while(i<count_dir)
  396.                 {
  397.                         sprintf(count_file,"%s/%s",PATH,i*304+tmp_buf+72);
  398.                         file.set(PATH,atr);
  399.                         i++;
  400.                 }
  401.                 free(tmp_buf);
  402.                 free(count_file);
  403.                 return 0;
  404.         }
  405.         if(cmd_read)
  406.         {
  407.                 if(cmd_read&01b)BDVK.readonly = true;
  408.                 else BDVK.readonly = false;
  409.         }
  410.         if(cmd_hide)
  411.         {
  412.                 if(cmd_hide&01b)BDVK.hidden = true;
  413.                 else BDVK.hidden = false;
  414.         }
  415.         if(cmd_system)
  416.         {
  417.                 if(cmd_system&01b)BDVK.system = true;
  418.                 else BDVK.system = false;
  419.         }
  420.         file.set(PATH);
  421. }
  422. :byte IO::copy(dword PATH,PATH1)
  423. {
  424.         dword i,tmp_buf,count_dir,count_file;
  425.         dword _path_;
  426.         byte ret;
  427.         if(!PATH)return 0;
  428.         if(___GetFileInfo(PATH, #BDVK))return false;
  429.         _path_ = malloc(4096);
  430.         if(BDVK.isfolder)
  431.         {
  432.                 sprintf(_path_,"%s/%s",PATH1,path.file(PATH));
  433.                 dir.make(_path_);
  434.                 tmp_buf = dir.load(PATH,DIR_ONLYREAL);
  435.                 count_dir = dir.count;
  436.                 i = 0;
  437.                 count_file = malloc(4096);
  438.                 while(i<count_dir)
  439.                 {
  440.                         sprintf(count_file,"%s/%s",PATH,i*304+tmp_buf+72);
  441.                         if(!copy(count_file,_path_))return false;
  442.                         i++;
  443.                 }
  444.                 free(tmp_buf);
  445.                 free(count_file);
  446.                 free(_path_);
  447.                 return true;
  448.         }
  449.         read(PATH);
  450.         sprintf(_path_,"%s/%s",PATH1,path.file(PATH));
  451.         ret = file.write(FILES_SIZE,buffer_data,_path_);
  452.         free(_path_);
  453.         if(!ret)return true;
  454.         return false;
  455. }
  456. :byte IO::move(dword PATH,PATH1)
  457. {
  458.         if(copy(PATH,PATH1))if(del(PATH))return true;
  459.         return false;
  460. }
  461.  
  462. #endif