1 -------------------------------------------------------------------------------
3 -- @release $Id: util.lua,v 1.16 2008/02/17 06:42:51 jasonsantos Exp $
4 -------------------------------------------------------------------------------
6 local posix = require "nixio.fs"
7 local type, table, string, io, assert, tostring, setmetatable, pcall = type, table, string, io, assert, tostring, setmetatable, pcall
9 -------------------------------------------------------------------------------
10 -- Module with several utilities that could not fit in a specific module
14 -------------------------------------------------------------------------------
15 -- Removes spaces from the beginning and end of a given string
16 -- @param s string to be trimmed
17 -- @return trimmed string
20 return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
23 -------------------------------------------------------------------------------
24 -- Removes spaces from the beginning and end of a given string, considering the
25 -- string is inside a lua comment.
26 -- @param s string to be trimmed
27 -- @return trimmed string
31 function trim_comment (s)
32 s = string.gsub(s, "^%s*%-%-+%[%[(.*)$", "%1")
33 s = string.gsub(s, "^%s*%-%-+(.*)$", "%1")
37 -------------------------------------------------------------------------------
38 -- Checks if a given line is empty
39 -- @param line string with a line
40 -- @return true if line is empty, false otherwise
42 function line_empty (line)
43 return (string.len(trim(line)) == 0)
46 -------------------------------------------------------------------------------
47 -- Appends two string, but if the first one is nil, use to second one
48 -- @param str1 first string, can be nil
49 -- @param str2 second string
50 -- @return str1 .. " " .. str2, or str2 if str1 is nil
52 function concat (str1, str2)
53 if str1 == nil or string.len(str1) == 0 then
56 return str1 .. " " .. str2
60 -------------------------------------------------------------------------------
61 -- Split text into a list consisting of the strings in text,
62 -- separated by strings matching delim (which may be a pattern).
63 -- @param delim if delim is "" then action is the same as %s+ except that
64 -- field 1 may be preceded by leading whitespace
65 -- @usage split(",%s*", "Anna, Bob, Charlie,Dolores")
66 -- @usage split(""," x y") gives {"x","y"}
67 -- @usage split("%s+"," x y") gives {"", "x","y"}
68 -- @return array with strings
71 function split(delim, text)
73 if string.len(text) > 0 then
76 -- if delim matches empty string then it would give an endless loop
77 if string.find("", delim, 1) and delim ~= "" then
78 error("delim matches empty string!")
83 first, last = string.find(text, delim, pos)
85 first, last = string.find(text, "%s+", pos)
88 first, last = string.find(text, "%s+", pos)
91 if first then -- found?
92 table.insert(list, string.sub(text, pos, first-1))
95 table.insert(list, string.sub(text, pos))
103 -------------------------------------------------------------------------------
104 -- Comments a paragraph.
105 -- @param text text to comment with "--", may contain several lines
106 -- @return commented text
108 function comment (text)
109 text = string.gsub(text, "\n", "\n-- ")
113 -------------------------------------------------------------------------------
114 -- Wrap a string into a paragraph.
115 -- @param s string to wrap
116 -- @param w width to wrap to [80]
117 -- @param i1 indent of first line [0]
118 -- @param i2 indent of subsequent lines [0]
119 -- @return wrapped paragraph
121 function wrap(s, w, i1, i2)
125 assert(i1 < w and i2 < w, "the indents must be less than the line width")
126 s = string.rep(" ", i1) .. s
127 local lstart, len = 1, string.len(s)
128 while len - lstart > w do
130 while i > lstart and string.sub(s, i, i) ~= " " do i = i - 1 end
132 while j > lstart and string.sub(s, j, j) == " " do j = j - 1 end
133 s = string.sub(s, 1, j) .. "\n" .. string.rep(" ", i2) ..
134 string.sub(s, i + 1, -1)
135 local change = i2 + 1 - (i - j)
142 -------------------------------------------------------------------------------
143 -- Opens a file, creating the directories if necessary
144 -- @param filename full path of the file to open (or create)
145 -- @param mode mode of opening
146 -- @return file handle
148 function posix.open (filename, mode)
149 local f = io.open(filename, mode)
151 filename = string.gsub(filename, "\\", "/")
153 for d in string.gfind(filename, ".-/") do
157 f = io.open(filename, mode)
163 ----------------------------------------------------------------------------------
164 -- Creates a Logger with LuaLogging, if present. Otherwise, creates a mock logger.
165 -- @param options a table with options for the logging mechanism
166 -- @return logger object that will implement log methods
168 function loadlogengine(options)
169 local logenabled = pcall(function()
171 require "logging.console"
174 local logging = logenabled and logging
177 if options.filelog then
178 logger = logging.file("luadoc.log") -- use this to get a file log
180 logger = logging.console("[%level] %message\n")
183 if options.verbose then
184 logger:setLevel(logging.INFO)
186 logger:setLevel(logging.WARN)
190 noop = {__index=function(...)
197 setmetatable(logger, noop)