Subversion Repositories Kolibri OS

Rev

Rev 5646 | 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 = 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 = 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 = 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 = 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 = 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 = 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 buffer;
  153.         signed count;
  154. };
  155. :int __DIR::make(dword new_folder_path)
  156. {
  157.         __file_F70.func = 9;
  158.         __file_F70.param1 =
  159.         __file_F70.param2 =
  160.         __file_F70.param3 =
  161.         __file_F70.param4 =
  162.         __file_F70.rezerv = 0;
  163.         __file_F70.name = new_folder_path;
  164.         $mov eax,70
  165.         $mov ebx,#__file_F70.func
  166.         $int 0x40
  167. }
  168.  
  169. :struct __PATH
  170. {
  171.         dword file(...);
  172.         dword path(...);
  173. };
  174. :char __PATH_NEW[4096];
  175. :dword __PATH::path(dword PATH)
  176. {
  177.         dword _NPT;
  178.         _NPT = #__PATH_NEW;
  179.         if(DSBYTE[PATH]=='/')
  180.         {
  181.                 if(strcmp(PATH,"sys/",4))
  182.                         if(strcmp(PATH,"hd/",3))
  183.                                 if(strcmp(PATH,"rd/",3))
  184.                                         if(strcmp(PATH,"tmp/",4))
  185.                                                 if(strcmp(PATH,"fd/",3))
  186.                                                         if(strcmp(PATH,"cd/",3)) sprintf(_NPT,"/%s%s","sys",PATH);
  187.         }
  188.         while(DSBYTE[_NPT])
  189.         {
  190.                 if(DSBYTE[_NPT]=='.')
  191.                 {
  192.                         if(DSBYTE[_NPT+1]=='.')
  193.                         {
  194.                                 if(DSBYTE[_NPT+1]=='/')
  195.                                 {
  196.                                        
  197.                                 }
  198.                         }
  199.                         else if(DSBYTE[_NPT+1]=='/')
  200.                         {
  201.                                 _NPT++;
  202.                                 sprintf(_NPT,"/%s%s","sys",_NPT);
  203.                         }
  204.                 }
  205.                 _NPT++;
  206.         }
  207.         return _NPT;
  208. }
  209.  
  210. :dword __PATH::file(dword name)
  211. {
  212.         dword ret;
  213.         ret = name;
  214.         while(DSBYTE[name])
  215.         {
  216.                 if(DSBYTE[name]=='/')ret = name+1;
  217.                 name++;
  218.         }
  219.         return ret;
  220. }
  221.  
  222. :struct IO
  223. {
  224.         dword buffer_data;
  225.         dword size_dir;
  226.         dword count_dirs,count_files;
  227.         signed FILES_SIZE;
  228.         dword file_name;
  229.         double size(...);
  230.         dword get_size_dir(dword name);
  231.         signed count(dword path);
  232.         dword dir_buffer(dword path;byte options);
  233.         dword dir_position(dword pos);
  234.         signed int run(dword path,param);
  235.         byte del(...);
  236.         dword read(...);
  237.         int write(...);
  238.         byte copy(...);
  239.         byte move(...);
  240.         dword set(...);
  241.         dword convert_size();
  242.         __DIR dir;
  243.         __PATH path;
  244.         __FILE file;
  245.         ____BDVK BDVK;
  246. }io;
  247.  
  248. :byte __ConvertSize_size_prefix[8];
  249. :dword IO::convert_size()
  250. {
  251.         byte size_nm[3];
  252.         dword bytes;
  253.         bytes = FILES_SIZE;
  254.         if (bytes>=1073741824) strncpy(#size_nm, __T__GB,2);
  255.         else if (bytes>=1048576) strncpy(#size_nm, __T__MB,2);
  256.         else if (bytes>=1024) strncpy(#size_nm, __T__KB,2);
  257.         else strncpy(#size_nm, __T___B,1);
  258.         while (bytes>1023) bytes/=1024;
  259.         sprintf(#__ConvertSize_size_prefix,"%d %s",bytes,#size_nm);
  260.         return #__ConvertSize_size_prefix;
  261. }
  262.        
  263. :int IO::write(dword PATH,data)
  264. {
  265.         file.write(0,strlen(data),data,PATH);
  266. }
  267. :dword IO::read(dword PATH)
  268. {
  269.         ___GetFileInfo(PATH, #BDVK);
  270.         if(BDVK.isfolder)return 0;
  271.         FILES_SIZE = BDVK.sizelo;
  272.         buffer_data = malloc(FILES_SIZE+1);
  273.         file.read(0,FILES_SIZE,buffer_data,PATH);
  274.         return buffer_data;
  275. }
  276.  
  277. :signed int IO::run(dword rpath,rparam)
  278. {
  279.         __file_F70.func = 7;
  280.     __file_F70.param1 =
  281.     __file_F70.param3 =
  282.     __file_F70.param4 =
  283.     __file_F70.rezerv = 0;
  284.     __file_F70.param2 = rparam;
  285.     __file_F70.name = rpath;
  286.     $mov eax,70
  287.     $mov ebx,#__file_F70.func
  288.     $int 0x40
  289. }
  290. :signed IO::count(dword PATH)
  291. {
  292.         byte buf[32];
  293.         if(!___ReadDir(0, #buf, PATH))
  294.         {
  295.                 dir.count = ESDWORD[#buf+8];
  296.                 return dir.count;
  297.         }
  298.         return -1;
  299. }
  300. :dword IO::dir_position(dword pos)
  301. {
  302.         return pos*304+dir.buffer+72;
  303. }
  304. :dword IO::dir_buffer(dword PATH;byte options)
  305. {
  306.         count(PATH);
  307.         if(dir.count!=-1)
  308.         {
  309.                 //if(dir.buffer) dir.buffer = realloc(dir.buffer,dir.count+1*304+32);
  310.                 //else          
  311.                 dir.buffer = malloc(dir.count+1*304+32);
  312.                 ___ReadDir(dir.count, dir.buffer, PATH);
  313.                 if (options == DIR_ONLYREAL)
  314.                 {
  315.                         if (!strcmp(".",dir.buffer+72)){dir.count--; memmov(dir.buffer,dir.buffer+304,dir.count*304);}
  316.                         if (!strcmp("..",dir.buffer+72)){dir.count--; memmov(dir.buffer,dir.buffer+304,dir.count*304);}
  317.                         return dir.buffer;
  318.                 }
  319.                 if (options == DIR_NOROOT)
  320.                 {
  321.                         if (!strcmp(".",dir.buffer+72)) memmov(dir.buffer,dir.buffer+304,dir.count*304-304);
  322.                         return dir.buffer;
  323.                 }
  324.                 return dir.buffer;
  325.         }
  326.         return NULL;
  327. }
  328.  
  329. :double IO::size(dword PATH)
  330. {
  331.         dword i,tmp_buf,count_dir,count_file;
  332.         dword filename;
  333.         double size_tmp;
  334.         double tmp;
  335.         if(!PATH)return 0;
  336.         if(___GetFileInfo(PATH, #BDVK))return -1;
  337.         if(BDVK.isfolder)
  338.         {
  339.                 tmp_buf = dir_buffer(PATH,DIR_ONLYREAL);
  340.                 if(dir.count<1)return 0;
  341.                 count_dir = dir.count;
  342.                 i = 0;
  343.                 size_tmp = 0;
  344.                 count_file = malloc(4096);
  345.                 while(i<count_dir)
  346.                 {
  347.                         filename = i*304+tmp_buf+72;
  348.                         sprintf(count_file,"%s/%s",PATH,filename);
  349.                         tmp = size(count_file);
  350.                         if(tmp==-1)return -1;
  351.                         size_tmp += tmp;
  352.                         i++;
  353.                         if (TestBit(ESDWORD[filename-40], 4))count_dirs++;
  354.                         else count_files++;
  355.                 }
  356.                
  357.                 free(tmp_buf);
  358.                 free(count_file);
  359.                 FILES_SIZE = size_tmp;
  360.                 return FILES_SIZE;
  361.         }
  362.         FILES_SIZE = BDVK.sizelo;
  363.         count_files++;
  364.         return FILES_SIZE;
  365. }
  366. :byte IO::del(dword PATH)
  367. {
  368.         dword i,tmp_buf,count_dir,count_file;
  369.         if(!PATH)return 0;
  370.         if(___GetFileInfo(PATH, #BDVK))return false;
  371.         if(BDVK.isfolder)
  372.         {
  373.                 tmp_buf = dir_buffer(PATH,DIR_ONLYREAL);
  374.                 count_dir = dir.count;
  375.                 i = 0;
  376.                 count_file = malloc(4096);
  377.                 while(i<count_dir)
  378.                 {
  379.                         sprintf(count_file,"%s/%s",PATH,i*304+tmp_buf+72);
  380.                         if(!del(count_file))return false;
  381.                         i++;
  382.                 }
  383.                 free(tmp_buf);
  384.                 free(count_file);
  385.         }
  386.         file.del(PATH);
  387.         return true;
  388. }
  389. :dword IO::set(dword PATH,atr)
  390. {
  391.         dword i,tmp_buf,count_dir,count_file;
  392.         byte cmd_read,cmd_hide,cmd_system;
  393.         if(!PATH)return 0;
  394.         if(___GetFileInfo(PATH, #BDVK))return false;
  395.         cmd_read   = atr&11b;
  396.         atr>>=2;
  397.         cmd_hide   = atr&11b;
  398.         atr>>=2;
  399.         cmd_system = atr&11b;
  400.         if(BDVK.isfolder)
  401.         {
  402.                 tmp_buf = dir_buffer(PATH,DIR_ONLYREAL);
  403.                 count_dir = dir.count;
  404.                 i = 0;
  405.                 count_file = malloc(4096);
  406.                 while(i<count_dir)
  407.                 {
  408.                         sprintf(count_file,"%s/%s",PATH,i*304+tmp_buf+72);
  409.                         file.set(PATH,atr);
  410.                         i++;
  411.                 }
  412.                 free(tmp_buf);
  413.                 free(count_file);
  414.                 return 0;
  415.         }
  416.         if(cmd_read)
  417.         {
  418.                 if(cmd_read&01b)BDVK.readonly = true;
  419.                 else BDVK.readonly = false;
  420.         }
  421.         if(cmd_hide)
  422.         {
  423.                 if(cmd_hide&01b)BDVK.hidden = true;
  424.                 else BDVK.hidden = false;
  425.         }
  426.         if(cmd_system)
  427.         {
  428.                 if(cmd_system&01b)BDVK.system = true;
  429.                 else BDVK.system = false;
  430.         }
  431.         file.set(PATH);
  432. }
  433. :byte IO::copy(dword PATH,PATH1)
  434. {
  435.         dword i,tmp_buf,count_dir,count_file;
  436.         dword _path_;
  437.         byte ret;
  438.         if(!PATH)return 0;
  439.         if(___GetFileInfo(PATH, #BDVK))return false;
  440.         _path_ = malloc(4096);
  441.         if(BDVK.isfolder)
  442.         {
  443.                 sprintf(_path_,"%s/%s",PATH1,path.file(PATH));
  444.                 dir.make(_path_);
  445.                 tmp_buf = dir_buffer(PATH,DIR_ONLYREAL);
  446.                 count_dir = dir.count;
  447.                 i = 0;
  448.                 count_file = malloc(4096);
  449.                 while(i<count_dir)
  450.                 {
  451.                         sprintf(count_file,"%s/%s",PATH,i*304+tmp_buf+72);
  452.                         if(!copy(count_file,_path_))return false;
  453.                         i++;
  454.                 }
  455.                 free(tmp_buf);
  456.                 free(count_file);
  457.                 free(_path_);
  458.                 return true;
  459.         }
  460.         read(PATH);
  461.         sprintf(_path_,"%s/%s",PATH1,path.file(PATH));
  462.         ret = file.write(FILES_SIZE,buffer_data,_path_);
  463.         free(_path_);
  464.         if(!ret)return true;
  465.         return false;
  466. }
  467. :byte IO::move(dword PATH,PATH1)
  468. {
  469.         if(copy(PATH,PATH1))if(del(PATH))return true;
  470.         return false;
  471. }
  472.  
  473. #endif