OWLNext
7.0
Borland's Object Windows Library for the modern age
Toggle main menu visibility
Loading...
Searching...
No Matches
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
15
namespace
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
//
22
TProfile::TProfile
(LPCTSTR section, LPCTSTR filename)
23
{
24
Init
(section, filename);
25
}
26
27
//
28
/// String-aware overload
29
//
30
TProfile::TProfile
(
const
tstring
& section,
const
tstring
& filename)
31
{
32
Init
(
33
section.empty() ?
nullptr
: section.c_str(),
34
filename.empty() ?
nullptr
: filename.c_str());
35
}
36
37
void
TProfile::Init
(LPCTSTR section, LPCTSTR filename)
38
{
39
Section
= section ?
strnewdup
(section) :
nullptr
;
40
#if defined(UNICODE)
41
USES_CONVERSION
;
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) {
48
OFSTRUCT ofs;
49
ofs.cBytes =
sizeof
ofs;
50
#if defined(UNICODE)
51
const
auto
fn =
W2A
(filename);
52
FileName
=
strnewdup
(
53
(OpenFile(fn, &ofs, OF_EXIST) == HFILE_ERROR) ? filename :
A2W
(ofs.szPathName)
54
);
55
#else
56
FileName
=
strnewdup
(
57
//JJH
58
#
if
defined(__GNUC__)
59
(OpenFile(filename, &ofs, OF_EXIST) == HFILE_ERROR) ? filename : (LPCTSTR)ofs.szPathName
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
//
75
TProfile::~TProfile
()
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
//
85
int
86
TProfile::GetInt
(LPCTSTR key,
int
defaultInt)
87
{
88
return
FileName
89
? ::GetPrivateProfileInt(
Section
, key, defaultInt,
FileName
)
90
: ::GetProfileInt(
Section
, key, defaultInt);
91
}
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
//
99
bool
100
TProfile::GetString
(LPCTSTR key, LPTSTR buff,
unsigned
buffSize,
101
LPCTSTR defaultString)
102
{
103
return
FileName
104
? ::GetPrivateProfileString(
Section
, key, defaultString?defaultString:
_T
(
""
), buff, buffSize,
FileName
)
105
: ::GetProfileString(
Section
, key, defaultString?defaultString:
_T
(
""
), buff, buffSize);
106
}
107
108
tstring
109
TProfile::GetString
(
const
tstring
& key,
const
tstring
& defaultString)
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
//
136
bool
137
TProfile::WriteInt
(LPCTSTR key,
int
value)
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
//
152
bool
153
TProfile::WriteString
(LPCTSTR key, LPCTSTR str)
154
{
155
return
FileName
156
? ::WritePrivateProfileString(
Section
, key, str,
FileName
)
157
: ::WriteProfileString(
Section
, key, str);
158
}
159
160
//
161
/// Makes sure that all written profile values are flushed to the actual file.
162
//
163
void
164
TProfile::Flush
()
165
{
166
if
(
FileName
)
167
::WritePrivateProfileString(
nullptr
,
nullptr
,
nullptr
,
FileName
);
168
}
169
170
}
// OWL namespace
171
/* ========================================================================== */
owl::TProfile::WriteInt
bool WriteInt(LPCTSTR key, int value)
Looks up the key and replaces its value with the integer value passed (int).
Definition
profile.cpp:137
owl::TProfile::Flush
void Flush()
Makes sure that all written profile values are flushed to the actual file.
Definition
profile.cpp:164
owl::TProfile::GetString
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
owl::TProfile::GetInt
int GetInt(LPCTSTR key, int defaultInt=0)
Looks up and returns the integer value associated with the given string, key.
Definition
profile.cpp:86
owl::TProfile::WriteString
bool WriteString(LPCTSTR key, LPCTSTR str)
Looks up the key and replaces its value with the string value passed (str).
Definition
profile.cpp:153
owl::TProfile::Section
TCHAR * Section
Name of the section to use.
Definition
profile.h:82
owl::TProfile::FileName
TCHAR * FileName
File name of the .INI file.
Definition
profile.h:83
owl::TProfile::Init
void Init(LPCTSTR section, LPCTSTR filename)
Definition
profile.cpp:37
owl::TProfile::~TProfile
~TProfile()
Destroys the TProfile object.
Definition
profile.cpp:75
owl::TProfile::TProfile
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
_itot
#define _itot
Definition
cygwin.h:66
_T
#define _T(x)
Definition
cygwin.h:51
owl::TXOwl::TXOwl
TXOwl(const tstring &msg, uint resId=0)
An OWL exception with a given message for displaying and an unsigned Id that can be used for identifi...
Definition
except.cpp:123
pch.h
memory.h
Reliable platform independent header for common memory and string functions.
W2A
#define W2A(lpw)
Definition
memory.h:166
USES_CONVERSION
#define USES_CONVERSION
Definition
memory.h:71
A2W
#define A2W(lpa)
Definition
memory.h:138
strnewdup
char * strnewdup(const char *s, size_t minAllocSize=0)
Definition
memory.cpp:25
owl
Object Windows Library (OWLNext Core).
Definition
animctrl.h:22
owl::tchar
char tchar
Definition
defs.h:77
owl::tstring
std::string tstring
Definition
defs.h:79
defs.h
General definitions used by all ObjectWindows programs.
except.h
ObjectWindows exception class & function definitions.
profile.h
Definition of TProfile class.
source
owlcore
profile.cpp
Generated by
1.18.0