1 /*****************************************************************************/
2 /*                                                                           */
3 /*                 (C) Copyright 1997       Alberto Pasquale                 */
4 /*                 Portions (C) Copyright 1999 Per Lundberg                  */
5 /*                                                                           */
6 /*                   A L L   R I G H T S   R E S E R V E D                   */
7 /*                                                                           */
8 /*****************************************************************************/
9 /*                                                                           */
10 /*   How to contact the author:  Alberto Pasquale of 2:332/504@fidonet       */
11 /*                               Viale Verdi 106                             */
12 /*                               41100 Modena                                */
13 /*                               Italy                                       */
14 /*                                                                           */
15 /*****************************************************************************/
16 
17 #ifndef _APGENLB2_HPP_
18 #define _APGENLB2_HPP_
19 
20 #include <defines.h>
21 #include <typedefs.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <stdarg.h>
25 #include <time.h>
26 #include <limits.h>
27 
28 //#pragma pack (1)
29 
30                     // Implemented in CfgFile.Cpp
31 
32 // Class to handle a Configuration File
33 
34 // comments after ';'
35 // tabs converted to blanks, trailing space removed
36 // ; and " must be enclosed in quotes (quotes doubled)
37 // e.g. "example; he said ""what ?"""
38 // Lines got via fgetln are already "cleaned"
39 //
40 // Environment variables are expanded.
41 // The variable names must be at least two character long.
42 // A '%' followed by 2 or more characters (different from ' ' and '%')
43 // indicates the start of the variable name.
44 // A '%' or a ' ' indicates the end of the variable name.
45 // To represent the '%' character, "%%" must be used.
46 // % followed by ONE character is reserved for parameter substitutions.
47 
48 
49 #define CFGLINESIZE 512         // Size of configuration Line
50 
51 
52 class CfgFile {
53   private:
54     FILE *f;
55     char *lbuff, *token;
56     const char *cleanln;            // First char of cleaned line
57     const char *nextc;              // next char to scan
58     uint l;                         // line number
59     int lsize;                      // Line size, incl. term. null
60     BOOL gotln, gottkn;
61     void Init ();
62   public:
63     CfgFile (int lsize = CFGLINESIZE);
64     ~CfgFile ();
65     int Open (pcsz filename);       // 0 on success, -1 error, 1 already open
66     int Close ();                   // 0 on success
67     pcsz GetLn ();                  // First non-space of Full cleaned line,
68                                     // NULL on EOF
69     pcsz ReGetLn ();                // As previous GetLn; no effect on GetToken
70     pcsz GetToken ();               // 0 term. token, NULL if EOL
71                                     // If there is a string between commas, the
72                                     // entire string is got, quotes excluded.
73                                     // Can return empty string.
74     pcsz ReGetToken ();             // As previous GetToken
75     pcsz TokenPtr ();               // start of next word, NULL if EOL
76     pcsz RestOfLine ();             // start of next word, NullStr if EOL
77     uint LineN ();                  // Line number
78 };
79 
80 
81                     // Implemented in WriteLog.Cpp
82 
83 // Class to handle a Log File
84 
85 // By default the log file is kept open and flushed at each write
86 // first char of strfmt is "priority", as in logset;
87 // strfmt must NOT contain \r\n
88 
89 // If setup not done or fname is empty, no log will be written on *write.
90 
91 
92 #define LF_NoChange         0x0000
93 
94 #define LF_CloseAfterWrite  0x0001
95 #define LF_FlushOnRequest   0x0002
96 
97 #define LF_PrintPrty        0x0004      // for aux output (usually stdout)
98 #define LF_PrintDate        0x0008      //
99 #define LF_PrintTime        0x0010      //
100 #define LF_PrintID          0x0020      //
101 
102 #define LF_AllDefaults      0x8000      // To reset flags on 2nd setup
103 
104 
105 #define LF_LogSetSize       40
106 #define LF_DateSize         16
107 
108 
109 class LogFile {
110   private:
111     FILE *f;
112     char fname[PATH_MAX];
113     char ID[5];
114     char logset[LF_LogSetSize];
115     word flags;
116     int open ();            // open the log file, 0 on success
117     int PrintStart (FILE *f, char prty, pcsz date); // 0 on success
118   public:
119     LogFile ();
120     ~LogFile ();
121     void setup (        // can be used again, with open log
122                 pcsz fname,    // file name; NULL->don't change
123                 pcsz ID,       // 4 char application ID; NULL->don't change
124                 pcsz logset,   // messages to be logged, NULL->don't change
125                 word flags)    // LF_NoChange->don't change
126                ;
127     int close ();           // Make sure the log is closed, 0 on success
128     int flush ();           // Make sure the log is flushed, 0 on success
129     int va_write (psz date,         // external date buffer
130                   pcsz strfmt,      // format string
131                   va_list args)     // args
132                  ;                  // 0 on success
133     int vwrite (pcsz strfmt, ...);              // 0 on success
134     int vwrite (FILE *f,            // stream for additional output, can be NULL
135                 pcsz strfmt, ...)   // format string and args
136                ;                    // 0 on success
137 };
138 
139 
140                     // Implementation in Misc.Cpp
141 
142 #define SCANFT_BUFSIZE  256         // max length of scanft input
143 
144 int scanft (pcsz format, time_t timeout, ...);
145 // as scanf but with timeout (seconds); EOF on timeout
146 
147 int getcht (time_t timeout);
148 // as getch but with timeout (seconds); EOF on timeout
149 
150 
151 #endif
152 
153