Options.cpp
资源名称:warftpd.zip [点击查看]
上传用户:surprise9
上传日期:2007-01-04
资源大小:426k
文件大小:13k
源码类别:
Ftp客户端
开发平台:
Visual C++
- // This is part of the WAR SOFTWARE SERIES initiated by Jarle Aase
- // Copyright 1996 by Jarle Aase. All rights reserved.
- // See the "War Software Series Licende Agreement" for details concerning
- // use and distribution.
- // ---
- // This source code, executables and programs containing source code or
- // binaries or proprietetary technology from the War Software Series are
- // NOT alloed used, viewed or tested by any governmental agencies in
- // any countries. This includes the government, departments, police,
- // military etc.
- // ---
- // This file is intended for use with Tab space = 2
- // Created and maintained in MSVC Developer Studio
- // ---
- // NAME : Options.cpp
- // PURPOSE : Class COptions
- // PROGRAM :
- // DATE : Sept. 21 1996
- // AUTHOR : Jarle Aase
- // ---
- // REVISION HISTORY
- //
- #include "stdafx.h"
- #include "..IncludeWarSoftware.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- // Rarely used externals
- extern LPSTR StrDataTypeName[];
- // Static data
- CLookupList COptions::m_OptionsList;
- CString COptions::m_DefaultIniFile = "Daemon.ini";
- COptions::COptions()
- {
- m_IniName.Empty();
- m_Remote = NULL;
- m_Section = NULL;
- }
- COptions::~COptions()
- {
- m_OptionsList.DeleteItem(0,m_Section);
- m_IniName.Empty();
- m_Options.KillAll();
- if (m_Section)
- delete m_Section;
- }
- void COptions::Create(LPVOID Remote, LPCSTR Origin, LPCSTR Section, int SymbolicNumber)
- {
- m_IniName = Origin ? Origin : m_DefaultIniFile;
- m_Remote = Remote;
- m_Section = strdup(Section);
- m_OptionsList.AddItem(this, SymbolicNumber, Section, DATATYPE_LPSTR);
- }
- CLookupListItem *COptions::ResolveOption(LPCSTR Option, CString& cPending)
- {
- LPSTR p, buf = strdup(Option);
- COptions *pOptGrp = NULL;
- CLookupListItem *pOpt = NULL;
- cPending.Empty();
- if ((p = strtok(buf,"/")) == NULL)
- goto done;
- if ((pOptGrp = FindOptionGroup(p)) == NULL)
- goto done;
- ASSERT(AfxIsValidAddress(pOptGrp,sizeof(COptions)));
- if ((p = strtok(NULL,"=")) == NULL)
- goto done;
- if ((pOpt = pOptGrp->FindOption(p)) == NULL)
- goto done;
- ASSERT(AfxIsValidAddress(pOpt,sizeof(CLookupListItem)));
- ASSERT(AfxIsValidAddress(pOpt->m_Ptr,1));
- if ((p = strtok(NULL,"")) != NULL)
- cPending = p;
- done:
- free(buf);
- return pOpt;
- }
- BOOL COptions::SetOption(LPCSTR Option, BOOL DoMap)
- {
- // Options have the form: Group/Name=new value
- BOOL Rval = FALSE;
- CString cBuf;
- CLookupListItem *pOpt;
- if ((pOpt = ResolveOption(Option, cBuf)) == NULL)
- return FALSE;
- if (cBuf.IsEmpty())
- cBuf = "";
- CString MappedOption = cBuf;
- if (DoMap)
- ::ConvertToMultiline(MappedOption, cBuf);
- if (! ::PutData(pOpt->m_Ptr,pOpt->m_DataType, MappedOption))
- return FALSE;
- return TRUE;
- }
- CString COptions::GetOption(int Grp, int Item)
- {
- CString Value("");
- CString Key;
- Key.Format("%d/%d", Grp, Item);
- if (!GetOption(Key, Value))
- Value.Format("Illegal or unavailable option: %s", Key);
- return Value;
- }
- BOOL COptions::GetOption(LPCSTR Option, CString& cValue, BOOL DoMap)
- {
- // Options have the form: Group/Name
- BOOL Rval = FALSE;
- CString cBuf;
- CLookupListItem *pOpt;
- if ((pOpt = ResolveOption(Option, cBuf)) == NULL)
- return FALSE;
- if (! ::GetData(pOpt->m_Ptr,pOpt->m_DataType, cValue))
- return FALSE;
- if (DoMap)
- {
- cBuf = cValue;
- ::ConvertToSingelline(cValue, cBuf);
- }
- return TRUE;
- }
- // Save all declered options
- BOOL COptions::SaveAll()
- {
- CLinkedListItem *Item = m_Options.First();
- CLookupListItem *Ptr;
- CString cBuf;
- while(Item)
- {
- Ptr = (CLookupListItem *)(m_Options.LookupPtr(Item));
- switch(Ptr->m_DataType)
- {
- case DATATYPE_CSTRING:
- ASSERT(AfxIsValidString(*((CString *)(Ptr->m_Ptr))));
- ConvertToSingelline(cBuf, *((CString *)(Ptr->m_Ptr)));
- PutIniItem(Ptr->m_Name, cBuf);
- break;
- case DATATYPE_INT:
- case DATATYPE_BOOL:
- PutIniItem(Ptr->m_Name, *((int *)(Ptr->m_Ptr)));
- break;
- case DATATYPE_LPSTR:
- ASSERT(FALSE); // Strings should not eb used!
- break;
- default:
- ASSERT(FALSE); // Datatype must be handled!
- break;
- }
- Item = m_Options.Next(Item);
- }
- return TRUE;
- }
- BOOL COptions::LoadAll()
- {
- CLinkedListItem *Item = m_Options.First();
- CLookupListItem *Ptr;
- CString cBuf;
- while(Item)
- {
- Ptr = (CLookupListItem *)(m_Options.LookupPtr(Item));
- switch(Ptr->m_DataType)
- {
- case DATATYPE_CSTRING:
- ConvertToSingelline(cBuf, *((CString *)(Ptr->m_Ptr)));
- GetIniItem(Ptr->m_Name, cBuf, cBuf);
- ConvertToMultiline(*((CString *)(Ptr->m_Ptr)), cBuf);
- break;
- case DATATYPE_INT:
- case DATATYPE_BOOL:
- GetIniItem(Ptr->m_Name, *((int *)(Ptr->m_Ptr)), *((int *)(Ptr->m_Ptr)));
- break;
- case DATATYPE_LPSTR:
- ASSERT(FALSE); // Strings should not be used!
- break;
- default:
- ASSERT(FALSE); // Datatype must be handled!
- break;
- }
- Item = m_Options.Next(Item);
- }
- return TRUE;
- }
- void COptions::ResetSection(LPCSTR Section)
- {
- ::ResetSection(m_Remote, m_IniName, Section ? Section : m_Section);
- }
- void COptions::GetIniItem(LPCSTR Key, CString& Value, LPCSTR DefValue)
- {
- ::GetIniItem(m_Remote, m_IniName, m_Section, Key, Value, DefValue);
- }
- void COptions::GetIniItem(LPCSTR Key, int& Value, int DefValue)
- {
- ::GetIniItem(m_Remote, m_IniName, m_Section, Key, Value, DefValue);
- }
- void COptions::PutIniItem(LPCSTR Key, CString& Value)
- {
- ::PutIniItem(m_Remote, m_IniName, m_Section, Key, Value);
- }
- void COptions::PutIniItem(LPCSTR Key, int Value)
- {
- ::PutIniItem(m_Remote, m_IniName, m_Section, Key, Value);
- }
- COptions *COptions::FindOptionGroup(LPCSTR Name)
- {
- LPVOID Item;
- Item = m_OptionsList.Find(atoi(Name),Name);
- if (!Item)
- return NULL;
- ASSERT(AfxIsValidAddress(Item,4));
- return ((COptions *)Item);
- }
- CLookupListItem *COptions::FindOption(LPCSTR Name)
- {
- CLinkedListItem *Item;
- Item = m_Options.FindItem(atoi(Name),Name);
- if (!Item)
- return NULL;
- ASSERT(AfxIsValidAddress(Item,sizeof(CLookupListItem)));
- return m_Options.LookupPtr(Item);
- }
- // Create a report of the available option groups (OptionGroupName == NULL)
- // or of the options in a group.
- BOOL COptions::ListOptions(LPCSTR OptionGroupName, CString& cReturnBuf)
- {
- CLinkedListItem *Item;
- CLookupListItem *Ptr;
- COptions *MyOption;
- CString cBuf;
- cReturnBuf.Empty();
- if (OptionGroupName && *OptionGroupName)
- {
- // List the options within the named group
- MyOption = FindOptionGroup(OptionGroupName);
- if (!MyOption)
- return FALSE;
- ASSERT(AfxIsValidAddress(MyOption,sizeof(MyOption)));
- return MyOption->ListGroupOptions(cReturnBuf);
- }
- // List all option groups
- Item = m_OptionsList.First();
- if (!Item)
- return FALSE;
- while(Item)
- {
- Ptr = (CLookupListItem *)(m_OptionsList.LookupPtr(Item));
- ASSERT(Ptr);
- ASSERT(AfxIsValidAddress(Ptr,sizeof(CLookupListItem)));
- ASSERT(AfxIsValidString(Ptr->m_Name));
- cBuf.Format("%3d WarOptions://%s/n", Ptr->m_Number, Ptr->m_Name);
- cReturnBuf += cBuf;
- Item = m_OptionsList.Next(Item);
- }
- return TRUE;
- }
- // Create a report of the available options and their values
- // Format: Symbolic value<sp>Datatype<sp>Name<sp>=<sp>Value<n>
- BOOL COptions::ListGroupOptions(CString& cReturnBuf)
- {
- CLinkedListItem *Item = m_Options.First();
- CLookupListItem *Ptr;
- CString cBuf, cTmpBuf;
- cReturnBuf.Format("WarOptions://%s/{%s}n", m_Section, m_IniName);
- while(Item)
- {
- Ptr = (CLookupListItem *)(m_Options.LookupPtr(Item));
- cBuf.Format("%3d %-8s %s = %sn",
- Ptr->m_Number,
- SafeStringIndex(StrDataTypeName,Ptr->m_DataType,DATATYPE_INVALID),
- Ptr->m_Name, m_Options.GetData(Ptr, cTmpBuf));
- cReturnBuf += cBuf;
- Item = m_Options.Next(Item);
- }
- return TRUE;
- }
- ////////////////////////////////////////////////////////////////////////////
- // Low level ini file handling
- LPCSTR ConvertToMultiline(CString& cDest, LPCSTR Src)
- {
- ASSERT(Src != NULL);
- cDest.Empty();
- while(*Src)
- {
- if (*Src == '\')
- {
- if (Src[1] == '\')
- cDest += '\';
- else if (Src[1] == 'n') // n ..
- cDest += 'n';
- else if (Src[1] == 'r') // r ..
- cDest += 'r';
- else if (Src[1] == 't') // t ..
- cDest += 't';
- if (Src[1])
- ++Src;
- }
- else
- cDest += *Src;
- ++Src;
- }
- return (LPCSTR)cDest;
- }
- LPCSTR ConvertToSingelline(CString& cDest, LPCSTR Src)
- {
- ASSERT(Src != NULL);
- cDest.Empty();
- while(*Src)
- {
- if (*Src == 'n')
- cDest += "\n";
- else if (*Src == '\')
- cDest += "\\";
- else if (*Src == 'r')
- cDest += "\r";
- else if (*Src == 't')
- cDest += "\t";
- else
- cDest += *Src;
- ++Src;
- }
- return (LPCSTR)cDest;
- }
- void ResetSection(LPVOID Remote, LPCSTR IniName, LPCSTR Section)
- {
- ASSERT(Remote == NULL);
- if (IniName)
- WritePrivateProfileString(Section, NULL, NULL, IniName);
- else
- AfxGetApp()->WriteProfileString(Section, NULL, NULL);
- }
- void GetIniItem(LPVOID Remote, LPCSTR IniName, LPCSTR Section, LPCSTR Key, CString& Value, LPCSTR DefValue)
- {
- if (Remote)
- {
- CRemoteInterface *pRI = (CRemoteInterface *)Remote;
- ASSERT(AfxIsValidAddress(pRI, sizeof(CRemoteInterface)));
- CString cBuf, cMyValue;
- cBuf.Format("GET OPTION %s/%s", Section, Key);
- if (!pRI->Req((HANDLE)pRI, cBuf, cMyValue, 1000 * 60))
- AfxThrowResourceException();
- if ((atoi(cMyValue) != 200) || (cMyValue.GetLength() < 4))
- Value = DefValue;
- else
- Value = (LPCSTR)cMyValue + 4;
- }
- else if (IniName && *IniName)
- {
- char buf[1024 * 4];
- *buf = ' ';
- GetPrivateProfileString(Section,Key,DefValue,buf,sizeof(buf),IniName);
- Value = buf;
- }
- else
- Value = AfxGetApp()->GetProfileString(Section,Key,DefValue);
- }
- void GetIniItem(LPVOID Remote, LPCSTR IniName, LPCSTR Section, LPCSTR Key, int& Value, int DefValue)
- {
- CString cBuf, cTmp;
- cTmp.Format("%d", DefValue);
- GetIniItem(Remote, IniName, Section, Key, cBuf, cTmp);
- if (cBuf == "TRUE")
- Value = TRUE;
- else
- Value = atoi(cBuf);
- }
- void PutIniItem(LPVOID Remote, LPCSTR IniName, LPCSTR Section, LPCSTR Key, LPCSTR Value)
- {
- if (Remote)
- {
- CRemoteInterface *pRI = (CRemoteInterface *)Remote;
- ASSERT(AfxIsValidAddress(pRI, sizeof(CRemoteInterface)));
- CString cBuf, cRbuf;
- cBuf.Format("SET OPTION %s/%s=%s", Section, Key, Value);
- if (!pRI->Req((HANDLE)pRI, cBuf, cRbuf, 1000 * 60))
- AfxThrowResourceException();
- if (atoi(cRbuf) != 200)
- {
- cBuf.Format("PutIniItem([%s], %s = '%s') - failed. (%s)",
- Section, Key, Value, cRbuf);
- AfxMessageBox(cBuf);
- }
- }
- else if (IniName && *IniName)
- {
- WritePrivateProfileString(Section,Key,Value,IniName);
- }
- else
- AfxGetApp()->WriteProfileString(Section,Key,Value);
- }
- void PutIniItem(LPVOID Remote, LPCSTR IniName, LPCSTR Section, LPCSTR Key, int Value)
- {
- CString cBuf;
- cBuf.Format("%d", Value);
- PutIniItem(Remote, IniName, Section, Key, cBuf);
- }
- DWORD GetIniItemSectionNames(LPVOID Remote, LPCSTR IniName, LPCSTR Pattern, CString& cBuf)
- {
- LPSTR Buffer, Key;
- DWORD BufLen = 1024 * 4;
- DWORD Rval = 0;
- ASSERT(IniName);
- cBuf.Empty();
- for(;;BufLen += (1024 * 32))
- {
- if ((Buffer = (LPSTR)malloc(BufLen)) == NULL)
- return 0;
- if (GetPrivateProfileSectionNames(Buffer, BufLen, IniName) != (BufLen - 2))
- break;
- free(Buffer);
- }
- for(Key = Buffer; *Key;)
- {
- ASSERT(AfxIsValidString(Key));
- if (Pattern && *Pattern)
- {
- if (!PatternMatchesName(Key, Pattern))
- goto next;
- }
- if (!cBuf.IsEmpty())
- cBuf += "rn";
- cBuf += Key;
- ++Rval;
- next:
- while(*Key)
- ++Key;
- ++Key;
- }
- free(Buffer);
- return Rval;
- }
- DWORD GetIniItemSection(LPVOID Remote, LPCSTR IniName, LPCSTR SectKey, LPCSTR Pattern, CString& cBuf)
- {
- LPSTR Buffer, Key;
- DWORD BufLen = 1024 * 4;
- DWORD Rval = 0;
- cBuf.Empty();
- ASSERT(IniName);
- for(;;BufLen += (1024 * 32))
- {
- if ((Buffer = (LPSTR)malloc(BufLen)) == NULL)
- return 0;
- if (GetPrivateProfileSection(SectKey, Buffer, BufLen, IniName) != (BufLen - 2))
- break;
- free(Buffer);
- }
- for(Key = Buffer; *Key;)
- {
- ASSERT(AfxIsValidString(Key));
- if (Pattern && *Pattern)
- {
- if (!PatternMatchesName(Key, Pattern))
- goto next;
- }
- if (!cBuf.IsEmpty())
- cBuf += "rn";
- cBuf += Key;
- ++Rval;
- next:
- while(*Key)
- ++Key;
- ++Key;
- }
- free(Buffer);
- return Rval;
- }
- ////////////////////////////////////////////////////////////////////
- // CFTPDaemonCoreOptions
- CFTPDaemonCoreOptions::CFTPDaemonCoreOptions()
- {
- DECLARE_FTPDCORE_OPTIONS
- }
- CLogOptions::CLogOptions()
- {
- DECLARE_LOG_OPTIONS
- }
- CRemoteAdminOptions::CRemoteAdminOptions()
- {
- DECLARE_RA_OPTIONS
- }
- CDaemonNTSOptions::CDaemonNTSOptions()
- {
- DECLARE_NTSERVICE_OPTIONS
- }
- int CAdvancedOptions::m_PriMap[4] =
- {
- REALTIME_PRIORITY_CLASS,
- HIGH_PRIORITY_CLASS,
- NORMAL_PRIORITY_CLASS,
- IDLE_PRIORITY_CLASS
- };
- int CAdvancedOptions::m_OverlappedMap[9] =
- {
- 0,
- (8 * 1024),
- (16 * 1024),
- (32 * 1024),
- (64 * 1024),
- (128 * 1024),
- (255 * 1024),
- (512 * 1024),
- (1024 * 1024)
- };
- CAdvancedOptions::CAdvancedOptions()
- {
- DECLARE_ADVANCED_OPTIONS
- if (!IsNT())
- m_OverlappedIO = 0;
- }
- CFTPClientOptions::CFTPClientOptions()
- {
- DECLARE_FTPC_OPTIONS
- }
- CGUIOptions::CGUIOptions()
- {
- DECLARE_GUI_OPTIONS
- }
- CFTPClientFirewallOptions::CFTPClientFirewallOptions()
- {
- DECLARE_FTPCFIREWALL_OPTIONS
- }