From ba9c10d566d44ac0dba683114aeed96938b2fb17 Mon Sep 17 00:00:00 2001 From: Rafael Kitover Date: Sun, 26 Jan 2020 23:35:09 +0000 Subject: [PATCH] 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 --- cmake/FileIterator.cmake | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/cmake/FileIterator.cmake b/cmake/FileIterator.cmake index c20011f5..98b56c56 100644 --- a/cmake/FileIterator.cmake +++ b/cmake/FileIterator.cmake @@ -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)