31static const tchar szNO[] =
_T(
"no");
32static const tchar szOFF[] =
_T(
"off");
33static const tchar szZERO[] =
_T(
"0");
34static const tchar szFALSE[] =
_T(
"false");
35static const tchar szYES[] =
_T(
"yes");
36static const tchar szON[] =
_T(
"on");
37static const tchar szONE[] =
_T(
"1");
38static const tchar szTRUE[] =
_T(
"true");
40static const tchar szSep[] =
_T(
"\\");
47static LPTSTR skip_ws(LPTSTR sz)
50 while (*sz ==
_T(
' ') || *sz ==
_T(
'\t'))
59static LPTSTR skipPrefix (LPTSTR sz,
tchar ch)
104 return tstring(b ? szYES : szNO);
125 while(*ep && (*ep ==
_T(
'\t') || *ep ==
_T(
' ')))
128 while(*p && *p !=
_T(
'\n')){
136static LPTSTR next_name(LPTSTR p)
139 while(*p !=
_T(
'\0'))
149 tchar *sections, *entries;
151 int sect_size = 4096;
154 _ClnObj1 = sections =
new tchar[sect_size];
158 while(size >= (sect_size-2));
164 _ClnObj2 = entries =
new tchar[sect_size];
165 size = file.
ReadSection(sections, entries, sect_size);
168 while(size >= (sect_size-2));
176 size = file.
ReadInteger(sections,entries, INT_MAX);
184 entries = next_name(entries);
187 sections = next_name(sections);
195 PRECONDITION(section && entry && buffer && bufSize > 0);
197 LPTSTR text = __ClnObj;
199 if (!
ReadString(section, entry, text, bufSize * 3 + 1,
_T(
"")))
206 for (
uint i = 0; i < bufSize; i++) {
224 LPTSTR text = __ClnObj;
227 for (
uint i = 0; i < bufSize; i++) {
246 int retval = (int)
_tcstol(text, &end, 10);
264 snwprintf(text, 20,
_T(
"%d"), value);
266 snprintf(text, 20,
"%d", value);
269 _ltot (value, text, 10);
284 int sect_size = 4096;
288 _ClnObj1 = sections =
new tchar[sect_size];
289 size =
ReadSection(section.c_str(),sections, sect_size);
292 while(size >= (sect_size-2));
303 int sect_size = 4096;
307 _ClnObj1 = sections =
new tchar[sect_size];
311 while(size >= (sect_size-2));
321 const auto resultSize =
static_cast<uint>(result.size() + 1);
322 const auto p =
const_cast<LPTSTR
>(result.data());
323 const auto n =
ReadString(section.c_str(), entry.c_str(), p, resultSize, defstr);
325 result.shrink_to_fit();
326 swap(buffer, result);
336 double retval = defval;
380 _stprintf(text, format.c_str(), (
long double)val);
421 if(text.length() > 0)
451 _stscanf(text,
_T(
"%hu,%hu,%hu,%hu,%hu,%hu,%hu,%hu"),&dt.wYear,&dt.wMonth,&dt.wDayOfWeek,
452 &dt.wDay,&dt.wHour,&dt.wMinute,&dt.wSecond,&dt.wMilliseconds);
465 wsprintf(text,
_T(
"%hu,%hu,%hu,%hu,%hu,%hu,%hu,%hu"),dt.wYear,dt.wMonth,dt.wDayOfWeek,
466 dt.wDay,dt.wHour,dt.wMinute,dt.wSecond,dt.wMilliseconds);
479 _stscanf(text,
_T(
"h=%d m=%d s=%d"), &h, &m, &s);
530 LPCTSTR szSep =
_T(
", \t");
558 lf.lfWeight = FW_NORMAL;
560 lf.lfUnderline = FALSE;
561 lf.lfStrikeOut = FALSE;
564 lf.lfWeight = FW_BOLD;
568 lf.lfUnderline = TRUE;
570 lf.lfStrikeOut = TRUE;
580 lf.lfOrientation = i;
585 lf.lfCharSet =
uint8(i);
590 lf.lfOutPrecision =
uint8(i);
595 lf.lfClipPrecision =
uint8(i);
600 lf.lfQuality =
uint8(i);
605 lf.lfPitchAndFamily =
uint8(i);
660 _tprintf(ach,
_T(
"%d"), logFont.lfHeight);
663 s =
_itot(logFont.lfHeight, ach, 10);
667 _tprintf(ach,
_T(
"%d"), logFont.lfWidth);
670 s +=
_itot(logFont.lfWidth, ach, 10);
675 ach[i++] = (logFont.lfWeight < FW_BOLD) ?
_T(
'N') :
_T(
'B');
676 if (logFont.lfItalic)
678 if (logFont.lfUnderline)
680 if (logFont.lfStrikeOut)
687 _tprintf(ach,
_T(
"%d"), logFont.lfEscapement);
690 s +=
_itot(logFont.lfEscapement, ach, 10);
694 _tprintf(ach,
_T(
"%d"), logFont.lfOrientation);
697 s +=
_itot(logFont.lfOrientation, ach, 10);
701 _tprintf(ach,
_T(
"%d"), logFont.lfCharSet);
704 s +=
_itot(logFont.lfCharSet, ach, 10);
708 _tprintf(ach,
_T(
"%d"), logFont.lfOutPrecision);
711 s +=
_itot(logFont.lfOutPrecision, ach, 10);
715 _tprintf(ach,
_T(
"%d"), logFont.lfClipPrecision);
718 s +=
_itot(logFont.lfClipPrecision, ach, 10);
722 _tprintf(ach,
_T(
"%d"), logFont.lfQuality);
725 s +=
_itot(logFont.lfQuality, ach, 10);
729 _tprintf(ach,
_T(
"%d"), logFont.lfPitchAndFamily);
732 s +=
_itot(logFont.lfPitchAndFamily, ach, 10);
735 s += logFont.lfFaceName;
765 int R =
_ttoi(skipPrefix(sz[0],
_T(
'R')));
766 int G =
_ttoi(skipPrefix(sz[1],
_T(
'G')));
767 int B =
_ttoi(skipPrefix(sz[2],
_T(
'B')));
769 if (R >= 0 && R < 256 &&
789 wsprintf (ach,
_T(
"R=%d,G=%d,B=%d"), clr.
Red(), clr.
Green(), clr.
Blue());
802 const auto r = _stscanf_s(text.c_str(),
_T(
"X=%d,Y=%d"), &x, &y);
803 if (r != 2)
return false;
815 wsprintf(buf,
_T(
"X=%d,Y=%d"), point.x, point.y);
828 const auto r = _stscanf_s(text.c_str(),
_T(
"L=%d,T=%d,W=%d,H=%d"), &l, &t, &w, &h);
829 if (r != 4)
return false;
830 rect = {{l, t},
TSize{w, h}};
841 wsprintf(buf,
_T(
"L=%d,T=%d,W=%d,H=%d"), r.left, r.top, r.
Width(), r.
Height());
854 const auto r = _stscanf_s(text.c_str(),
_T(
"W=%d,H=%d"), &w, &h);
855 if (r != 2)
return false;
867 wsprintf(buf,
_T(
"W=%d,H=%d"), size.cx, size.cy);
886 return GetPrivateProfileString(section, 0,
_T(
""), buffer, bufSize,
FileName.c_str());
892 return GetPrivateProfileString(0, 0,
_T(
""), buffer, bufSize,
FileName.c_str());
900 return ::WritePrivateProfileString(section, 0, 0,
FileName.c_str()) != 0;
910 return ::WritePrivateProfileString (section, entry, 0,
FileName.c_str()) != 0;
917 ::WritePrivateProfileString(0, 0, 0,
FileName.c_str());
928 LPTSTR buffer,
uint bufSize, LPCTSTR def)
const
930 PRECONDITION(section && entry && buffer && bufSize > 0);
931 return ::GetPrivateProfileString (section, entry, def?def:
_T(
""), buffer, bufSize,
FileName.c_str());
939 return ::WritePrivateProfileString (section, entry, value,
FileName.c_str()) != 0;
946 PRECONDITION(section && entry && buffer && bufSize > 0);
947 return ::GetPrivateProfileStruct(section, entry, buffer, bufSize,
FileName.c_str()) != 0;
955 return ::WritePrivateProfileStruct(section, entry, buffer, bufSize,
FileName.c_str()) != 0;
961class TStringMapNode:
public TMapNode<tstring,tstring> {
966 TStringMapNode(
const tstring& str)
972class TStringNode:
public TMapNode<tstring,TStringMap> {
974 TStringNode(
const tstring& str)
978class TMemConfigFileSections:
public TIPtrArray<TStringNode*> {
980 TMemConfigFileSections(){}
981 ~TMemConfigFileSections(){}
983 bool DestroyItem(TStringNode* item);
984 bool Destroy(
int loc);
985 int Add(TStringNode* item);
986 bool HasMember(TStringNode* item)
const;
987 int Find(TStringNode* item)
const;
990bool TMemConfigFileSections::HasMember(TStringNode* item)
const
992 return Find(item) !=
NPOS;
995bool TMemConfigFileSections::DestroyItem(TStringNode* item)
997 const auto index = Find(item);
1000 TStringNode* item =
Data[index];
1008bool TMemConfigFileSections::Destroy(
int loc)
1010 if(loc < (
int)
Size()){
1011 TStringNode* item =
Data[loc];
1019int TMemConfigFileSections::Add(TStringNode* item)
1024 while( loc > 0 && *item < *
Data[loc-1]) {
1032int TMemConfigFileSections::Find(TStringNode* item)
const
1035 auto upper =
static_cast<int>(
Size()) - 1;
1036 while (lower <= upper)
1038 const auto middle = (lower + upper) / 2;
1039 if (*
Data[middle] == *item)
1041 if (*
Data[middle] < *item)
1056 Sections =
new TMemConfigFileSections();
1076int TMemConfigFile::AddSection(
const tstring& section)
1078 return Sections->Add(
new TStringNode(section));
1084 TStringNode sn(section);
1085 const auto index = Sections->Find(&sn);
1086 if (index != Sections->NPOS)
1088 return (*Sections)[index]->Value.DestroyItem(TStringMapNode(entry));
1096 TStringNode sn(section);
1097 return Sections->DestroyItem(&sn);
1111 TStringNode sn(section);
1112 const auto k = Sections->Find(&sn);
1113 if (k == Sections->NPOS)
return 0;
1114 const TStringMap& map = (*Sections)[k]->Value;
1115 if (map.Empty())
return 0;
1116 for (
uint i = 0; i != map.Size(); ++i)
1118 const tstring& s = map[i].Name;
1119 size_t m =
static_cast<size_t>(buffer + bufSize - 1 - p);
1121 size_t n = min(s.size(), m - 1);
1130 return static_cast<uint>(p - buffer) - 2;
1144 if (Sections->Empty())
return 0;
1145 for (
uint i = 0; i != Sections->Size(); ++i)
1147 const tstring& s = (*Sections)[i]->Name;
1148 size_t m =
static_cast<size_t>(buffer + bufSize - 1 - p);
1150 size_t n = min(s.size(), m - 1);
1159 return static_cast<uint>(p - buffer) - 2;
1174 PRECONDITION(section && entry && buffer && bufSize > 0);
1177 static LPCTSTR Lookup(
const TMemConfigFile& f, LPCTSTR section, LPCTSTR entry, LPCTSTR def)
1179 TStringNode sn(section);
1180 const auto i = f.Sections->Find(&sn);
1181 if (i == f.Sections->
NPOS)
return def;
1182 const TStringMap& map = (*f.Sections)[i]->Value;
1183 const auto j = map.
Find(TStringMapNode{entry});
1184 if (j == map.NPOS)
return def;
1185 return map[j].Value.c_str();
1188 LPCTSTR s = TLocal::Lookup(*
this, section, entry, defstr ? defstr :
_T(
""));
1189 size_t n = min(
::_tcslen(s),
static_cast<size_t>(bufSize - 1));
1191 buffer[n] =
_T(
'\0');
1192 return static_cast<uint>(n);
1199 TStringNode sn(section);
1200 auto index = Sections->Find(&sn);
1201 if (index == Sections->NPOS)
1202 index = AddSection(section);
1204 index = map.
Find(TStringMapNode(entry));
1205 if (index != map.NPOS)
1206 map[index].Value =
tstring(value);
1208 index = map.Add(TStringMapNode(entry,value));
1209 return index != map.NPOS;
1219 for(
uint section = 0; section < Sections->Size(); section++){
1221 file <<
_T(
"[") << (*Sections)[section]->Name.c_str() <<
_T(
"]") <<
_T(
"\r\n");
1223 TStringMap& map = (*Sections)[section]->Value;
1224 for(
uint value = 0; value < map.Size(); value++)
1225 file << map[value].Name.c_str() <<
_T(
"=") << map[value].Value.c_str() <<
_T(
"\r\n");
1231static LPTSTR getName(
tstring& name,LPTSTR p)
1248static void getValue(
tstring& name,LPTSTR p)
1251 while(*p && (*p==
_T(
' ') || *p ==
_T(
'\t')) && *p !=
_T(
'='))
1270 LPTSTR p = skip_ws((LPTSTR)(LPCTSTR)fitr);
1274 getName(tmp_name1,p);
1275 sec_index = AddSection(tmp_name1.c_str());
1278 else if(sec_index >= 0){
1279 p = getName(tmp_name1,p);
1280 getValue(tmp_name2,p);
1281 (*Sections)[sec_index]->Value.Add(TStringMapNode(tmp_name1,tmp_name2));
1361 size_t m =
static_cast<size_t>(buffer + bufSize - 1 - p);
1372 return static_cast<uint>(p - buffer) - 2;
1393 size_t m =
static_cast<size_t>(buffer + bufSize - 1 - p);
1404 return static_cast<uint>(p - buffer) - 2;
1429 return k ?
static_cast<int>(k->GetValueOrDefault<
uint32>(entry, defint)) : defint;
1436 return k.
SetValue(entry,
static_cast<uint32>(value)) == ERROR_SUCCESS;
1452 PRECONDITION(section && entry && buffer && bufSize > 0);
1454 const auto d = defstr ? defstr :
tstring{};
1455 const auto s = k ? k->GetValueOrDefault(entry, d) : d;
1456 const auto n = min(
static_cast<uint>(s.size()), bufSize - 1);
1457 const auto e = copy_n(s.begin(), n, buffer);
1467 return k.
SetValue(entry, value) == ERROR_SUCCESS;
1475 PRECONDITION(section && entry && buffer && bufSize > 0);
1477 if (!k)
return false;
1478 auto size =
uint32{bufSize};
1480 return k->QueryValue(entry, &type,
reinterpret_cast<uint8*
>(buffer), &size) == ERROR_SUCCESS;
1489 return k.
SetValue(entry, REG_BINARY,
reinterpret_cast<const uint8*
>(buffer), bufSize) == ERROR_SUCCESS;
#define PRECONDITION(condition)
Pointer to an array of type T.
Class wrapper for management of color values.
uint8 Blue() const
Return the color's blue component.
uint8 Red() const
Return the color's red component.
uint8 Green() const
Return the color's green component.
TConfigFile(const tstring &name)
Constructs instance and sets FileName to name.
virtual bool WriteData(LPCTSTR section, LPCTSTR entry, void *buffer, uint size)
This method writes binary data.
static int DefaultPrecision
bool ReadTime(LPCTSTR section, LPCTSTR entry, TTime &val) const
bool ReadColor(LPCTSTR section, LPCTSTR entry, TColor &color) const
This method reads an RGB color.
double ReadDouble(LPCTSTR section, LPCTSTR entry, double defval=0.0) const
virtual bool WriteInteger(LPCTSTR section, LPCTSTR entry, int value)
This method writes an integer. We format it as a string.
bool WriteRect(LPCTSTR section, LPCTSTR entry, const TRect &rc)
This method writes a TRect.
bool ReadDate(LPCTSTR section, LPCTSTR entry, TDate &value) const
virtual bool SectionExists(LPCTSTR section) const
bool WriteFont(LPCTSTR section, LPCTSTR entry, const LOGFONT &font)
This method writes a font description in the following format:
bool WriteColor(LPCTSTR section, LPCTSTR entry, const TColor &clr)
This method writes an RGB color.
bool ReadPoint(LPCTSTR section, LPCTSTR entry, TPoint &val) const
This method reads a TPoint.
bool WriteDouble(LPCTSTR section, LPCTSTR entry, double value)
This method writes an integer.
virtual void LoadValues(const TConfigFile &file)
virtual uint ReadSections(TCHAR *sections, uint bufSize) const =0
bool WriteTime(LPCTSTR section, LPCTSTR entry, TTime &val)
bool WriteDate(LPCTSTR section, LPCTSTR entry, const TDate &val)
bool WriteSystemTime(LPCTSTR section, LPCTSTR entry, const TSystemTime &val)
bool ReadSize(LPCTSTR section, LPCTSTR entry, TSize &sz) const
This method reads a TSize.
bool ReadBool(LPCTSTR section, LPCTSTR entry, bool defval=false) const
This method reads a boolean value.
bool ReadRect(LPCTSTR section, LPCTSTR entry, TRect &val) const
This method reads a TRect.
bool WritePoint(LPCTSTR section, LPCTSTR entry, const TPoint &point)
This method writes a TPoint.
virtual uint ReadSection(LPCTSTR section, TCHAR *buffer, uint bufSize) const =0
virtual int ReadInteger(LPCTSTR section, LPCTSTR entry, int defint=0) const
This method reads an integer.
static tstring BoolToString(bool val)
This static method returns yes or no given a boolean value.
bool WriteSize(LPCTSTR section, LPCTSTR entry, const TSize &size)
This method writes a TSize.
bool ReadFont(LPCTSTR section, LPCTSTR entry, LOGFONT &font) const
This method will take a profile string of the form:
virtual bool ReadData(LPCTSTR section, LPCTSTR entry, void *buffer, uint size) const
This method reads binary data.
bool WriteBool(LPCTSTR section, LPCTSTR entry, bool val)
This method writes a bool.
bool ReadSystemTime(LPCTSTR section, LPCTSTR entry, TSystemTime &val) const
static bool StringToBool(LPCTSTR str, bool *val)
This static method converts a string to a bool, accepting most common boolean keywords: yes,...
virtual bool WriteString(LPCTSTR section, LPCTSTR entry, LPCTSTR value)=0
virtual uint ReadString(LPCTSTR section, LPCTSTR entry, TCHAR *buffer, uint bufSize, LPCTSTR defstr=0) const =0
The TDate class represents a date.
TDate()
Construct a TDate with the current date.
static HowToPrint SetPrintOption(HowToPrint h)
Sets the print option for all TDate objects and returns the old setting.
tstring AsString() const
Converts the TDate object to a string object.
HowToPrint
Lists different print formats.
bool IsOpen() const
Returns true if the file is open, false otherwise.
@ CreateAlways
Creates a new file. The function overwrites the file if it exists.
The TFileLineIterator class is used to iterate through a TTextFile file.
virtual bool ReadData(LPCTSTR section, LPCTSTR entry, void *buffer, uint size) const
This method reads binary data.
virtual uint ReadSections(TCHAR *buffer, uint bufSize) const
virtual uint ReadSection(LPCTSTR section, TCHAR *buffer, uint bufSize) const
virtual bool WriteString(LPCTSTR section, LPCTSTR entry, LPCTSTR value)
This method is the low-level method for writing strings.
virtual void UpdateFile()
virtual bool EraseEntry(LPCTSTR section, LPCTSTR entry)
This method deletes the specified entry.
virtual uint ReadString(LPCTSTR section, LPCTSTR entry, TCHAR *buffer, uint bufSize, LPCTSTR defstr=0) const
This method is the base method for reading strings.
virtual bool WriteData(LPCTSTR section, LPCTSTR entry, void *buffer, uint size)
This method writes binary data.
virtual bool EraseSection(LPCTSTR section)
This method deletes the specified section.
int Find(const T &t) const
TMapNode(tstring &name, tstring &val)
virtual uint ReadString(LPCTSTR section, LPCTSTR entry, TCHAR *buffer, uint bufSize, LPCTSTR defstr=0) const
Looks up the value of the given entry of the given section, and if found, copies it to the given buff...
virtual uint ReadSections(TCHAR *buffer, uint bufSize) const
Copies all section names to the specified buffer.
virtual ~TMemConfigFile()
virtual uint ReadSection(LPCTSTR section, TCHAR *buffer, uint bufSize) const
Copies all value names of the given section to the specified buffer.
virtual bool EraseSection(LPCTSTR section)
TMemConfigFile(const tstring &name)
virtual void UpdateFile()
virtual bool WriteString(LPCTSTR section, LPCTSTR entry, LPCTSTR value)
virtual void LoadValues(const TConfigFile &file)
virtual bool EraseEntry(LPCTSTR section, LPCTSTR entry)
TPoint is a support class, derived from tagPOINT.
TRect is a mathematical class derived from tagRect.
int Height() const
Returns the height of this rectangle (bottom - top).
int Width() const
Returns the width of this rectangle (right - left).
virtual bool EraseSection(LPCTSTR section)
virtual bool EraseEntry(LPCTSTR section, LPCTSTR entry)
virtual uint ReadSections(TCHAR *buffer, uint bufSize) const
Copies all section names to the specified buffer.
virtual ~TRegConfigFile()
TRegConfigFile(const tstring &name, HKEY hRoot=HKEY_CURRENT_USER)
This source file implements TRegConfigFile.
virtual void UpdateFile()
virtual bool WriteData(LPCTSTR section, LPCTSTR entry, void *buffer, uint size)
This method writes binary data.
virtual bool SectionExists(LPCTSTR section) const
virtual int ReadInteger(LPCTSTR section, LPCTSTR entry, int defint=0) const
virtual bool WriteString(LPCTSTR section, LPCTSTR entry, LPCTSTR value)
virtual uint ReadSection(LPCTSTR section, TCHAR *buffer, uint bufSize) const
Copies all value names of the given section to the specified buffer.
virtual bool WriteInteger(LPCTSTR section, LPCTSTR entry, int value)
virtual uint ReadString(LPCTSTR section, LPCTSTR entry, TCHAR *buffer, uint bufSize, LPCTSTR defstr=0) const
Looks up the value of the given entry of the given section, and if found, copies it to the given buff...
virtual bool ReadData(LPCTSTR section, LPCTSTR entry, void *buffer, uint size) const
This method reads binary data.
Encapsulates a registration key in the Windows Registry.
long NukeKey(LPCTSTR subKeyName)
Completely eliminates a child key, including any of its subkeys.
LPCTSTR GetName() const
Returns a string identifying this key.
auto HasSubkey(const tstring &keyName) const -> bool
Returns true if this key has a subkey with the given name.
auto GetSubkey(const tstring &keyName, REGSAM samDesired=KEY_READ) const -> std::optional< TRegKey >
Returns the subkey with the given name, if it exists.
long SetValue(LPCTSTR valName, uint32 type, const uint8 *data, uint32 dataSize) const
Associates a value with this key.
long DeleteValue(LPCTSTR valName) const
Removes a named value from this registry key.
Iterator for walking thru the subkeys of a key.
Encapsulates a value-data entry within one registration key.
LPCTSTR GetName() const
Returns a string identifying this value.
Iterator for walking through the values of a key.
The tagSIZE struct is defined as.
TSystemTime is a class derived from the structure SYSTEMTIME.
The TTextFile class is derived from TBufferedFile and encapsulates standard file characteristics and ...
The TTime class encapsulates time functions and characteristics.
SecondTy Second() const
Returns seconds.
MinuteTy Minute() const
Returns the minute in local time.
HourTy Hour() const
Returns the hour in local time.
TTime()
Construct TTime with current time (seconds since Jan 1, 1901).
Definition of windowing system color classes.
This header file declares the class: TConfigFile,TIniConfigFile, TRegConfigFile and TConfigFileSectio...
Object Windows Library (OWLNext Core).
std::ostringstream tostringstream
bool is_integer(TCHAR *text)
std::istringstream tistringstream
bool is_digit(tchar c)
where string is integer????
TSortedObjectArray< TStringMapNode > TStringMap
#define COUNTOF(s)
Array element count Important: Only use this with an argument of array type.
Various types of smart pointer templatized classes.
Definition of container classes used and made available by OWL.
Classes for window system structure and type encapsulation.