-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.h
157 lines (126 loc) · 4.65 KB
/
utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#ifndef __UTILS_H__
#define __UTILS_H__
#ifdef __cplusplus
extern "C" {
#endif
#include <fcntl.h>
#include <stdarg.h>
#include <sys/types.h>
#define R_OK 4 /* Test for read permission. */
#define W_OK 2 /* Test for write permission. */
#define X_OK 1 /* Test for execute permission. */
#define F_OK 0 /* Test for existence. */
#define CFG_FLOCK_BSD 0 //enable flock or not
#define CFG_FLOCK_POSIX 0 //enable flock or not, using posix standard
#define CFG_FMagic 0 //check file Magic
#define CFG_MyFileLock 0 //my file lock for the whole /mnt/mtd/*.conf
#define CFG_MAX_LINE_LENGTH 1024
#define CFG_ThreadLock 0 // 0:no lock, 1:lock
#define NULL_STR '\0'
#define NULL_VAL 0xFFFF
#define MAX_FILE_NAME 128
#if (CFG_MyFileLock==1)
typedef struct
{
char LockFlag;
char FileName[MAX_FILE_NAME];
}MyFileLock;
#define My_RDLCK 1
#define My_WRLCK 2
#define My_UNLCK 0
#define MyMaxCfgFile 32
#endif
/* configuration file entry */
typedef struct TCFGENTRY
{
char *section;
char *id;
char *value;
char *comment;
unsigned short flags;
}
TCFGENTRY, *PCFGENTRY;
/* values for flags */
#define CFE_MUST_FREE_SECTION 0x8000
#define CFE_MUST_FREE_ID 0x4000
#define CFE_MUST_FREE_VALUE 0x2000
#define CFE_MUST_FREE_COMMENT 0x1000
/* configuration file */
typedef struct TCFGDATA
{
char *fileName; // Current file name
int fd; //file handler
int dirty; // Did we make modifications?
char *image; // In-memory copy of the file
size_t size; // Size of this copy (excl. \0)
time_t mtime; // Modification time
unsigned int numEntries;
unsigned int maxEntries;
PCFGENTRY entries;
/* Compatibility */
unsigned int cursor;
char *section;
char *id;
char *value;
char *comment;
unsigned short flags;
}
TCONFIG, *PCONFIG;
#define CFG_VALID 0x8000
#define CFG_EOF 0x4000
#define CFG_ERROR 0x0000
#define CFG_SECTION 0x0001
#define CFG_DEFINE 0x0002
#define CFG_CONTINUE 0x0003
#define CFG_TYPEMASK 0x000F
#define CFG_TYPE(X) ((X) & CFG_TYPEMASK)
#define cfg_valid(X) ((X) != NULL && ((X)->flags & CFG_VALID))
#define cfg_eof(X) ((X)->flags & CFG_EOF)
#define cfg_section(X) (CFG_TYPE((X)->flags) == CFG_SECTION)
#define cfg_define(X) (CFG_TYPE((X)->flags) == CFG_DEFINE)
#define cfg_cont(X) (CFG_TYPE((X)->flags) == CFG_CONTINUE)
char * remove_quotes(const char *szString);
int cfg_init (PCONFIG * ppconf, const char *filename, int doCreate);
int cfg_done (PCONFIG pconfig);
int cfg_freeimage (PCONFIG pconfig);
int cfg_refresh (PCONFIG pconfig);
int cfg_storeentry (PCONFIG pconfig, char *section, char *id,char *value, char *comment, int dynamic);
int cfg_rewind (PCONFIG pconfig);
int cfg_nextentry (PCONFIG pconfig);
int cfg_find (PCONFIG pconfig, char *section, char *id);
int cfg_next_section (PCONFIG pconfig);
int cfg_write (PCONFIG pconfig, char *section, char *id, char *value);
int cfg_writelong ( PCONFIG pconfig,char *section,char *id,long value);
int cfg_writeint( PCONFIG pconfig,char *section,char *id,int value);
int cfg_writeshort ( PCONFIG pconfig,char *section,char *id,unsigned short value);
int cfg_writebyte ( PCONFIG pconfig,char *section,char *id,unsigned char value);
int cfg_commit (PCONFIG pconfig);
int cfg_copy(PCONFIG pconfig,char *newfilename);
int cfg_getstring(PCONFIG pconfig, char *section, char *id, char *defval, char *valptr);
int cfg_getlong (PCONFIG pconfig, char *section, char *id, long *valptr);
int cfg_getint (PCONFIG pconfig, char *section, char *id, int *valptr);
int cfg_getshort (PCONFIG pconfig, char *section, char *id, unsigned short *valptr);
int cfg_getbyte(PCONFIG pconfig, char *section, char *id, unsigned char *valptr);
int cfg_get_item (PCONFIG pconfig, char *section, char *id, char * fmt, ...);
int cfg_write_item(PCONFIG pconfig, char *section, char *id, char * fmt, ...);
int list_entries (PCONFIG pCfg, const char * lpszSection, char * lpszRetBuffer, int cbRetBuffer);
int list_sections (PCONFIG pCfg, char * lpszRetBuffer, int cbRetBuffer);
//APIs//decimal number(not 0xHHHH)
int Cfg_ReadStr(char *File,char *Section,char *Entry,char *valptr);
int Cfg_ReadInt(char *File,char *Section,char *Entry,int defval,int *valptr);
int Cfg_ReadShort(char *File,char *Section,char *Entry,unsigned short *valptr);
int Cfg_WriteInt(char *File,char *Section,char *Entry,int Value);
int Cfg_WriteHexInt(char *File,char *Section,char *Entry,int Value);
int Cfg_WriteStr(char *File,char *Section,char *Entry,char *valptr);
#define MIN(a,b) ((a>b)?b:a)
#define KB 1024
#define MB (KB*KB)
#define LOG_FILE_LIMIT (5*MB)
#define LOG_FILE_DIR "/tmp/"
#define LOG_LINE_LIMIT 1000
int Log_MsgLine(char *LogFileName,char *sLine);
int util_kill(int pid);
#ifdef __cplusplus
}
#endif
#endif