mbxmlutils  1.3.0
Multi-Body XML Utils
thislinelocation.h
1 /*
2  * Author: Markus Friedrich
3  *
4  * This file is free and unencumbered software released into the public domain.
5  *
6  * Anyone is free to copy, modify, publish, use, compile, sell, or
7  * distribute this software, either in source code form or as a compiled
8  * binary, for any purpose, commercial or non-commercial, and by any
9  * means.
10  *
11  * In jurisdictions that recognize copyright laws, the author or authors
12  * of this software dedicate any and all copyright interest in the
13  * software to the public domain. We make this dedication for the benefit
14  * of the public at large and to the detriment of our heirs and
15  * successors. We intend this dedication to be an overt act of
16  * relinquishment in perpetuity of all present and future rights to this
17  * software under copyright law.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  *
27  * For more information, please refer to <http://unlicense.org/>
28  */
29 
30 #ifndef _MBXMLUTILSHELPER_THISLINELOCATION_H_
31 #define _MBXMLUTILSHELPER_THISLINELOCATION_H_
32 
33 #ifdef _WIN32
34 # include <windows.h>
35 #else
36 # ifndef _GNU_SOURCE
37 # define _GNU_SOURCE // dladdr requires _GNU_SOURCE to be defined
38 # endif
39 # include <dlfcn.h>
40 # include <unistd.h>
41 #endif
42 
43 namespace {
44  #ifdef _WIN32
45  extern "C" void *__ImageBase;
46  #endif
47 }
48 
49 namespace MBXMLUtils {
50 
56  public:
58 #ifdef _WIN32
59  char moduleName[2048];
60  GetModuleFileName(reinterpret_cast<HMODULE>(&__ImageBase), moduleName, sizeof(moduleName));
61  p=moduleName;
62 #else
63  char buffer[2048];
64  std::string curDir=getcwd(buffer, sizeof(buffer));
65 
66  Dl_info info;
67  dladdr(reinterpret_cast<void*>(&dummyFunc), &info);
68  // convert to absolute path and return
69  std::string name(info.dli_fname);
70  p=name[0]=='/'?name:curDir+"/"+name;
71 #endif
72  }
73  std::string operator()() { return p; }
74  private:
75  std::string p;
76 #ifndef _WIN32
77  static void dummyFunc() {}
78 #endif
79 };
80 
81 }
82 
83 #endif
Definition: thislinelocation.h:55