"Fossies" - the Fresh Open Source Software Archive 
Member "xosview-1.23/Xrm.cc" (11 Jul 2020, 8891 Bytes) of package /linux/misc/xosview-1.23.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "Xrm.cc" see the
Fossies "Dox" file reference documentation.
1 //
2 // Copyright (c) 1994, 1995, 2006, 2008 by Mike Romberg ( mike.romberg@noaa.gov )
3 //
4 // This file may be distributed under terms of the GPL
5 //
6
7 #include "Xrm.h"
8 #include "Xrmcommandline.h"
9 #include <string.h>
10 #include <stdlib.h>
11 #include <stdio.h> // For snprintf().
12 #include <ctype.h>
13 #include <unistd.h> // for access(), etc. BCG
14 #include <iostream>
15
16 extern char *defaultXResourceString;
17
18
19 bool Xrm::_initialized = false;
20
21 Xrm::Xrm(const char *instanceName, int argc, char **argv){
22 std::cerr << " Error: This constructor is not supported yet." << std::endl;
23 exit (-1);
24 _db = NULL;
25 _class = _instance = NULLQUARK;
26 getDisplayName(argc, argv);
27
28 (void) instanceName;
29 // End unsupported constructor. !!!!!!!! BCG
30 }
31
32 Xrm::Xrm(const char *className, const char *instanceName){
33 XrmInitialize ();
34
35 // Initialize everything to NULL.
36 _db = NULL;
37 _class = _instance = NULLQUARK;
38
39 // init the _instance and _class Quarks
40 _instance = XrmStringToQuark(instanceName);
41 initClassName(className);
42 }
43
44 const char*
45 Xrm::getDisplayName (int argc, char** argv)
46 {
47 (void) argc; // Avoid gcc warnings.
48 // See if '-display foo:0' is on the command line, and return it if it is.
49 char** argp;
50
51 for (argp = argv; (*argp != NULL) &&
52 (strncasecmp (*argp, "-display", 9)); argp++)
53 ; // Don't do anything.
54
55 // If we found -display and the next word exists...
56 if (*argp && *(++argp))
57 _display_name = *argp;
58 else
59 _display_name = "";
60 return _display_name;
61 // An empty display string means use the DISPLAY environment variable.
62 }
63
64 const char *Xrm::getResource(const char *rname) const{
65 char frn[1024], fcn[1024];
66 snprintf(frn, 1024, "%s.%s", instanceName(), rname);
67 snprintf(fcn, 1024, "%s.%s", className(), rname);
68
69 XrmValue val;
70 val.addr = NULL;
71 char *type;
72 XrmGetResource(_db, frn, fcn, &type, &val);
73 // This case here is a hack, because we are currently moving from
74 // always making the instance name be "xosview" to allowing
75 // user-specified ones. And unfortunately, the class name is
76 // XOsview, and not xosview, so our old defaults (xosview.font)
77 // will not be found when searching for XOsview.font. bgrayson Dec. 1996
78 if (!val.addr)
79 {
80 // Let's try with a non-uppercased class name.
81 char fcn_lower[1024];
82 strncpy(fcn_lower, className(), 1024);
83 char *p = fcn_lower;
84 while (p && *p)
85 {
86 *p = tolower(*p);
87 p++;
88 }
89 snprintf(fcn, 1024, "%s.%s", fcn_lower, rname);
90 XrmGetResource(_db, frn, fcn, &type, &val);
91 }
92
93 return val.addr;
94 }
95
96 Xrm::~Xrm(){
97 XrmDestroyDatabase(_db);
98 }
99
100 //---------------------------------------------------------------------
101 // This function uses XrmParseCommand, and updates argc and argv through it.
102 void Xrm::loadAndMergeResources(int& argc, char** argv, Display* display){
103
104 // init the database if it needs it
105 if (!_initialized){
106 XrmInitialize();
107 _initialized = true;
108 }
109 else
110 {
111 std::cerr << "Error: Xrm:loadAndMergeResources() called twice!" << std::endl;
112 exit (-1);
113 }
114 // This is ugly code. According to X and Xt rules, many files need
115 // to be checked for resource settings. Since we aren't (yet) using
116 // Xt or any other package, we need to do all of these checks
117 // individually. BCG
118 // =========== BEGIN X Resource lookup and merging ==========
119
120 // This all needs to be done in the proper order:
121 /*
122 Listed from weakest to strongest:
123 (from code-builtin-resources) (Stored in the string in defaultstring.h)
124 app-defaults
125 XOSView (from XAPPLRESDIR directory)
126 from RESOURCE_MANAGER property on server (reads .Xdefaults if needed)
127 from file specified in XENVIRONMENT
128 from command line (i.e., handled with XrmParseCommand)
129 */
130
131 // Get resources from the various resource files
132
133 // Put the default, compile-time options as the lowest priority.
134 _db = XrmGetStringDatabase (defaultXResourceString);
135
136 // Merge in the system resource database.
137 char rfilename[2048];
138 int result;
139 const int rlen = sizeof rfilename;
140
141 // Get the app-defaults
142 result = snprintf(rfilename, sizeof rfilename, "/etc/X11/app-defaults/%s",
143 XrmQuarkToString(_class));
144 if (result >= 0 && result < rlen)
145 XrmCombineFileDatabase (rfilename, &_db, 1);
146 result = snprintf(rfilename, sizeof rfilename, "/usr/lib/X11/app-defaults/%s",
147 XrmQuarkToString(_class));
148 if (result >= 0 && result < rlen)
149 XrmCombineFileDatabase (rfilename, &_db, 1);
150 result = snprintf(rfilename, sizeof rfilename, "/usr/X11R6/lib/X11/app-defaults/%s",
151 XrmQuarkToString(_class));
152 if (result >= 0 && result < rlen)
153 XrmCombineFileDatabase (rfilename, &_db, 1);
154 result = snprintf(rfilename, sizeof rfilename, "/usr/share/X11/app-defaults/%s",
155 XrmQuarkToString(_class));
156 if (result >= 0 && result < rlen)
157 XrmCombineFileDatabase (rfilename, &_db, 1);
158 // Try a few more, for SunOS/Solaris folks.
159 result = snprintf(rfilename, sizeof rfilename, "/usr/openwin/lib/X11/app-defaults/%s",
160 XrmQuarkToString(_class));
161 if (result >= 0 && result < rlen)
162 XrmCombineFileDatabase (rfilename, &_db, 1);
163 result = snprintf(rfilename, sizeof rfilename, "/usr/local/X11R6/lib/X11/app-defaults/%s",
164 XrmQuarkToString(_class));
165 if (result >= 0 && result < rlen)
166 XrmCombineFileDatabase (rfilename, &_db, 1);
167
168 // Now, check for an XOSView file in the XAPPLRESDIR directory...
169 char* xappdir = getenv ("XAPPLRESDIR");
170 if (xappdir != NULL)
171 {
172 char xappfile[1024];
173 snprintf (xappfile, 1024, "%s/%s", xappdir, className());
174 // this did not work for XAPPLRESDIR
175 //if (!access (xappfile, X_OK | R_OK))
176 if (!access (xappfile, R_OK))
177 {
178 XrmCombineFileDatabase (xappfile, &_db, 1);
179 }
180 }
181
182 // Now, check the display's RESOURCE_MANAGER property...
183 char* displayString = XResourceManagerString (display);
184 if (displayString != NULL)
185 {
186 XrmDatabase displayrdb = XrmGetStringDatabase (displayString);
187 XrmMergeDatabases (displayrdb, &_db); // Destroys displayrdb when done.
188 }
189
190 // And check this screen of the display...
191 char* screenString =
192 XScreenResourceString (DefaultScreenOfDisplay(display));
193 if (screenString != NULL)
194 {
195 XrmDatabase screenrdb = XrmGetStringDatabase (screenString);
196 XrmMergeDatabases (screenrdb, &_db); // Destroys screenrdb when done.
197 }
198
199 // Now, check for a user resource file, and merge it in if there is one...
200 if ( getenv( "HOME" ) != NULL ){
201 char userrfilename[1024];
202 char *home = getenv("HOME");
203 snprintf(userrfilename, 1024, "%s/.Xdefaults", home);
204 // User file overrides system (_db).
205 XrmCombineFileDatabase (userrfilename, &_db, 1);
206 }
207
208 // Second-to-last, parse any resource file specified in the
209 // environment variable XENVIRONMENT.
210 char* xenvfile;
211 if ((xenvfile = getenv ("XENVIRONMENT")) != NULL)
212 {
213 // The XENVIRONMENT file overrides all of the above.
214 XrmCombineFileDatabase (xenvfile, &_db, 1);
215 }
216 // Command-line resources override system and user defaults.
217 XrmDatabase cmdlineRdb_ = NULL;
218 XrmParseCommand (&cmdlineRdb_, options, NUM_OPTIONS, instanceName(),
219 &argc, argv);
220 XrmCombineDatabase (cmdlineRdb_, &_db, 1); // Keeps cmdlineRdb_ around.
221 // =========== END X Resource lookup and merging ==========
222 }
223
224 void Xrm::initClassName(const char* name){
225 char className[256];
226 strncpy(className, name, 255); // Avoid evil people out there...
227
228 className[0] = toupper(className[0]);
229 if (className[0] == 'X')
230 className[1] = toupper(className[1]);
231
232 _class = XrmStringToQuark(className);
233 }
234
235
236
237 //------------ Some debugging functions follow. -----------------------
238 inline std::ostream &operator<<(std::ostream &os, const XrmBinding &b){
239 switch (b){
240 case XrmBindTightly:
241 return os << ".";
242 case XrmBindLoosely:
243 return os << "*";
244 default:
245 std::cerr <<"std::ostream operator<<(std::ostream &, const XrmBinding &) : "
246 <<"Unknown XrmBinding!";
247 return os;
248 }
249
250 return os;
251 }
252
253 std::ostream &Xrm::dump(std::ostream &os) const {
254 os <<"--- Xrm --- class: " <<XrmQuarkToString(_class)
255 <<", instance: " <<XrmQuarkToString(_instance) <<"\n";
256
257 XrmName names[] = { _instance, NULLQUARK };
258 XrmClass classes[] = { _class, NULLQUARK };
259
260 XrmEnumerateDatabase(_db, names, classes, XrmEnumAllLevels, enumCB,
261 (XPointer)&os);
262
263 return os;
264 }
265
266 Bool Xrm::enumCB(XrmDatabase *, XrmBindingList bindings,
267 XrmQuarkList quarks, XrmRepresentation *type,
268 XrmValue *value, XPointer closure) {
269
270 std::ostream *os = (std::ostream *)closure;
271 (void) type; // Avoid gcc warnings.
272
273 //std::cerr <<"type = " <<XrmQuarkToString(*type) <<std::endl;
274
275 int i = 0;
276 while (quarks[i] != NULLQUARK){
277 *os <<bindings[i] <<XrmQuarkToString(quarks[i]);
278 i++;
279 }
280 *os <<": " <<value->addr <<"\n";
281
282 return False;
283 }