OWLNext    7.0
Borland's Object Windows Library for the modern age
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
profile.cpp
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// Borland WinSys Library
3// Copyright (c) 1991, 1996 by Borland International, All Rights Reserved
4//
5/// \file
6/// Implementation of TProfile class
7//----------------------------------------------------------------------------
8#include <owl/pch.h>
9#include <owl/defs.h>
10#include <owl/profile.h>
11#include <owl/private/memory.h>
12#include <limits>
13#include <owl/except.h>
14
15namespace owl {
16
17//
18/// Constructs a TProfile object for the indicated section within the profile file
19/// specified by filename. If the file name is not provided, the file defaults to
20/// the system profile file; for example, WIN.INI under Windows .
21//
26
27//
28/// String-aware overload
29//
31{
32 Init(
33 section.empty() ? nullptr : section.c_str(),
34 filename.empty() ? nullptr : filename.c_str());
35}
36
38{
39 Section = section ? strnewdup(section) : nullptr;
40#if defined(UNICODE)
42#endif
43 // Use OpenFile to track down the given filename
44 // if can't find it, use copy of original name,
45 // if found, use copy of full path
46 //
47 if (filename) {
49 ofs.cBytes = sizeof ofs;
50#if defined(UNICODE)
51 const auto fn = W2A(filename);
53 (OpenFile(fn, &ofs, OF_EXIST) == HFILE_ERROR) ? filename : A2W(ofs.szPathName)
54 );
55#else
57 //JJH
58 #if defined(__GNUC__)
60 #else
61 (OpenFile(filename, &ofs, OF_EXIST) == HFILE_ERROR) ? filename : ofs.szPathName
62 #endif
63 );
64#endif
65 }
66 else {
67 FileName = nullptr;
68 }
69}
70
71//
72/// Destroys the TProfile object.
73/// Cleans up buffers
74//
76{
77 delete[] FileName;
78 delete[] Section;
79}
80
81//
82/// Looks up and returns the integer value associated with the given string, key. If
83/// key is not found, the default value, defaultInt, is returned.
84//
85int
92
93//
94/// Looks up and returns the string value associated with the given key string. The
95/// string value is copied into buff, up to buffSize bytes. If the key is not
96/// found, defaultString provides the default value. If a 0 key is passed, all
97/// section values are returned in buff.
98//
99bool
107
110{
111 tstring buf;
112 tstring::size_type buf_size = 256;
113 const tstring::size_type limit = std::numeric_limits<tstring::size_type>::max();
114 for (;;)
115 {
116 buf.resize(buf_size);
117 DWORD n = FileName ?
118 ::GetPrivateProfileString(Section, key.c_str(), defaultString.c_str(), &buf[0], static_cast<DWORD>(buf.size()), FileName) :
119 ::GetProfileString(Section, key.c_str(), defaultString.c_str(), &buf[0], static_cast<DWORD>(buf.size()));
120 if (n < buf_size - 2) // Sure it's not truncated? (See doc for GetProfileString.)
121 {
122 buf.resize(n); // Shrink to contents.
123 break;
124 }
125 if (buf_size > limit / 2)
126 throw TXOwl(_T("TProfile::GetString: String is too large"));
127 buf_size *= 2;
128 }
129 return buf;
130}
131
132//
133/// Looks up the key and replaces its value with the integer value passed (int). If
134/// the key is not found, WriteInt makes a new entry. Returns true if successful.
135//
136bool
138{
139 tchar buf[16];
140#if defined __GNUC__
141 _tprintf(buf, "%d", value);
142#else
143 _itot(value, buf, 10);
144#endif
145 return WriteString(key, buf);
146}
147
148//
149/// Looks up the key and replaces its value with the string value passed (str). If
150/// the key is not found, WriteString makes a new entry. Returns true if successful.
151//
152bool
159
160//
161/// Makes sure that all written profile values are flushed to the actual file.
162//
163void
165{
166 if (FileName)
167 ::WritePrivateProfileString(nullptr, nullptr, nullptr, FileName);
168}
169
170} // OWL namespace
171/* ========================================================================== */
bool WriteInt(LPCTSTR key, int value)
Looks up the key and replaces its value with the integer value passed (int).
Definition profile.cpp:137
void Flush()
Makes sure that all written profile values are flushed to the actual file.
Definition profile.cpp:164
bool GetString(LPCTSTR key, TCHAR *buff, unsigned buffSize, LPCTSTR defaultString=0)
Looks up and returns the string value associated with the given key string.
Definition profile.cpp:100
int GetInt(LPCTSTR key, int defaultInt=0)
Looks up and returns the integer value associated with the given string, key.
Definition profile.cpp:86
bool WriteString(LPCTSTR key, LPCTSTR str)
Looks up the key and replaces its value with the string value passed (str).
Definition profile.cpp:153
TCHAR * Section
Name of the section to use.
Definition profile.h:82
TCHAR * FileName
File name of the .INI file.
Definition profile.h:83
void Init(LPCTSTR section, LPCTSTR filename)
Definition profile.cpp:37
~TProfile()
Destroys the TProfile object.
Definition profile.cpp:75
TProfile(LPCTSTR section, LPCTSTR filename=0)
Constructs a TProfile object for the indicated section within the profile file specified by filename.
Definition profile.cpp:22
TXOwl is root class of the ObjectWindows exception hierarchy.
Definition except.h:38
#define _itot
Definition cygwin.h:66
#define _T(x)
Definition cygwin.h:51
Reliable platform independent header for common memory and string functions.
#define W2A(lpw)
Definition memory.h:166
#define USES_CONVERSION
Definition memory.h:71
#define A2W(lpa)
Definition memory.h:138
char * strnewdup(const char *s, size_t minAllocSize=0)
Definition memory.cpp:25
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
std::string tstring
Definition defs.h:79
General definitions used by all ObjectWindows programs.
ObjectWindows exception class & function definitions.
Definition of TProfile class.