Format.lua

From Alpine Linux
Revision as of 18:50, 28 November 2007 by Ms13sp (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

dostounix

INPUT:
This library function required the following inputs/parameters.

  • file contents
    • Example: c = file:read("*a")
  • String with EOL

OUTPUT:
This library function deliverers the following output/parameters.

  • Output is returned minus the \r characters

CODING EXAMPLE:

-- Include/Call for this library
require("fs")
-- file test.txt is from dos/windows
require "fs"
f = io.open("test.txt")
c = f:read("*a")
file = fs.dostounix(c)
--file now contains the contents of test.txt without the \r char

remove_blanks_comments

INPUT:
This library function required the following inputs/parameters.

  • Path
    • Example: /root/mynotes

OUTPUT:
This library function deliverers the following output/parameters.

  • Returns a table to iterate over minus the blank and commented lines

CODING EXAMPLE:

-- Include/Call for this library
require("fs")
t = fs.remove_blanks_comments("/etc/shorewall/rules")
-- t is a table with only pertinent lines

search_replace

INPUT:
This library function required the following inputs/parameters.

  • Path to file
    • Example: /root/mynotes
  • Search
    • What to find
  • Replace
    • What to replace the found items with

OUTPUT:
This library function deliverers the following output/parameters.

  • Table

CODING EXAMPLE:

-- Include/Call for this library
require("fs")
t = fs.search_replace("/etc/shorewall/interfaces", "eth0", "eth1")
--t will be a table that contains /etc/shorewall/interfaces with eth0 replaced as eth1

ipairs_string

INPUT:
This library function required the following inputs/parameters.

  • Table

OUTPUT:
This library function deliverers the following output/parameters.

  • String

CODING EXAMPLE:

-- Include/Call for this library
require("fs")
t = { "eth0", "eth1", "lo"}
fs.ipairs_string(t)
--will give you a table that can be writing one per line for a file.


table_to_string

INPUT:
This library required the following inputs/parameters.

  • Delimiter
    • Could be one or more chars.
  • Array
    • Data which is to be joined into a string.

OUTPUT:
This library deliverers the following output/parameters.

  • String
    • Could be something like "Word, Word, Word, Word"

CODING EXAMPLE:

-- Set variable/Call for this library
require "fs"
-- Create a array of data (Not sure if the next row is correct)
arraytojoin = { "Bird", "Fish", "Cow", "Hammer" }
-- Process the data (note the delimiter)
liboutput = fs.table_to_string(";", arraytojoin)
Bird;Fish;Cow;Hammer

string_to_table

INPUT:
This library required the following inputs/parameters.

  • Delimiter
    • Could be one or more chars.
  • String or line from a file: bobo~foo~bar~baz~1
    • Data is to be split into a table

OUTPUT:
This library deliverers the following output/parameters.

  • Table
    • { 1 = "bobo", 2 = "foo", 3 = "bar", 4 = "baz", 5 = 1}

CODING EXAMPLE:

-- Set variable/Call for this library
require "fs"
-- Grab the line from a file or input it into the funtcion
line = "bobo~foo~bar~baz~1"
-- Process the data (note the delimiter)
t = fs.string_to_table("~", line)
't' would contain: 
{ 1 = "bobo", 2 = "foo", 3 = "bar", 4 = "baz", 5 = 1}
for a,b in ipairs(t) do print(a,b) end
1 bobo
2 foo
3 bar
4 baz
5 1