Subversion Repositories Kolibri OS

Rev

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

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