Feature: 1501518
[csql.git] / src / server / Config.cxx
blobc81253886afe1f8e0f63e2b19f04f6ca2b7f9e20
1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 ***************************************************************************/
16 #include<Config.h>
17 #include<ErrorType.h>
18 #include<Debug.h>
20 int Config::readLine(FILE *fp, char * buffer)
22 char c =0;
23 int count =0;
24 while ( true)
26 c = fgetc(fp);
27 if (c == '\n') break;
28 if (c == EOF) return EOF;
29 buffer[count++] = c;
31 return count;
33 int Config::storeKeyVal(char *key, char *value)
35 if (strcasestr(key, "PAGE_SIZE") != NULL )
36 { cVal.pageSize = atoi(value); }
37 else if (strcasestr(key, "MAX_TRANS") != NULL)
38 { cVal.maxTrans = atoi(value); }
39 else if (strcasestr(key, "MAX_PROCS") != NULL)
40 { cVal.maxProcs = atoi(value); }
41 else if (strcasestr(key, "MAX_THREADS") != NULL)
42 { cVal.maxThreads = atoi(value); }
43 else if (strcasestr(key, "MAX_SYS_DB_SIZE") != NULL)
44 { cVal.maxSysSize = atol(value); }
45 else if (strcasestr(key, "MAX_DB_SIZE") != NULL)
46 { cVal.maxDbSize = atol(value); }
47 else if (strcasestr(key, "SYS_DB_KEY") != NULL)
48 { cVal.sysDbKey = atoi(value); }
49 else if (strcasestr(key, "USER_DB_KEY") != NULL)
50 { cVal.userDbKey = atoi(value); }
51 else if (strcasestr(key, "LOG_FILE") != NULL)
52 { strcpy(cVal.logFile , value); }
53 else if (strcasestr(key, "MAP_ADDRESS") != NULL)
54 { cVal.mapAddr = atol(value); }
55 else if (strcasestr(key, "MUTEX_TIMEOUT_SECS") != NULL)
56 { cVal.mutexSecs = atoi(value); }
57 else if (strcasestr(key, "MUTEX_TIMEOUT_USECS") != NULL)
58 { cVal.mutexUSecs = atoi(value); }
59 else return 1;
60 return 0;
62 int Config::validateValues()
64 if (cVal.pageSize < 1024 || cVal.pageSize > 1024 * 1024 * 10 )
66 printError(ErrBadArg, "PAGE_SIZE should be >= 1024 and <= 10 MB");
67 return 1;
69 if (cVal.maxTrans < 10 || cVal.maxTrans > 8192)
71 printError(ErrBadArg, "MAX_TRANS should be >= 10 and <= 8192");
72 return 1;
74 if (cVal.maxProcs < 10 || cVal.maxProcs > 8192)
76 printError(ErrBadArg, "MAX_PROCS should be >= 10 and <= 8192");
77 return 1;
79 if (cVal.maxThreads < 1 || cVal.maxThreads > 64)
81 printError(ErrBadArg, "MAX_THREADS should be >= 1 and <= 64");
82 return 1;
84 if (cVal.maxSysSize < 1024 * 1024 || cVal.maxSysSize > 1024 *1024 *1024)
86 printError(ErrBadArg, "MAX_SYS_DB_SIZE should be >= 1 MB and <= 1 GB");
87 return 1;
89 if (cVal.maxSysSize % 8192 !=0 )
91 printError(ErrBadArg, "MAX_SYS_DB_SIZE should be multiples of 8192");
92 return 1;
95 if (cVal.maxDbSize < 1024 * 1024 || cVal.maxDbSize > 1024 *1024 *1024)
97 printError(ErrBadArg, "MAX_DB_SIZE should be >= 1 MB and <= 1 GB");
98 return 1;
100 if (cVal.maxDbSize % 8192 !=0)
102 printError(ErrBadArg, "MAX_DB_SIZE should be multiples of 8192");
103 return 1;
106 if (cVal.sysDbKey < 10 || cVal.sysDbKey > 8192)
108 printError(ErrBadArg, "SYS_DB_KEY should be >= 10 and <= 8192");
109 return 1;
111 if (cVal.userDbKey < 10 || cVal.userDbKey > 8192)
113 printError(ErrBadArg, "USER_DB_KEY should be >= 10 and <= 8192");
114 return 1;
116 if ( cVal.sysDbKey == cVal.userDbKey)
118 printError(ErrBadArg, "USER_DB_KEY and SYS_DB_KEY have same value %d", cVal.userDbKey);
119 return 1;
121 if (0 == strcmp(cVal.logFile,""))
123 printError(ErrBadArg, "LOG_FILE is set to NULL");
124 return 1;
126 //TODO::Check for the existence of the log file
127 if (cVal.mapAddr < 400000000 || cVal.mapAddr > 4000000000)
129 printError(ErrBadArg, "MAP_ADDRESS should be >= 400000000 and <= 4000000000");
130 return 1;
132 if (cVal.mutexSecs < 0 || cVal.mutexSecs > 360)
134 printError(ErrBadArg, "MUTEX_TIMEOUT_SECS should be >= 0 and <= 360");
135 return 1;
137 if (cVal.mutexUSecs < 0 || cVal.mutexUSecs > 1000000)
139 printError(ErrBadArg, "MUTEX_TIMEOUT_USECS should be >= 0 and <= 1000000");
140 return 1;
142 return 0;
145 int Config::readAllValues(char *fileName)
147 FILE *fp;
149 fp = fopen(fileName,"r");
150 if( fp == NULL ) {
151 printError(ErrSysInit, "Invalid path/filename in CSQL_CONFIG_FILE.");
152 return 1;
155 int hasData = 1;
156 char buffer[1024];
157 char key[1024];
158 char value[1024];
159 while (hasData)
161 memset(buffer, 0, 1024);
162 //int ret = fscanf(fp,"%s\r",buffer);
163 int ret = readLine(fp, buffer);
164 if (ret == EOF) break;
165 bool isComment= false;
166 int posEqual =0;
167 for (int i = 0; i <1024; i++)
169 if (buffer[i] == '=' ) posEqual=i;
170 if (buffer[i] == '#' ) { isComment = true; break; }
171 if (buffer[i] == '\n') { break; }
172 if (buffer[i] == '\0') { break; }
174 if (isComment) continue;
175 if (!posEqual) continue;
176 strncpy(key, buffer, posEqual);
177 key[posEqual] = '\0';
178 posEqual++;
179 strcpy(value, &buffer[posEqual]);
180 storeKeyVal(key, value);
182 fclose(fp);
183 if (validateValues())
185 return 1;
188 return 0;