1. Trying to concatenating strings to a file path
- Posted by Icy_Viking in May
- 401 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
2. Re: Trying to concatenating strings to a file path
- Posted by ChrisB (moderator) in May
- 378 views
Hi
I've had this pain before, the confusion arising from "
", "\" and '\', remembering that a \ is also an escape character.
I've not recently tested this, but sometimes it's easier to say
object slash = '\'
and then use that in your directory / filename creation
eg
"c:\\the_directory\\the_subdirectory" & slash & the_filename_sequence
or some variance thereof.
Cheers
Chris
3. Re: Trying to concatenating strings to a file path
- Posted by axtens_bruce in May
- 373 views
object slash = '\'
SLASH would seem to be just the ticket then. And don't forget join_path
-Bruce
4. Re: Trying to concatenating strings to a file path
- Posted by ghaberek (admin) in May
- 358 views
I'm trying to concatenate strings to a file path, need a little help.
I would lean a bit more into the standard library routines.
--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)
You can use prompt_string() here to request input from the user. This automatically strips the newline added by gets().
include std/console.e --Prompt the user to enter a directory path sequence input_path = prompt_string("Please enter the directory path: ")
-- ???? ?? ?? ??????? ??????? ???? ?????? ???? regex backslash_re = re:new (`\\`) sequence words = re:split(backslash_re,input_path) sequence directory_name = words[length(words)]
The split() function in std/regex.e is a bit heavy-handed and should only be used when you need to split using a pattern. You can use the split() function in std/sequence.e when your delimiter is static (i.e. not a pattern).
Also note that I am using the SLASH constant from std/filesys.e instead of hard-coding the backslash character. This is helpful for creating portable code and to avoid confusion about escape characters.
include std/filesys.e include std/sequence.e sequence words = stdseq:split(input_path, SLASH) sequence directory_name = words[$]
Better yet, if all you want is the "name" from a path, use the filename() function in std/filesys.e. It will return the name of the last item in the path as long as the path does not end in a slash.
include std/filesys.e include std/text.e input_path = text:trim_tail(input_path, SLASH) sequence directory_name = filesys:filename(input_path)
-- ????? ?? ????? ???? - ???? sequence asm_=".asm" sequence output_filename = input_path&"\\"&directory_name & asm_ puts(1 ,output_filename )
I would recommend either concatenating the strings with the SLASH constant or using join_path() from std/filesys.e. Although I think join_path() still erroneously adds a leading slash to the returned path on Windows. You may need to trim that off for the time being.
include std/filesys.e sequence output_filename = input_path & SLASH & directory_name & SLASH & asm_ -- or -- include std/filesys.e include std/text.e sequence output_filename = filesys:join_path({input_path,directory_name,asm_}) output_filename = text:trim_head(output_filename, SLASH)
-- ???? ????? ????? ??????? file_list = dir(input_path & "\\*.vm")
Same as above, it's much easier to concatenate with the SLASH constant.
include std/filesys.e file_list = dir(input_path & SLASH & "*.vm")
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
You can use the filebase() function in std/filesys.e to get just the "base" name of the file without the extension.
include std/filesys.e function remove_extension(sequence file_name) return filesys:filebase(file_name) end function
If you want the full path and file name without extension, you could concatenate them together like this.
include std/filesys.e function remove_extension(sequence file_name) return filesys:pathname(file_name) & SLASH & filesys:filebase(file_name) end function
Hope that helps.
-Greg
5. Re: Trying to concatenating strings to a file path
- Posted by Icy_Viking in May
- 331 views
Thanks for all the help, guys!