Thread: lua logic failing

  1. #1
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,582

    lua logic failing

    I can extract directories with the below code just fine but arguments like "src/libbasic/*.c" aren't resulting in anything (yes there are c files in that directory), any ideas?
    Code:
    local function _wildcard(arg)
    	if type(arg) ~= "string" then return {} end
    	print('# _wildcard("' .. arg .. '")')
    	local pfx, sfx, c, sep, j
    	local dir, sep, d = '.', '/', 0
    	local list = {}
    	local dirs = false
    	for i = 1, #arg, 1 do
    		c = arg:sub(i,i)
    		if c == '*' then break end
    		j = i
    		if c == '/' or c == '\\' then
    			sep = c
    			d = i
    		end
    	end
    	if not c then return list end
    	if j then
    		pfx = arg:sub(1,j)
    		if d > 0 then dir = arg:sub(1,d-1) end
    	end
    	if not j then sfx = arg:sub(2) else sfx = arg:sub(j+2) end
    	if sfx then
    		c = sfx:sub(#sfx)
    		if c == '/' or c == '\\' then
    			dirs = true
    			if #sfx > 1 then
    				sfx = sfx:sub(1,#sfx-1)
    			else
    				sfx = nil
    			end
    		end
    	end
    	for name in lfs.dir(dir) do
    		local path = dir .. sep .. name
    		local mode = lfs.attributes(path,"mode")
    		local same_mode = true
    		if dirs == true then
    			same_mode = (mode == "directory")
    		else
    			same_mode = (mode ~= "directory")
    		end
    		if name ~= '.' and name ~= '..' and same_mode then
    
    			local same_pfx, same_sfx = true, true
    			if not pfx then path = name
    			else
    				local seg = path:sub(1,j)
    				same_pfx = (seg == pfx)
    			end
    			if sfx then
    				local val = sfx
    				local seg = path:sub(#path - #sfx)
    				same_sfx = (seg == sfx)
    			end
    			if same_pfx == true and same_sfx == true then
    				table.insert(list, path)
    			end
    		end
    	end
    	return list
    end

  2. #2
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,582
    Never mind, finally spotted what I did wrong, I didn't realise I needed to minus 1 from the length of sfx, code now looks like this (I also noticed some left over code from experiments so fixed that too):
    Code:
    local function _wildcard(arg)
    	if type(arg) ~= "string" then return {} end
    	local pfx, sfx, c, sep, j
    	local dir, sep, d = '.', '/', 0
    	local list = {}
    	local dirs = false
    	for i = 1, #arg, 1 do
    		c = arg:sub(i,i)
    		if c == '*' then break end
    		j = i
    		if c == '/' or c == cfg.dir_sep then
    			sep = c
    			d = i
    		end
    	end
    	if not c then return list end
    	if j then
    		pfx = (arg:sub(1,j) or '')
    		sfx = (arg:sub(j+2) or '')
    		if d > 0 then dir = arg:sub(1,d-1) end
    	else
    		pfx = ''
    		sfx = (arg:sub(2) or '')
    	end
    	if sfx ~= '' then
    		c = sfx:sub(#sfx)
    		if c == '/' or c == cfg.dir_sep then
    			dirs = true
    			sfx = (sfx:sub(1,#sfx-1) or '')
    		end
    	end
    	for name in lfs.dir(dir) do
    		local path = dir .. sep .. name
    		local mode = lfs.attributes(path,"mode")
    		local same_pfx, same_sfx, same_mode
    		same_mode = (dirs == (mode == "directory"))
    		if name ~= '.' and name ~= '..' and same_mode == true then
    			if pfx == '' then
    				path = name
    				same_pfx = true
    			else
    				local seg = (path:sub(1,#pfx) or '')
    				same_pfx = (seg == pfx)
    			end
    			if sfx == '' then
    				same_sfx = true
    			else
    				local seg = (path:sub(#path - (#sfx-1)) or '')
    				same_sfx = (seg == sfx)
    			end
    			if same_pfx == true and same_sfx == true then
    				table.insert(list, path)
    			end
    		end
    	end
    	return list
    end

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. old failing brain
    By jcfuller in forum C Programming
    Replies: 7
    Last Post: 01-01-2016, 06:10 AM
  2. Why is this if statement failing?
    By xScen3xHdstyl3x in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2011, 02:37 AM
  3. CreateWindowEx() failing with 64-bit
    By domokato in forum Windows Programming
    Replies: 7
    Last Post: 07-02-2009, 07:30 PM
  4. LoadBitmap failing
    By zid in forum Windows Programming
    Replies: 7
    Last Post: 10-10-2005, 09:44 AM
  5. Failing to initialize COM
    By andyhunter in forum Windows Programming
    Replies: 8
    Last Post: 12-22-2004, 03:43 PM

Tags for this Thread