Mercurial > ~dholland > hg > ag > index.cgi
comparison anagram/guisupport/readprofile.cpp @ 0:13d2b8934445
Import AnaGram (near-)release tree into Mercurial.
author | David A. Holland |
---|---|
date | Sat, 22 Dec 2007 17:52:45 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:13d2b8934445 |
---|---|
1 /* | |
2 * AnaGram, A System for Syntax Directed Programming | |
3 * Copyright 1997-2002 Parsifal Software. All Rights Reserved. | |
4 * Copyright 2006 David A. Holland. All Rights Reserved. | |
5 * See the file COPYING for license and usage terms. | |
6 * | |
7 * readprofile.cpp | |
8 */ | |
9 | |
10 #include <stdio.h> | |
11 #include <stdlib.h> | |
12 #include <limits.h> | |
13 | |
14 #ifdef __IBMCPP__ | |
15 #include <iprofile.hpp> | |
16 #endif | |
17 | |
18 #include "port.h" | |
19 | |
20 #include "agstring.h" | |
21 #include "assert.h" | |
22 #include "readprofile.h" | |
23 #include "textfile.h" | |
24 | |
25 //#define INCLUDE_LOGGING | |
26 #include "log.h" | |
27 | |
28 | |
29 //////////////////////////////////////////////////////////// | |
30 | |
31 #ifdef AG_ON_WINDOWS | |
32 | |
33 /* | |
34 * Store and retrieve the profile from the Windows registry. | |
35 */ | |
36 | |
37 #ifdef __IBMCPP__ | |
38 | |
39 class AgProfile : public IProfile { | |
40 public: | |
41 AgProfile(); | |
42 void store(const char *key, AgString value); | |
43 void retrieve(const char *key, AgString &result); | |
44 }; | |
45 | |
46 AgProfile::AgProfile() | |
47 : IProfile("Parsifal Software") | |
48 { | |
49 setDefaultApplicationName("AnaGram"); | |
50 } | |
51 | |
52 void AgProfile::store(const char *key, AgString value) { | |
53 addOrReplaceElementWithKey(key, IString(value.pointer())); | |
54 } | |
55 | |
56 void AgProfile::retrieve(const char *key, AgString &result) { | |
57 try { result = (char *) elementWithKey(key); } | |
58 catch(...) {} | |
59 } | |
60 | |
61 void AgStoreProfile(const char *key, AgString value) { | |
62 LOGSECTION("AgStoreProfile"); | |
63 LOGV(key); | |
64 | |
65 AgProfile p; | |
66 LOGS("profile object created"); | |
67 | |
68 p.store(key, value); | |
69 LOGS("store completed"); | |
70 } | |
71 | |
72 AgString AgFetchProfile(const char *key) { | |
73 LOGSECTION("AgFetchProfile"); | |
74 LOGV(key); | |
75 | |
76 AgProfile p; | |
77 LOGS("profile object created"); | |
78 | |
79 AgString result; | |
80 p.retrieve(key, result); | |
81 LOGS("fetch completed"); | |
82 | |
83 LOGV(result); | |
84 return result; | |
85 } | |
86 | |
87 #endif /* __IBMCPP__ */ | |
88 | |
89 #endif /* AG_ON_WINDOWS */ | |
90 | |
91 //////////////////////////////////////////////////////////// | |
92 | |
93 #ifdef AG_ON_UNIX | |
94 | |
95 /* | |
96 * Store and retrieve the profile from ~/.AnaGram. | |
97 */ | |
98 | |
99 static void getdotfilepath(char *buf, size_t len) { | |
100 const char *home = getenv("HOME"); | |
101 if (!home) { | |
102 // use root directory | |
103 home = ""; | |
104 } | |
105 snprintf(buf, len, "%s/.AnaGram", home); | |
106 } | |
107 | |
108 void AgStoreProfile(const char *key, AgString value) { | |
109 LOGSECTION("AgStoreProfile"); | |
110 LOGV(key); | |
111 | |
112 assert(!strcmp(key, "initializationData")); | |
113 | |
114 char path[PATH_MAX]; | |
115 getdotfilepath(path, sizeof(path)); | |
116 LOGV(path); | |
117 | |
118 FILE *f = fopen(path, "w"); | |
119 if (!f) { | |
120 // give up | |
121 LOGS("fopen failed"); | |
122 return; | |
123 } | |
124 | |
125 fputs(value.pointer(), f); | |
126 fclose(f); | |
127 LOGS("complete"); | |
128 } | |
129 | |
130 AgString AgFetchProfile(const char *key) { | |
131 LOGSECTION("AgFetchProfile"); | |
132 LOGV(key); | |
133 | |
134 assert(!strcmp(key, "initializationData")); | |
135 | |
136 char path[PATH_MAX]; | |
137 getdotfilepath(path, sizeof(path)); | |
138 LOGV(path); | |
139 | |
140 text_file tf(path); | |
141 AgString result = tf.text; | |
142 | |
143 LOGV(result); | |
144 return result; | |
145 } | |
146 | |
147 #endif /* AG_ON_UNIX */ |