📄 cfgfiles.cpp
字号:
/*
+-----------------------------------------------------------------------------+
| |
| cfgfiles.cpp |
| Last change: yyyy-mm-dd |
| |
| Author: Jan Krumsiek |
| eMail: proxy@krumsiek.com |
| Web: http://www.krumsiek.com/proxy |
| |
| Copyright 2003 - Jan Krumsiek |
| This source code can freely be modified and redistributed. No liability |
| whatsoever is taken by the author for any use of this software. |
| |
| Description: |
| This piece of code handles the opening of config files and automatic |
| extraction of all commands and their parameters. A command has the format |
| command[space]para1[comma]para2[comma]... |
| |
+-----------------------------------------------------------------------------+
*/
#include "proxymain.h"
#include "filesystem.h"
#include "cfgfiles.h"
#include "string.h"
// help variables
CHARPTR filecont=0,fileend=0;
CHARPTR filepos=0;
UINT linecnt=0;
CHARPTR filename;
// Prototypes
void GetNextLine(CHARPTR source, CHARPTR end, CHARPTR* linestart, CHARPTR* lineend);
void GetCommand(CHARPTR source, CHARPTR end, CHARPTR* cmdstart, CHARPTR* cmdend);
CHARPTR* GetParameters(CHARPTR source, CHARPTR end, UINT* number);
COMMAND GetFullCommand(CHARPTR source, CHARPTR end);
int OpenConfigFile(CHARPTR file) // exported in cfgfiles.h
// opens the config file and saves contents
{
UINT filelen;
// call GetFileCont, defined in proxymain.cpp
GetFileCont(file,&filecont,&filelen);
// error?
if (filecont == 0) return 0;
// let fileend point to the last byte of file contents
fileend = filecont + filelen - 2;
// current position in file = beginning
filepos = filecont;
// initialize line counter
linecnt = 0;
filename = (CHARPTR)CopyAndPtr(file,strlen(file));
return 1;
}
void ReleaseConfigFile() // exported in cfgfiles.h
// releases memory allocated for current config file
{
delete filename;
delete filecont;
}
COMMAND GetNextFullCommand() // exported in cfgfiles.h
// reads next line and extracts command and parameters (COMMAND struct)
{
CHARPTR linestart=0,lineend=0;
// Finished?
if (filepos >= fileend)
{
// Indicate end by returning a COMMAND struct where command = 0
COMMAND vreturn;
vreturn.command = 0;
return vreturn;
}
// Get next line
GetNextLine(filepos,fileend,&linestart,&lineend);
// if linestart == 0 -> no new command was found
if (linestart == 0)
{
// Indicate end by returning a COMMAND struct where command = 0
COMMAND vreturn;
vreturn.command = 0;
return vreturn;
}
// Increase filepos
filepos = lineend + 2;
// Now extract command from line and return it
return GetFullCommand(linestart,lineend);
}
void ReleaseCommand(COMMAND* cmd) // exported in cfgfiles.h
// Dellocates all memory that has been allocated for a COMMAND struct
{
UINT i;
// Delete command first
delete cmd->command;
// Iterate through parameters
for (i=0;i<cmd->c_paras;i++)
delete cmd->paras[i];
}
void GetNextLine(CHARPTR source, CHARPTR end,
CHARPTR* linestart, CHARPTR* lineend)
// Returns the next complete line of current input
// linestart and lineend are set to contain the beginning and end of line
{
CHARPTR search=0;
UINT repeat=1;
// Increase line counter
linecnt++;
// now search for next line
while (repeat)
{
repeat = 0;
// seek line feed [LF] = ASCII 10
search = strchr(source,10);
// Now check if comment or empty line
if ((source[0] == '#') || (search-source==0))
{
// Last line?
if (search == 0)
// Yes! end of file, indicate by setting linestart = 0
*linestart = 0;
else
// No, go to next line
{
source = search+1;
repeat = 1;
// Increase line counter
linecnt++;
}
}
else
{
// Line contains commands
if (search != 0)
*lineend = search-1;
else
*lineend = end+1;
*linestart = source;
}
}
}
COMMAND GetFullCommand(CHARPTR source, CHARPTR end)
// Retrieves a command and its parameters from a line
// the line is delimited by 'source' and 'end'
{
CHARPTR cmdstart=0, cmdend=0;
COMMAND vreturn;
// Get command
GetCommand(source,end,&cmdstart,&cmdend);
// Save command
vreturn.command = (char*)CopyAndPtr(cmdstart,cmdend-cmdstart+1);
// Get parameters (search between end of command and end of line)
vreturn.paras = GetParameters(cmdend+1,end,&vreturn.c_paras);
/*// error?
if (vreturn.paras == 0)
{
cout << endl << "Error in " << filename << "(" << linecnt << ")"
<< ": syntax error\0";
vreturn.command = 0;
return vreturn;
}*/
// the current line is also saved in COMMAND struct (needed for error msgs)
vreturn.line = linecnt;
// return command
return vreturn;
}
void GetCommand(CHARPTR source, CHARPTR end,
CHARPTR* cmdstart, CHARPTR* cmdend)
// Extracts command from the current line
{
CHARPTR search=0, search2=0;
*cmdstart = 0;
*cmdend = 0;
// Search for first byte which is not [32] (space) or [9] (tab)
search = source;
while (((*search == 32) || (*search == 9)) && (search <= end)) search++;
if (search <= end)
{
// Now search next bite which is [32] (space) or [9] (tab)
search2 = search;
while (((*search2 != 32) && (*search2 != 9)) && (search2 <= end)) search2++;
if (search <= end)
{
*cmdstart = search;
*cmdend = search2-1;
}
}
}
CHARPTR* GetParameters(CHARPTR source, CHARPTR end, UINT* number)
// extracts the parameters of a command from a line
// 'source' should be end of command and 'end' the end of line
{
int c=0,i=0;
CHARPTR search=0, search2=0, search3=0;
CHARPTR* toreturn=0;
// bool inquote=false; // saves if we're currently within quotes
// First of all count commas ',' to calculate number of parameters
for (search=source;search<=end;search++)
{
// if (search[0] == '"') inquote = !inquote;
if (search[0] == ',') c++;
}
// # of parameters = commas + 1
c++;
/*// if inquote is true over here -> error!
if (inquote == true)
{
return 0;
}*/
// Create array of pointers
toreturn = new CHARPTR[c];
// Extract single parameters
search = source;
while (true)
{
// Search until first character appears (not 32 or 9)
while (((*search == 32) || (*search == 9)) && (search <= end)) search++;
// Search until comma
search2 = search;
while ((*search2 != 44) && (search2 <= end)) search2++;
// Go backwards to first char (not 32 or 9)
search3 = search2-1;
while (((*search3 == 32) || (*search3 == 9)) && (search3 >= source)) search3--;
// Parameter is know between search and search3
// if search3 < search then no parameter given
if (search3 < search)
toreturn[i] = (CHARPTR)CopyAndPtr("\0",1);
else
toreturn[i] = (CHARPTR)CopyAndPtr(search,search3-search+1);
i++;
// Set search to byte after comma
search = search2+1;
if (search >= end) break;
}
*number = i;
return toreturn;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -