U E D R S I H C RSS
ID
Password
Join
지옥으로 통하는 길은 걷기가 쉽다. -아브데라

 * VRML 화일내에서 특정 부분만을 읽어 첫번째 인자로 주어진 화일에 적는 단순 예제입니다.
  • lua convert.lua result.txt 1000이라고 적으면 result.txt에 mocap50_1.wrl ~ mocap50_1000.wrl의 데이타를 훑어서 기록하게 됩니다.
  • 빠진화일 체크하는 부분을 보면 io.open()으로 여는데 실패했을 경우(nil반환)에도 fh:close()를 해주는 것을 볼 수 있습니다. 이거를 안하면 오동작을 하더군요. (1047번 화일을 열고 더이상 열리지 않습니다.) 우연히 발견한 "불편한" 점이랄까요... 주의하시길 바랍니다.
  • lua 5.0 정식 기준.
  • convert.lua 소스
    function stripspace(str)
    	if str == nil then return nil end
    	for i = 1, string.len(str), 1 do
    		if string.byte(str, i) ~= 32 then 
    			tmpstr = string.sub(str, i)
    			tmpstr = string.gsub(tmpstr,", ","\t")
    			tmpstr = string.gsub(tmpstr," ","\t")
    			tmpstr = string.gsub(tmpstr,"]","")
    			return tmpstr
    		end
    	end
    end
    
    -- 화일 존재유무 체크
    print("빠진화일목록 :")
    bValid = true
    for i = 1, tonumber(arg[2]) do
    	filename = string.format("MoCap50_%d.wrl", i)
    	fh = io.open(filename)
    	if fh == nil then
    		print(filename)
    		bValid = false
    	end
    	fh:close()
    end
    
    if bValid then
    	print ("빠진화일이 없습니다. 처리를 진행합니다.")
    
    	newfile = io.open(arg[1], "w+")
    	for i = 1, tonumber(arg[2]) do
    		print(i)
    		filename = string.format("MoCap50_%i.wrl", i)
    		fh = io.open(filename)
    		if fh == nil then
    			print(string.format("%s 화일이 없습니다. 처리를 종료합니다.", filename))
    			break
    		end
    			
    		oneline = fh:read()
    		bwrite = false
    		tmptbl = {}
    		while oneline ~= nil do
    			if string.find(oneline, "coord DEF Plane04-COORD Coordinate", 1, true) ~= nil then
    				bwrite = true
    			elseif bwrite then
    				if string.find(oneline, "}") ~= nil then
    					bwrite = false
    				else
    					newfile:write(stripspace(oneline))
    				end
    			end
    			oneline = fh:read()
    		end
    		fh:close()
    		newfile:write("\n")
    	end
    	newfile:close()
    end
    print("처리가 종료했습니다.")
    

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2010-10-28 12:42:52
Processing time 0.0621 sec