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:
Rafael Kitover 2020-01-26 23:35:09 +00:00
parent ed4d1b9827
commit ba9c10d566
No known key found for this signature in database
GPG Key ID: 08AB596679D86240
1 changed files with 23 additions and 8 deletions

View File

@ -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)