AW: [CMake] SUBSTRING - STRING - shorten a string variable

Sören Freudiger muffmolch at gmx.de
Mon Dec 3 16:12:23 EST 2007


No, it's the other way around.

e.g.
CMAKE_CURRENT_SOURCE_DIR =
"E:/LBM_subversion/source/topology/amr3d/lbmd3q19/singlephase/testcases/RCF/
all_in_one"

And I'm looking for
Path="E:/LBM_subversion/source/"

And the keyword I'm looking for is:

"/source/"
That's the only keyword that's possible...
The path to source can be different and also the path to the CMakeLists.txt
(depend on the project and the machine I'm on).

So later one I'll check if the folder "roost/basics" exists... then the
"source" folder is correct. Else wise I have to set the path manually.

SirAnn

 

-----Ursprüngliche Nachricht-----
Von: cmake-bounces+muffmolch=gmx.de at cmake.org
[mailto:cmake-bounces+muffmolch=gmx.de at cmake.org] Im Auftrag von Brandon Van
Every
Gesendet: Montag, 3. Dezember 2007 21:01
An: cmake at cmake.org
Betreff: Re: [CMake] SUBSTRING - STRING - shorten a string variable

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/a
ll_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
_______________________________________________
CMake mailing list
CMake at cmake.org
http://www.cmake.org/mailman/listinfo/cmake



More information about the CMake mailing list