1 /* -------------------------------------------------------------------------- *
2  *                            OpenSim:  About.cpp                             *
3  * -------------------------------------------------------------------------- *
4  * The OpenSim API is a toolkit for musculoskeletal modeling and simulation.  *
5  * See http://opensim.stanford.edu and the NOTICE file for more information.  *
6  * OpenSim is developed at Stanford University and supported by the US        *
7  * National Institutes of Health (U54 GM072970, R24 HD065690) and by DARPA    *
8  * through the Warrior Web program.                                           *
9  *                                                                            *
10  * Copyright (c) 2005-2017 Stanford University and the Authors                *
11  * Author(s): Ayman Habib                                                     *
12  *                                                                            *
13  * Licensed under the Apache License, Version 2.0 (the "License"); you may    *
14  * not use this file except in compliance with the License. You may obtain a  *
15  * copy of the License at http://www.apache.org/licenses/LICENSE-2.0.         *
16  *                                                                            *
17  * Unless required by applicable law or agreed to in writing, software        *
18  * distributed under the License is distributed on an "AS IS" BASIS,          *
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
20  * See the License for the specific language governing permissions and        *
21  * limitations under the License.                                             *
22  * -------------------------------------------------------------------------- */
23 
24 /**
25  * Define the standard SimTK compliant "version" and "about" routines.
26  */
27 
28 
29 #include "SDFastDLL.h"
30 
31 #include <string>
32 #include <cstring>
33 #include <cctype>
34 
35 
36 #define STR(var) #var
37 #define MAKE_VERSION_STRING(maj,min,build)  STR(maj.min.build)
38 #define MAKE_COPYRIGHT_STRING(y,a) \
39     "Copyright (c) " STR(y) " Stanford University, " STR(a)
40 #define MAKE_STRING(a) STR(a)
41 
42 #define GET_VERSION_STRING  \
43     MAKE_VERSION_STRING(OPENSIM_RDSDFAST_MAJOR_VERSION,  \
44                         OPENSIM_RDSDFAST_MINOR_VERSION,  \
45                         OPENSIM_RDSDFAST_BUILD_VERSION)
46 
47 #define GET_COPYRIGHT_STRING \
48     MAKE_COPYRIGHT_STRING(OPENSIM_RDSDFAST_COPYRIGHT_YEARS, \
49                           OPENSIM_RDSDFAST_AUTHORS)
50 
51 #define GET_AUTHORS_STRING \
52     MAKE_STRING(OPENSIM_RDSDFAST_AUTHORS)
53 
54 #define GET_LIBRARY_STRING \
55     MAKE_STRING(OPENSIM_RDSDFAST_LIBRARY_NAME)
56 
57 #define GET_TYPE_STRING \
58     MAKE_STRING(OPENSIM_RDSDFAST_TYPE)
59 
60 #ifndef NDEBUG
61     #define GET_DEBUG_STRING "debug"
62 #else
63     #define GET_DEBUG_STRING "release"
64 #endif
65 
66 extern "C" {
67 
opensim_version_SDFast(int * major,int * minor,int * build)68 void opensim_version_SDFast(int* major, int* minor, int* build) {
69     static const char* l = "OPENSIM library="   GET_LIBRARY_STRING;
70     static const char* t = "OPENSIM type="      GET_TYPE_STRING;
71     static const char* d = "OPENSIM debug="     GET_DEBUG_STRING;
72     static const char* v = "OPENSIM version="   GET_VERSION_STRING;
73     static const char* c = "OPENSIM copyright=" GET_COPYRIGHT_STRING;
74 
75     if (major) *major = OPENSIM_RDSDFAST_MAJOR_VERSION;
76     if (minor) *minor = OPENSIM_RDSDFAST_MINOR_VERSION;
77     if (build) *build = OPENSIM_RDSDFAST_BUILD_VERSION;
78 
79     // Force statics to be present in the binary (Release mode otherwise
80     // optimizes them away).
81     volatile int i=0;
82     if (i) { // never true, but compiler doesn't know ...
83         *major = *l + *t + *d + *v + *c;
84     }
85 }
86 
opensim_about_SDFast(const char * key,int maxlen,char * value)87 void opensim_about_SDFast(const char* key, int maxlen, char* value) {
88     if (maxlen <= 0 || value==0) return;
89     value[0] = '\0'; // in case we don't find a match
90     if (key==0) return;
91 
92     // downshift the key
93     std::string skey(key);
94     for (size_t i=0; i<skey.size(); ++i)
95         skey[i] = std::tolower(skey[i]);
96 
97     char* v = 0;
98     if      (skey == "version")   v = GET_VERSION_STRING;
99     else if (skey == "library")   v = GET_LIBRARY_STRING;
100     else if (skey == "type")      v = GET_TYPE_STRING;
101     else if (skey == "copyright") v = GET_COPYRIGHT_STRING;
102     else if (skey == "authors")   v = GET_AUTHORS_STRING;
103     else if (skey == "debug")     v = GET_DEBUG_STRING;
104 
105     if (v) {
106         std::strncpy(value,v,maxlen-1);
107         value[maxlen-1] = '\0'; // in case we ran out of room
108     }
109 }
110 
111 }
112