1 #include <stdarg.h>
2 
3 class CLog
4 {
5 	int inited;
6 	char LogFileName[256];
7 	FILE *flog;
8 public:
9 	CLog(void);
10 	int SetLogFileName (char *FileName);
11 	int OpenLog (void);
12 	int WriteOnLog (char *string,...);
13 	int CloseLog (void);
14 };
15 
CLog(void)16 CLog::CLog (void)
17 {
18 	inited=0;
19 }
20 
OpenLog(void)21 int CLog::OpenLog (void)
22 {
23         //DateTime dt;
24 	char buffer[256];
25 	if (inited)
26 		return ELH_ALREADYOPEN;
27 	flog=fopen (LogFileName,"at");
28 	if (flog==NULL)
29 		return ELH_CANTOPEN;
30 	inited=1;
31 /*
32         dt.getCurrentTime();
33         sprintf (buffer,
34                  "%s started at %s %02d,%04d (%s) %02u:%02u:%02u\n",
35                  IDENTString,Months[dt.month-1],dt.day,dt.year,
36                  Days[GetDOW()],dt.hours,dt.minutes,dt.seconds);
37         WriteOnLog ("%s",buffer);
38 	WriteOnLog ("------------------------------------------------------\n");
39 */
40 	return SUCCESS;
41 }
42 
CloseLog(void)43 int CLog::CloseLog (void)
44 {
45 	if (inited)
46 		fclose (flog);
47 	return SUCCESS;
48 }
WriteOnLog(char * string,...)49 int CLog::WriteOnLog (char *string,...)
50 {
51 	char *expand;
52 	va_list vap;
53 	expand=(char *) malloc (640); // 640 bytes should be enough for everyone :-)
54 	if (inited==0 || expand==NULL)
55 		return ELH_NOTOPEN;
56 	va_start (vap,string);
57 	vsprintf (expand,string,vap);
58 	fputs (expand,flog);
59 #ifdef DEBUG
60 	fputs (expand,stdout);
61 #endif
62 	free (expand);
63 	va_end (vap);
64 	return SUCCESS;
65 }
66 
SetLogFileName(char * FileName)67 int CLog::SetLogFileName (char *FileName)
68 {
69 	if (inited)
70 		return ELH_ALREADYOPEN;
71 	strcpy (LogFileName,FileName);
72 	return SUCCESS;
73 }
74 
75 // This small classes handles a string list, to buffer log writes
76 class C_StringList
77 {
78 protected:
79 	char **Seed;
80 	int StringCount;
81 public:
82 	C_StringList (void);
83 	~C_StringList (void);
84 	int AddString (char *NewString);
85 	int InsertString (char *NewString,int pos);
86 	void DestroyList (void);
87 	char *GetString (int number);
88 	int GetStringCount (void);
89 	void RemoveFirst (void);
90 	void RemoveLast (void);
91 	int IsString (char *string, int *storage);
92 };
93 
IsString(char * string,int * storage)94 int C_StringList::IsString (char *string,int *storage)
95 {
96 	int count;
97 	for (count=0;count<StringCount;count++)
98                 if (!cistrcmp (Seed[count],string))
99 		{
100 			if (storage!=NULL)
101 				*storage=count;
102 			return 1;
103 		}
104 	return 0;
105 }
106 
RemoveLast(void)107 void C_StringList::RemoveLast (void)
108 {
109 	if (StringCount>0)
110 	{
111 		StringCount--;
112 		Seed=(char **) realloc (Seed,StringCount*sizeof (char *));
113 	}
114 }
115 
RemoveFirst(void)116 void C_StringList::RemoveFirst (void)
117 {
118 	if (StringCount>0)
119 	{
120 		StringCount--;
121 		memcpy (Seed,Seed+1,StringCount*sizeof (char *));
122 		Seed=(char **) realloc (Seed,StringCount*sizeof (char *));
123 	}
124 }
125 
GetStringCount(void)126 int C_StringList::GetStringCount (void)
127 {
128 	return (StringCount);
129 }
130 
C_StringList(void)131 C_StringList::C_StringList (void)
132 {
133 	StringCount=0;
134 	Seed=NULL;
135 }
136 
DestroyList(void)137 void C_StringList::DestroyList (void)
138 {
139 	int count;
140 	if (StringCount>0)
141 	{
142 		for (count=0;count<StringCount;count++)
143 			free (Seed[count]);
144 		free (Seed);
145 		StringCount=0;
146 		Seed=NULL;
147 	}
148 }
149 
~C_StringList(void)150 C_StringList::~C_StringList (void)
151 {
152 	DestroyList ();
153 }
154 
InsertString(char * NewString,int pos)155 int C_StringList::InsertString (char *NewString,int pos)
156 {
157 	char *NewItem;
158 	int tomove;
159 	NewItem=(char *) malloc (strlen (NewString)+1);
160 	if (NewItem==NULL)
161 		return NOMEMORY;
162 	strcpy (NewItem,NewString);
163 	Seed=(char **) realloc (Seed,(1+StringCount)*(sizeof (char *)));
164 	if (Seed==NULL)
165 		return NOMEMORY;
166 	if (pos>StringCount)
167 		pos=StringCount;
168 	tomove=StringCount-pos;
169 	if (pos<StringCount)
170 		memmove (Seed+pos+1,Seed+pos,tomove*sizeof (char *));
171 	Seed[pos]=NewItem;
172 	StringCount++;
173 	return SUCCESS;
174 }
175 
AddString(char * NewString)176 int C_StringList::AddString (char *NewString)
177 {
178 	char *NewItem;
179 	NewItem=(char *) malloc (strlen (NewString)+1);
180 	if (NewItem==NULL)
181 		return NOMEMORY;
182 	strcpy (NewItem,NewString);
183 	Seed=(char **) realloc (Seed,(1+StringCount)*(sizeof (char *)));
184 	if (Seed==NULL)
185 		return NOMEMORY;
186 	Seed[StringCount]=NewItem;
187 	StringCount++;
188 	return SUCCESS;
189 }
190 
GetString(int number)191 char *C_StringList::GetString (int number)
192 {
193 	if (number>=StringCount)
194 		return NULL;
195     return (Seed[number]);
196 }
197 
198 // A stringlist class with a sort function
199 class C_SpecialStringList: public C_StringList
200 {
201 	public:
202 		void Sort (void);
203 		int AddFileName (char *NewString);
204 };
205 
AddFileName(char * NewString)206 int C_SpecialStringList::AddFileName (char *NewString)
207 {
208 	char *help;
209         size_t l = strlen(NewString);
210 	help=NewString;
211 	while (*help)
212 	{
213 		*help=toupper (*help);
214 		help++;
215 	}
216         if (l < 5 ||
217             toupper(NewString[l-1] != 'G') ||
218             toupper(NewString[l-2] != 'S') ||
219             toupper(NewString[l-3] != 'M') ||
220             NewString[l-4] != '.')
221 	{
222 #ifdef DEBUG
223 		printf ("C_SpecialStringList:: "
224                         "Rejected string - file extension not .MSG\n");
225 #endif
226 		return ESH_INVALID;
227 	}
228 	if (strchr (NewString,DIRSEPC)==NULL)
229 	{
230 #ifdef DEBUG
231 		printf ("C_SpecialStringList:: Rejected string - "
232                         "character "DIRSEPS" not found\n");
233 #endif
234 		return ESH_INVALID;
235 	}
236 	return (AddString (NewString));
237 }
238 
239 int
240 #ifdef VAC
241 _LNK_CONV
242 #endif
SortStuff(void const * name1,void const * name2)243 SortStuff (void const *name1,void const *name2)
244 {
245 	char *namestart;
246 	char assist[256],*search;
247 	char dir1[256],dir2[256];
248         size_t l;
249 	int number1,number2,res;
250 
251         l = strlen(*(const char**)name1);
252         memcpy ((char *) assist,*(char **)name1, l);
253         assist[l - 4] = '\0'; /* cut the ".msg" extension */
254 	namestart=assist + l - 4;
255 	while (*namestart!=DIRSEPC)
256 		namestart--;
257 	*namestart=0;
258 	strcpy (dir1,assist);
259 	number1=atoi (namestart+1);
260 
261 	l = strlen(*(const char**)name2);
262         memcpy ((char *) assist, *(char**)name2, l);
263         assist[l - 4] = '\0'; /* cut the ".msg" extension */
264         namestart = assist + l - 4;
265 	while (*namestart!=DIRSEPC)
266 		namestart--;
267 	*namestart=0;
268 	strcpy (dir2,assist);
269 	number2=atoi (namestart+1);
270 	if ((res=strcmp (dir1,dir2))==0)
271 		return (number1-number2);
272 	return res;
273 };
274 
Sort(void)275 void C_SpecialStringList::Sort (void)
276 {
277 #ifdef OS_2
278         qsort (Seed,StringCount,sizeof (char *),&SortStuff);
279 #else
280         qsort (Seed,StringCount,sizeof (char *),SortStuff);
281 #endif
282 }
283