Excluding lines containing a specific string from REGEX REPLACE in CMake -
this first post have been using stackoverflow years. helping me every time.
i writing script in cmake supposed rewrite portion of .cfg file (it resources.cfg file ogre 3d engine) that:
- when running config cmake, absolute paths set according system
- when installing, files copied folder , relative paths set instead
i focus on first part, since both similar. resource file working on:
# resources required sample browser , samples. [essential] zip=stufftochange/media/packs/sdktrays.zip # resource locations added default path [general] filesystem=../media #local filesystem=stufftochange/media/materials/scripts filesystem=stufftochange/media/materials/textures filesystem=stufftochange/media/models filesystem=stufftochange/media/materials/programs/hlsl_cg filesystem=stufftochange/media/materials/programs/glsl
my current strategy lines without #local identifier should affected regex replace
statement. current code is:
file(read ${cmake_source_dir}/cfg/resources.cfg resources_file) string(regex replace "/media" "${ogre_home_backslashes}/media" resources_file_modified ${resources_file}) file(write ${cmake_source_dir}/cfg/resources.cfg ${resources_file_modified})
which replaces /media
occurrences. need replace stufftochange
(that can before string media
) if #local
not @ end of line. tried modify matching expression in lots of ways but, when do, first , last line replaced properly. suspect has line endings.
these of expressions tried, without luck:
([^ #]*)=[^ ]*/media([^#\n$]*) =[^ ]*/media([^#\n$]*) /media([^# ]*) /media([^#\n ]*)
of course used \\1
, \\2
save parts of string want keep.
i did few searches on google , stackoverflow couldn't find proper solution or guide using cmake's regex replace (and documentation basic). idea holy grail match expression be?
cmake's regex syntax , documentation pretty limited. i'd favour turning file's contents list of strings, each string being line in file. iterating these makes regex simpler:
set(sourcefile "${cmake_source_dir}/cfg/resources.cfg") file(read ${sourcefile} contents) # set variable "esc" ascii value 27 - # unlikely conflict in file contents. string(ascii 27 esc) # turn contents list of strings, each ending esc. # allows preserve blank lines in file since cmake # automatically prunes empty list items during foreach loop. string(regex replace "\n" "${esc};" contentsaslist "${contents}") unset(modifiedcontents) foreach(line ${contentsaslist}) # don't modify line if contains #local @ end. if(not "${line}" matches "#local${esc}$") string(regex replace "=.*/media" "=${ogre_home_backslashes}/media" line ${line}) endif() # swap appended esc character out in favour of line feed string(regex replace "${esc}" "\n" line ${line}) set(modifiedcontents "${modifiedcontents}${line}") endforeach() file(write ${sourcefile} ${modifiedcontents})
if don't care preserving blank lines, can use file(strings ...)
read in file, makes life bit simpler:
set(sourcefile "${cmake_source_dir}/cfg/resources.cfg") file(strings ${sourcefile} contents) unset(modifiedcontents) foreach(line ${contents}) # don't modify line if contains #local @ end. if(not "${line}" matches "#local$") string(regex replace "=.*/media" "=${ogre_home_backslashes}/media" line ${line}) endif() set(modifiedcontents "${modifiedcontents}${line}\n") endforeach() file(write ${sourcefile} ${modifiedcontents})
probably best description of cmake's regex syntax found in docs string
.
Comments
Post a Comment