/[pearpc]/src/tools/configfile.cc
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /src/tools/configfile.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 11 - (show annotations)
Thu Sep 6 16:48:55 2007 UTC (11 years, 10 months ago) by dpavlin
File size: 5330 byte(s)
more forgotten files
1 /*
2 * HT Editor
3 * configfile.cc
4 *
5 * Copyright (C) 1999-2002 Stefan Weyergraf
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include <cstdlib>
22 #include <cstring>
23
24 // FIXME: needed for gAppFilename
25 #include "system/sys.h"
26
27 #include "cstream.h"
28 #include "configfile.h"
29 #include "tools/debug.h"
30 #include "tools/endianess.h"
31 #include "tools/except.h"
32 #include "tools/strtools.h"
33
34 /* VERSION 2 (for ht-0.4.4 and later) */
35
36 /* NOTE: as of Version 2 ALL integers in HT config-files are
37 stored in big-endian (network-order, non-intel) format... */
38
39 struct ConfigFileHeader {
40 char magic[4] PACKED;
41 char version[4] PACKED;
42 char type[2] PACKED;
43 };
44
45 ObjectStream *createObjectStream(File *file, bool own_file, ConfigFileType type)
46 {
47 switch (type) {
48 case CONFIGFILE_BIN: return new ObjectStreamBin(file, own_file);
49 case CONFIGFILE_TEXT: return new ObjectStreamText(file, own_file);
50 case CONFIGFILE_BIN_COMPRESSED: {
51 CompressedStream *cs = new CompressedStream(file, own_file);
52 return new ObjectStreamBin(cs, true);
53 }
54 default: return NULL;
55 }
56 }
57
58 static ObjectStream *createReadConfigFile(File *rfile, bool own_rfile, char magic[4], uint32 version)
59 {
60 ConfigFileHeader header;
61 String filedesc;
62 rfile->getDesc(filedesc);
63 rfile->readx(&header, sizeof header);
64
65 // magic
66 if (memcmp(magic, header.magic, 4) != 0) throw MsgfException(
67 "Invalid config-file magic in '%y' (found '%c%c%c%c', expected '%c%c%c%c')",
68 &filedesc, header.magic[0], header.magic[1], header.magic[2],
69 header.magic[3], magic[0], magic[1], magic[2], magic[3]);
70
71 // version
72 uint16 read_version;
73 if (!hexw_ex(read_version, header.version)) throw MsgfException(
74 "Corrupted config-file header in'%y' (invalid version '%c%c%c%c', must be lowercase-hex)",
75 &filedesc, header.version[0], header.version[1], header.version[2], header.version[3]);
76 if (read_version != version) {
77 char *rela;
78 char *hint;
79 if (read_version < version) {
80 rela = "old";
81 hint = "The file has probably been left over by an older version of the program. You might want to delete it.";
82 } else {
83 rela = "NEW";
84 hint = "You're probably using an old version of the program. You might want to update.";
85 }
86 throw MsgfException(
87 "Config-file is too %s in '%y' (found version %d/0x%x, expected version %d/0x%x).\n%s",
88 rela, &filedesc, read_version, read_version, version, version, hint);
89 }
90
91 // type
92 uint8 read_type;
93 if (!hexb_ex(read_type, header.type)) throw MsgfException(
94 "Corrupted config-file header in'%y' (invalid type '%c%c', must be lowercase-hex)",
95 &filedesc, header.type[0], header.type[1]);
96
97 ObjectStream *o = createObjectStream(rfile, own_rfile, (ConfigFileType)read_type);
98 if (!o) throw MsgfException(
99 "Corrupted config-file in '%y' (invalid type %d/0x%x)",
100 &filedesc, read_type, read_type);
101
102 return o;
103 }
104
105 static ObjectStream *createWriteConfigFile(File *wfile, bool own_wfile, char magic[4], uint32 version, ConfigFileType type)
106 {
107 ObjectStream *o = createObjectStream(wfile, own_wfile, type);
108 ConfigFileHeader header;
109 char scratch[16];
110 // magic
111 memcpy(header.magic, magic, 4);
112
113 // version
114 sprintf(scratch, "%04x", version);
115 memcpy(header.version, scratch, 4);
116
117 // type
118 sprintf(scratch, "%02x", type);
119 memcpy(header.type, scratch, 2);
120
121 o->writex(&header, sizeof header);
122
123 if (type == CONFIGFILE_TEXT) {
124 o->writex((void*)"\n#\n#\tThis is a generated file!\n#\n", 33);
125 }
126
127 return o;
128 }
129
130 ConfigFileObjectStream::ConfigFileObjectStream(File *readable_file, bool own_rfile, char magic[4], uint32 version)
131 : ObjectStreamLayer(createReadConfigFile(readable_file, own_rfile, magic, version), true)
132 {
133 ASSERT(readable_file->getAccessMode() & IOAM_READ);
134 }
135
136 ConfigFileObjectStream::ConfigFileObjectStream(File *writable_file, bool own_wfile, char magic[4], uint32 version, ConfigFileType type)
137 : ObjectStreamLayer(createWriteConfigFile(writable_file, own_wfile, magic, version, type), true)
138 {
139 ASSERT(writable_file->getAccessMode() & IOAM_WRITE);
140 }
141
142 /*
143 * INIT
144 */
145
146 char *systemconfig_file;
147
148 bool initConfigFile()
149 {
150 #if defined(MSDOS) || defined(DJGPP) || defined(WIN32) || defined(__WIN32__)
151 #define SYSTEM_CONFIG_FILE_NAME "ht.cfg"
152 char d[1024]; /* FIXME: !!!! */
153 sys_dirname(d, gAppFilename);
154 char *b = "\\"SYSTEM_CONFIG_FILE_NAME;
155 systemconfig_file = (char*)malloc(strlen(d)+strlen(b)+1);
156 strcpy(systemconfig_file, d);
157 strcat(systemconfig_file, b);
158 #else
159 #define SYSTEM_CONFIG_FILE_NAME ".htcfg"
160 char *home = getenv("HOME");
161 char *b = "/"SYSTEM_CONFIG_FILE_NAME;
162 if (!home) home = "";
163 systemconfig_file = (char*)malloc(strlen(home)+strlen(b)+1);
164 strcpy(systemconfig_file, home);
165 strcat(systemconfig_file, b);
166 #endif
167 return true;
168 }
169
170 /*
171 * DONE
172 */
173
174 void doneConfigFile()
175 {
176 free(systemconfig_file);
177 }

  ViewVC Help
Powered by ViewVC 1.1.26