CTK  0.1.0
The Common Toolkit is a community effort to provide support code for medical image analysis, surgical navigation, and related projects.
ctkMacroListFilter.cmake
Go to the documentation of this file.
1 
2 #! See http://www.cmake.org/Wiki/CMakeMacroListOperations#LIST_FILTER
3 #!
4 #! Usage:
5 #! \code
6 #! ctkMacroListFilter(<list> <regexp_var> [<regexp_var> ...]
7 #! [OUTPUT_VARIABLE <variable>])
8 #! \endcode
9 #!
10 #! Removes items from <list> which do not match any of the specified
11 #! regular expressions. An optional argument OUTPUT_VARIABLE
12 #! specifies a variable in which to store the matched items instead of
13 #! updating <list>
14 #! As regular expressions can not be given to macros (see bug #5389), we pass
15 #! variable names whose content is the regular expressions.
16 #! Note that this macro requires PARSE_ARGUMENTS macro, available here:
17 #! http://www.cmake.org/Wiki/CMakeMacroParseArguments
18 #!
19 #! \ingroup CMakeUtilities
20 macro(ctkMacroListFilter)
21  ctkMacroParseArguments(LIST_FILTER "OUTPUT_VARIABLE" "" ${ARGV})
22  # Check arguments.
23  list(LENGTH LIST_FILTER_DEFAULT_ARGS LIST_FILTER_default_length)
24  if(${LIST_FILTER_default_length} EQUAL 0)
25  message(FATAL_ERROR "LIST_FILTER: missing list variable.")
26  endif()
27  if(${LIST_FILTER_default_length} EQUAL 1)
28  message(FATAL_ERROR "LIST_FILTER: missing regular expression variable.")
29  endif()
30  # Reset output variable
31  if(NOT LIST_FILTER_OUTPUT_VARIABLE)
32  set(LIST_FILTER_OUTPUT_VARIABLE "LIST_FILTER_internal_output")
33  endif()
34  set(${LIST_FILTER_OUTPUT_VARIABLE})
35  # Extract input list from arguments
36  list(GET LIST_FILTER_DEFAULT_ARGS 0 LIST_FILTER_input_list)
37  list(REMOVE_AT LIST_FILTER_DEFAULT_ARGS 0)
38  foreach(LIST_FILTER_item ${${LIST_FILTER_input_list}})
39  foreach(LIST_FILTER_regexp_var ${LIST_FILTER_DEFAULT_ARGS})
40  foreach(LIST_FILTER_regexp ${${LIST_FILTER_regexp_var}})
41  if(${LIST_FILTER_item} MATCHES ${LIST_FILTER_regexp})
42  list(APPEND ${LIST_FILTER_OUTPUT_VARIABLE} ${LIST_FILTER_item})
43  endif()
44  endforeach()
45  endforeach()
46  endforeach()
47  # If OUTPUT_VARIABLE is not specified, overwrite the input list.
48  if(${LIST_FILTER_OUTPUT_VARIABLE} STREQUAL "LIST_FILTER_internal_output")
49  set(${LIST_FILTER_input_list} ${${LIST_FILTER_OUTPUT_VARIABLE}})
50  endif()
51 endmacro(ctkMacroListFilter)