N1E7R4V

Untitled

Apr 11th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     -= Extended file class =-
  3.     Author: Bimbol 
  4.    
  5.     //-------------------------------------------
  6.    
  7.     USING EXAMPLE:
  8.    
  9.     //-------------------------------------------
  10.     // Writing string to file
  11.     //-------------------------------------------
  12.    
  13.     local myfile = io.file("test.txt", "w");
  14.     if (myfile.isOpen)
  15.     {
  16.         myfile.write("Test :)");
  17.         myfile.write("And this is test2\n");
  18.         myfile.write("Gothic 2 Online");
  19.         myfile.close();
  20.     }
  21.     else
  22.         print(myfile.errorMsg)
  23.        
  24.     //-------------------------------------------
  25.     // Reading string from file
  26.     //-------------------------------------------
  27.  
  28.     local myfile = io.file("test.txt", "r");
  29.     if (myfile.isOpen)
  30.     {
  31.         print(myfile.read(io_type.ALL));
  32.         myfile.seek(0);
  33.        
  34.         print("1 Line:");
  35.         print(myfile.read(io_type.LINE));
  36.        
  37.         print("2 Line:");
  38.         print(myfile.read(io_type.LINE));
  39.        
  40.         print("3 Line:");
  41.         print(myfile.read(io_type.LINE));
  42.        
  43.         myfile.close();
  44.     }
  45.     else
  46.         print(myfile.errorMsg);
  47.        
  48.     //-------------------------------------------
  49. */
  50.  
  51. io <- {}
  52.  
  53. enum io_type
  54. {
  55.     LINE,
  56.     ALL
  57. };
  58.  
  59. class io.file extends file
  60. {
  61.     constructor(fileName, mode)
  62.     {
  63.         errorMsg = null;
  64.    
  65.         try
  66.         {
  67.             base.constructor(fileName, mode);
  68.             isOpen = true;
  69.         }
  70.         catch (msg)
  71.         {
  72.             errorMsg = msg;
  73.             isOpen = false;
  74.         }
  75.     }
  76.    
  77.     function write(text)
  78.     {
  79.         foreach (char in text)
  80.         {
  81.             writen(char, 'b');
  82.         }
  83.     }
  84.    
  85.     function read(type = io_type.ALL)
  86.     {
  87.         if (type == io_type.LINE)
  88.         {
  89.             local line = "";
  90.             local char;
  91.            
  92.             while (!eos() && (char = readn('b')))
  93.             {
  94.                 if (char != '\n')
  95.                     line += char.tochar();
  96.                 else
  97.                     return line;
  98.             }
  99.            
  100.             return line.len() == 0 ? null : line;
  101.         }
  102.         else if (type == io_type.ALL)
  103.         {
  104.             local content = "";
  105.             local char;
  106.            
  107.             while (!eos() && (char = readn('b')))
  108.             {
  109.                 content += char.tochar();
  110.             }
  111.            
  112.             return content.len() == 0 ? null : content;
  113.         }
  114.        
  115.         return null;
  116.     }
  117.    
  118.     function close()
  119.     {
  120.         base.close();
  121.         isOpen = false;
  122.     }
  123.    
  124.     errorMsg = null;
  125.     isOpen = false;
  126. }
Add Comment
Please, Sign In to add comment