Subversion Repositories Kolibri OS

Rev

Rev 5640 | Rev 5648 | 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 position(dword i);
  153.         dword buffer;
  154.         signed count;
  155. };
  156. :dword __DIR::position(dword i)
  157. {
  158.         return i*304+buffer+72;
  159. }
  160. :int __DIR::make(dword new_folder_path)
  161. {
  162.         __file_F70.func = 9;
  163.         __file_F70.param1 =
  164.         __file_F70.param2 =
  165.         __file_F70.param3 =
  166.         __file_F70.param4 =
  167.         __file_F70.rezerv = 0;
  168.         __file_F70.name = io.path.path(new_folder_path);
  169.         $mov eax,70
  170.         $mov ebx,#__file_F70.func
  171.         $int 0x40
  172. }
  173.  
  174. :struct __PATH
  175. {
  176.         dword file(...);
  177.         dword path(...);
  178. };
  179.  
  180. :char __PATH_NEW[4096];
  181. :dword __PATH::path(dword PATH)
  182. {
  183.         dword pos = PATH;
  184.         if(DSBYTE[pos]=='/')
  185.         {
  186.                 pos++;
  187.                 if(!strncmp(pos,"sys/",4)) return PATH;
  188.                 if(!strncmp(pos,"hd/",3)) return PATH;
  189.                 if(!strncmp(pos,"fd/",3)) return PATH;
  190.                 if(!strncmp(pos,"rd/",3)) return PATH;
  191.                 if(!strncmp(pos,"tmp/",4)) return PATH;
  192.                 if(!strncmp(pos,"cd/",3)) return PATH;
  193.                 if(!strncmp(pos,"bd/",3)) return PATH;
  194.                 if(!strncmp(pos,"usbhd/",6)) return PATH;
  195.                 sprintf(#__PATH_NEW,"/sys%s",PATH);
  196.                 return #__PATH_NEW;
  197.         }
  198.         if(!strncmp(PATH,"./",2)) return PATH;
  199.         sprintf(#__PATH_NEW,"%s/%s",__DIR__,PATH);
  200.         return #__PATH_NEW;
  201. }
  202.  
  203. :dword __PATH::file(dword name)
  204. {
  205.         dword ret;
  206.         ret = name;
  207.         while(DSBYTE[name])
  208.         {
  209.                 if(DSBYTE[name]=='/')ret = name+1;
  210.                 name++;
  211.         }
  212.         return ret;
  213. }
  214.  
  215. :struct IO
  216. {
  217.         dword buffer_data;
  218.         dword size_dir;
  219.         dword count_dirs,count_files;
  220.         signed FILES_SIZE;
  221.         dword file_name;
  222.         double size(...);
  223.         dword get_size_dir(dword name);
  224.         signed count(dword path);
  225.         dword dir_buffer(dword path;byte options);
  226.         signed int run(dword path,param);
  227.         byte del(...);
  228.         dword read(...);
  229.         int write(...);
  230.         byte copy(...);
  231.         byte move(...);
  232.         dword set(...);
  233.         dword convert_size();
  234.         __DIR dir;
  235.         __PATH path;
  236.         __FILE file;
  237.         ____BDVK BDVK;
  238. }io;
  239.  
  240. :byte __ConvertSize_size_prefix[8];
  241. :dword IO::convert_size()
  242. {
  243.         byte size_nm[3];
  244.         dword bytes;
  245.         bytes = FILES_SIZE;
  246.         if (bytes>=1073741824) strlcpy(#size_nm, __T__GB,2);
  247.         else if (bytes>=1048576) strlcpy(#size_nm, __T__MB,2);
  248.         else if (bytes>=1024) strlcpy(#size_nm, __T__KB,2);
  249.         else strlcpy(#size_nm, __T___B,1);
  250.         while (bytes>1023) bytes/=1024;
  251.         sprintf(#__ConvertSize_size_prefix,"%d %s",bytes,#size_nm);
  252.         return #__ConvertSize_size_prefix;
  253. }
  254.        
  255. :int IO::write(dword PATH,data)
  256. {
  257.         file.write(0,strlen(data),data,PATH);
  258. }
  259. :dword IO::read(dword PATH)
  260. {
  261.         ___GetFileInfo(PATH, #BDVK);
  262.         if(BDVK.isfolder)return 0;
  263.         FILES_SIZE = BDVK.sizelo;
  264.         buffer_data = malloc(FILES_SIZE+1);
  265.         file.read(0,FILES_SIZE,buffer_data,PATH);
  266.         return buffer_data;
  267. }
  268.  
  269. :signed int IO::run(dword rpath,rparam)
  270. {
  271.         __file_F70.func = 7;
  272.     __file_F70.param1 =
  273.     __file_F70.param3 =
  274.     __file_F70.param4 =
  275.     __file_F70.rezerv = 0;
  276.     __file_F70.param2 = rparam;
  277.     __file_F70.name = path.path(rpath);
  278.     $mov eax,70
  279.     $mov ebx,#__file_F70.func
  280.     $int 0x40
  281. }
  282. :signed IO::count(dword PATH)
  283. {
  284.         byte buf[32];
  285.         if(!___ReadDir(0, #buf, PATH))
  286.         {
  287.                 dir.count = ESDWORD[#buf+8];
  288.                 return dir.count;
  289.         }
  290.         return -1;
  291. }
  292.  
  293. :dword IO::dir_buffer(dword PATH;byte options)
  294. {
  295.         count(PATH);
  296.         if(dir.count!=-1)
  297.         {
  298.                 //if(dir.buffer) dir.buffer = realloc(dir.buffer,dir.count+1*304+32);
  299.                 //else          
  300.                 dir.buffer = malloc(dir.count+1*304+32);
  301.                 ___ReadDir(dir.count, dir.buffer, PATH);
  302.                 if (options == DIR_ONLYREAL)
  303.                 {
  304.                         if (!strcmp(".",dir.buffer+72)){dir.count--; memmov(dir.buffer,dir.buffer+304,dir.count*304);}
  305.                         if (!strcmp("..",dir.buffer+72)){dir.count--; memmov(dir.buffer,dir.buffer+304,dir.count*304);}
  306.                         return dir.buffer;
  307.                 }
  308.                 if (options == DIR_NOROOT)
  309.                 {
  310.                         if (!strcmp(".",dir.buffer+72)) memmov(dir.buffer,dir.buffer+304,dir.count*304-304);
  311.                         return dir.buffer;
  312.                 }
  313.                 return dir.buffer;
  314.         }
  315.         return NULL;
  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_buffer(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_buffer(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_buffer(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_buffer(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