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#include <string>
34
35#ifdef _WIN32
36# ifndef WIN32_LEAN_AND_MEAN
37# define WIN32_LEAN_AND_MEAN
38# endif
39# include <windows.h>
40#else
41# ifndef _GNU_SOURCE
42# define _GNU_SOURCE // dladdr requires _GNU_SOURCE to be defined
43# endif
44# include <dlfcn.h>
45# include <unistd.h>
46#endif
47
48namespace {
49 #ifdef _WIN32
50 extern "C" void *__ImageBase;
51 #endif
52}
53
54namespace MBXMLUtils {
55
61 public:
63#ifdef _WIN32
64 char moduleName[2048];
65 GetModuleFileName(reinterpret_cast<HMODULE>(&__ImageBase), moduleName, sizeof(moduleName));
66 p=moduleName;
67#else
68 char buffer[2048];
69 std::string curDir=getcwd(buffer, sizeof(buffer));
70
71 Dl_info info;
72 dladdr(reinterpret_cast<void*>(&dummyFunc), &info);
73 // convert to absolute path and return
74 std::string name(info.dli_fname);
75 p=name[0]=='/'?name:curDir+"/"+name;
76#endif
77 }
78 std::string operator()() { return p; }
79 private:
80 std::string p;
81#ifndef _WIN32
82 static void dummyFunc() {}
83#endif
84};
85
86}
87
88#endif
Definition: thislinelocation.h:60