mirror of
https://github.com/visualboyadvance-m/visualboyadvance-m
synced 2025-10-05 23:52:49 +02:00
cmake: Make FileIterator more correct, add eg..
Add macro fi_check_done() to set fi_done if leftover contents is empty, use it in both fi_open_file() and fi_get_next_line(). This fixes the behavior when the opened file is empty and makes the code cleaner. Add a usage example to top comment. Signed-off-by: Rafael Kitover <rkitover@gmail.com>
This commit is contained in:
@@ -1,10 +1,31 @@
|
||||
# Interface for iterating over a text file by line.
|
||||
#
|
||||
# Example usage:
|
||||
#
|
||||
# fi_open_file(${some_file})
|
||||
#
|
||||
# while(NOT fi_done)
|
||||
# fi_get_next_line()
|
||||
#
|
||||
# message(STATUS "read line: ${fi_line}")
|
||||
# endwhile()
|
||||
|
||||
macro(fi_check_done)
|
||||
string(LENGTH "${fi_file_contents}" len)
|
||||
|
||||
set(fi_done FALSE PARENT_SCOPE)
|
||||
|
||||
if(len EQUAL 0)
|
||||
set(fi_done TRUE PARENT_SCOPE)
|
||||
endif()
|
||||
endmacro()
|
||||
|
||||
function(fi_open_file file)
|
||||
file(READ "${file}" fi_file_contents)
|
||||
|
||||
set(fi_file_contents "${fi_file_contents}" PARENT_SCOPE)
|
||||
set(fi_done FALSE PARENT_SCOPE)
|
||||
|
||||
fi_check_done()
|
||||
endfunction()
|
||||
|
||||
function(fi_get_next_line)
|
||||
@@ -16,13 +37,7 @@ function(fi_get_next_line)
|
||||
|
||||
string(SUBSTRING "${fi_file_contents}" ${pos} -1 fi_file_contents)
|
||||
|
||||
string(LENGTH "${fi_file_contents}" len)
|
||||
|
||||
set(fi_done FALSE PARENT_SCOPE)
|
||||
|
||||
if(len EQUAL 0)
|
||||
set(fi_done TRUE PARENT_SCOPE)
|
||||
endif()
|
||||
fi_check_done()
|
||||
|
||||
set(fi_line "${fi_line}" PARENT_SCOPE)
|
||||
set(fi_file_contents "${fi_file_contents}" PARENT_SCOPE)
|
||||
|
Reference in New Issue
Block a user