gerkrt

Manage text files with event commands

Sep 14th, 2011
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 6.08 KB | None | 0 0
  1. #==============================================================================
  2. # Manage text files with event commands
  3. # By gerkrt/gerrtunk
  4. # Version: 1
  5. # License: GPL, credits
  6. # Date: 05/01/2011
  7. # IMPORTANT NOTE: to acces the more actualitzed or corrected version of this
  8. # script check here: http://usuarios.multimania.es/kisap/english_list.html
  9. #==============================================================================
  10.  
  11. =begin
  12.  
  13. -------INTRODUCTION-----------
  14.  
  15. This script gives a little control to create and modifiy text files to eventers.
  16. You can create a file, erase it, append text, add or remove a specific line.
  17. Also you can configure the files extensions and paths.
  18.  
  19. ----------INSTRUCTIONS----------------
  20.  
  21. 1. First of all you have to create the text file. You do this with the
  22. script command newTxt. That will create a new text file called at your will.
  23.  
  24. 2. Then you can erase it using delTxt or modify it and continue to append contents
  25. using appTxt, that add new text after the existent one.
  26.  
  27. You can also check for the rest of the functions to do other things:
  28.  
  29. newTxt(name, text). This command create a new text file named 'name' and adds
  30. the text 'text.
  31.  
  32. delTxt(name). This deletes the text file of the specified name.
  33.  
  34.   newTxt("test", "Initial text") --> Creates a test.txt file with  Initial text inside.
  35.   delTxt("test") --> Erases it
  36.  
  37. appTxt(name, text). Appends at the end of the file a new text. If you make
  38. a newTxt over a file that already exist it will be overwritten.
  39.  
  40.   With the file called "test":
  41.   "start of file and line 1
  42.   line 2
  43.   line3
  44.   end of file and line 4
  45.   "
  46.   If you do a appTxt("test", "Append \n me \n))  it will result in:
  47.  
  48.   "start of file and line 1
  49.   line 2
  50.   line3
  51.   end of file and line 4
  52.   Append
  53.   me
  54.  
  55.   "
  56.  
  57. addLine(name, text, line). Insert a line of text in the specified position of
  58. the file. A line means a piece of text thats beyond two \n (or the end of start
  59. of the file). The insert moves all the lines after the one you are pushing, so:
  60.  
  61.   With the file called "test":
  62.   "start of file and line 1
  63.   line 2
  64.   line3
  65.   end of file and line 4
  66.   "
  67.   If you do a addLine("test", "test line \n", 2) it will be inserted where the
  68.   actual line 2 is, so it will result in:
  69.  
  70.   "start of file and line 1
  71.   test line
  72.   line 2
  73.   line3
  74.   end of file and line 4
  75.   "
  76.  
  77.  
  78. delLines(name, lines). Deletes a list of lines from the file named 'name'.
  79. Works in a similar way that the add line but deleting them. It also
  80. accepts more lines to erase:
  81.  
  82.  
  83.   With the file called "test":
  84.   "start of file and line 1
  85.   line 2
  86.   line3
  87.   end of file and line 4
  88.   "
  89.   If you do a delLine("test", [2,4])  it will result in:
  90.  
  91.   "start of file and line 1
  92.   line3
  93.   "
  94.  
  95. -------SPECIAL CARACTERS------------
  96.  
  97. You can add new lines marks and tabulations marks using these codes:
  98.  
  99. \t (tab)
  100. \n (newline)
  101.  
  102. This works internally for the system, but the reader doesent see ethese symbols
  103. but their true meaning. So if you write:
  104.  
  105. text = " testing \n \ttabulating it"
  106.  
  107. It will read as:
  108.  
  109. testing
  110.   tabulating it
  111.  
  112. ------CONFIGURATION-------------
  113.  
  114. Texts_file_path: Ifor you put this to empty "", it wont create a folder.
  115. Also you have to add the \\ at the end of the folder name.
  116.  
  117. Texts_file_extension: You can changue the extension of all the text files.
  118.  
  119.  
  120. =end
  121.  
  122. # Configuration
  123. module Wep
  124.   Texts_file_path = "Text Files\\"
  125.   Texts_file_extension = ".txt"
  126. end
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134. # Create Folder for text files
  135. if Wep::Texts_file_path  != ''
  136.     Dir.mkdir(Wep::Texts_file_path) if !FileTest.directory?(Wep::Texts_file_path)
  137. end
  138.  
  139. class Interpreter
  140.   #--------------------------------------------------------------------------
  141.   # * newTxt
  142.   # creates a new text file
  143.   # name: file name, text: text to write in the file
  144.   #--------------------------------------------------------------------------
  145.   def newTxt(name, text='')
  146.     file = open(Wep::Texts_file_path + name + Wep::Texts_file_extension, 'w')
  147.     file.write text
  148.     file.close
  149.   end
  150.  
  151.   #--------------------------------------------------------------------------
  152.   # * delTxt
  153.   # deletes a  text file
  154.   # name: file name
  155.   #--------------------------------------------------------------------------
  156.   def delTxt(name)
  157.     File.delete Wep::Texts_file_path + name + Wep::Texts_file_extension
  158.   end
  159.  
  160.   #--------------------------------------------------------------------------
  161.   # * addLine
  162.   # insert line in text file
  163.   # name: file name, text: text to write in the file, line: line number(-1 internal)
  164.   #--------------------------------------------------------------------------
  165.   def addLine(name, text, line)
  166.     # Extract a array of lines from the file and insert text
  167.     line_arr = File.readlines(Wep::Texts_file_path + name + Wep::Texts_file_extension)
  168.     line_arr.insert(line-1, text)
  169.     # Save it
  170.     file = open(Wep::Texts_file_path + name + Wep::Texts_file_extension, 'w')
  171.     file.write line_arr
  172.     file.close
  173.   end
  174.  
  175.   #--------------------------------------------------------------------------
  176.   # * delLines
  177.   # deletes lines in text file
  178.   # name: file name, lines: array of line numbers to erase(-1 internal)
  179.   #--------------------------------------------------------------------------
  180.   def delLines (name, lines)
  181.     # Extract a array of lines from the file and insert text
  182.     line_arr = File.readlines(Wep::Texts_file_path + name + Wep::Texts_file_extension)
  183.     # Delete specified lines
  184.     lines.each do |index|
  185.       line_arr.delete_at(index-1)
  186.     end
  187.     # Save it
  188.     File.open(Wep::Texts_file_path+name+Wep::Texts_file_extension, "w") do |f|
  189.       line_arr.each{|line| f.puts(line)}
  190.     end
  191.   end
  192.  
  193.   #--------------------------------------------------------------------------
  194.   # * appTxt
  195.   # appends text to a  text file
  196.   # name: file name text: text to append
  197.   #--------------------------------------------------------------------------
  198.   def appTxt (name, text)
  199.     file = open(Wep::Texts_file_path + name + Wep::Texts_file_extension, 'a')
  200.     file.write text
  201.     file.close
  202.   end
  203.  
  204. end
Advertisement
Add Comment
Please, Sign In to add comment