Subversion Repositories Kolibri OS

Rev

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