diff options
author | Natanael Copa <natanael.copa@gmail.com> | 2007-07-27 12:53:38 +0000 |
---|---|---|
committer | Natanael Copa <natanael.copa@gmail.com> | 2007-07-27 12:53:38 +0000 |
commit | dc53423183a0c459284ebd139022b707f01af006 (patch) | |
tree | 8a67a2904ec991028bddd429d57eec114b05baab /lib/ed.lua | |
parent | 275c80281ba2e84b8d810bdb1c2b7f8c9a4333d9 (diff) | |
download | acf-core-dc53423183a0c459284ebd139022b707f01af006.tar.bz2 acf-core-dc53423183a0c459284ebd139022b707f01af006.tar.xz |
moved core files to new dir structurev2.0_alpha1
git-svn-id: svn://svn.alpinelinux.org/acf/core/trunk@219 ab2d0c66-481e-0410-8bed-d214d4d58bed
Diffstat (limited to 'lib/ed.lua')
-rw-r--r-- | lib/ed.lua | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/lib/ed.lua b/lib/ed.lua new file mode 100644 index 0000000..2e86f08 --- /dev/null +++ b/lib/ed.lua @@ -0,0 +1,60 @@ +#!/usr/bin/lua + +require "object" + +-- ed object +Ed = Object:new{ + filename = nil, + lines = {} +} + + +-- openfile and read it to table +function Ed:open( filename, mode ) + local f = io.open( filename, mode ) + -- check that open was success + if f == nil then + return nil + end + + -- read the lines + for line in f:lines() do + table.insert( self.lines, line ) + end + f:close() + self.filename = filename + return self.lines +end + + +-- search and replace on lines that matches linematch +function Ed:find_gsub( linematch, search, replace, limit ) + local i, line + for i, line in ipairs( self.lines ) do + if string.find( line, linematch ) then + self.lines[i] = string.gsub( line, search, replace, limit ) + end + end +end + + +-- Write the table to file again +function Ed:flush( filename ) + local f = io.open( filename, "w" ) + if f == nil then + return false + end + + -- write each line to file + for i, line in ipairs( self.lines ) do + f:write(line .. "\n") -- test this! + end + f:close() + return true +end + +function Ed:insert( line ) + self.lines:insert( line ) +end + + |