Trying to concatenating strings to a file path
- Posted by Icy_Viking May 07, 2024
- 565 views
Hi all,
I'm trying to concatenate strings to a file path, need a little help.
include std/filesys.e include std/text.e include std/regex.e as re include std/console.e include std/convert.e sequence file_name integer counter, file_handle_out --procedure main() sequence file_list --integer file_handle_out --Prompt the user to enter a directory path puts(1, "Please enter the directory path: " & "\n") -- Read the entered path sequence input_path = gets(0) input_path = head(input_path, length(input_path)-1) -- ???? ?? ?? ??????? ??????? ???? ?????? ???? regex backslash_re = re:new (`\\`) sequence words = re:split(backslash_re,input_path) sequence directory_name = words[length(words)] -- ????? ?? ????? ???? - ???? sequence asm_=".asm" sequence output_filename = input_path&"\\"&directory_name & asm_ puts(1 ,output_filename ) -- ????? ???? ???? ?????? file_handle_out = open(output_filename , "w") if file_handle_out = -1 then puts(1, "Error: Could not open output file.\n") abort(1) end if -- ???? ????? ????? ??????? file_list = dir(input_path & "\\*.vm") integer j = 1 while j <= length(file_list) do file_name = file_list[j] sequence path_file= input_path & "\\" & file_name printf(1, "\n%s\n", path_file) printf(1, "\n%s\n", file_name) process_file(path_file, file_name) j += 1 end while close(file_handle_out) puts(1, "Output file is ready: " & output_filename & "\n") function remove_extension(sequence file_name) regex dot_re = re:new (".") sequence remove_vm = re:split(dot_re, file_name) return remove_vm[1] end function procedure process_file(sequence input_file_path, sequence file_name) integer file_handle_in --sequence line counter = 0 printf(1, "%s\n", input_file_path) --file_handle_in = open(input_file_path & ".vm", "r") file_handle_in = open(input_file_path, "r") if file_handle_in != -1 then object line while sequence(line) entry do --line = gets(file_handle_in) process_line(line) end while close(file_handle_in) -- Print end-of-input-file message puts(1, "End of input file: " & file_name & ".vm\n") else puts(1, "Failed to open input file: " & input_file_path & ".vm\n") end if end procedure procedure process_line(sequence line) regex space_re = re:new (` `) sequence words = re:split(space_re, line) if length(words) > 0 then sequence command = words[1] if equal(command, "push") then handlePush(words[2],words[3]) elsif equal(command, "pop") then handlePop(words[2],words[3]) elsif equal(command, "add") then handleAdd() elsif equal(command, "sub") then handleSub() elsif equal(command, "neg") then handleNeg() elsif equal(command, "eq") then handleEq() elsif equal(command, "gt") then handleGt() elsif equal(command, "lt") then handleLt() else puts(1, "Unknown command: " & command & "\n") end if end if end procedure -- Define helper functions for each VM command here procedure handlePush(sequence segment,sequence index) puts(file_handle_out, "command: push segment: " & segment & " index: " & index & "\n") end procedure procedure handlePop(sequence segment,sequence index) puts(file_handle_out, "command: pop segment: " & segment & " index: " & index & "\n") end procedure procedure handleAdd() puts(file_handle_out, "command: add\n") end procedure procedure handleSub() puts(file_handle_out, "command: sub\n") end procedure procedure handleNeg() puts(file_handle_out, "command: neg\n") end procedure procedure handleEq() puts(file_handle_out, "command: eq\n") counter=counter+1 puts(file_handle_out, "counter: " & counter & "\n") end procedure procedure handleGt() puts(file_handle_out, "command: gt\n") counter=counter+1 puts(file_handle_out, "counter: " & counter & "\n") end procedure procedure handleLt() puts(file_handle_out, "command: lt\n") counter=counter+1 puts(file_handle_out, "counter: " & counter & "\n") end procedure