[CMake] SUBSTRING - STRING - shorten a string variable
Brandon Van Every
bvanevery at gmail.com
Mon Dec 3 15:00:59 EST 2007
On Dec 3, 2007 12:56 PM, Sören Freudiger <muffmolch at gmx.de> wrote:
>
>
>
>
> Hello
>
> Following problem:
>
>
>
> My CMakeList.txt file is somewhere like:
>
> E:/LBM_subversion/source/topology/amr3d/lbmd3q19/singlephase/testcases/RCF/all_in_one
>
>
>
> Now I want to have only the substring:
>
> E:/LBM_subversion/source
>
>
>
> Is it possible to get last string with the help of CMAKE_CURRENT_SOURCE_DIR?
> I just want to know the position of "source". Thus I can shorten the string
> stored in CMAKE_CURRENT_SOURCE_DIR very easy. I do not always know the path
> behind source/…
If you know the string contains "${CMAKE_CURRENT_SOURCE_DIR}" then you can do:
string(REPLACE
"${CMAKE_CURRENT_SOURCE_DIR}"
""
relative_path "${absolute_path}")
If you need to find out whether the string contains
"${CMAKE_CURRENT_SOURCE_DIR}" then the following will work as long as
"${CMAKE_CURRENT_SOURCE_DIR}" doesn't contain regex special
characters.
if(absolute_path MATCHES "^${CMAKE_CURRENT_SOURCE_DIR}")
# blah blah blah
If "${CMAKE_CURRENT_SOURCE_DIR}" contains regex special characters,
you'll have to double escape them.
string(REPLACE
"\\"
"\\\\"
escaped_path "${toplevel_path}")
string(REGEX REPLACE
"([][.?*+|()$^-])"
"\\\\\\1"
escaped_path "${escaped_path}")
if(absolute_path MATCHES "^${escaped_path}")
string(REPLACE
"${toplevel_path}"
""
relative_path "${absolute_path}")
Cheers,
Brandon Van Every
More information about the CMake
mailing list