OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
codepages.cpp
Go to the documentation of this file.
1//
2/// \file codepages.cpp
3/// Utilities for accessing and setting code page information
4//
5// Part of OWLNext - the next generation Object Windows Library
6// Copyright (c) 2014 by Joe Slater
7//
8// For more information, including license details, see
9// http://owlnext.sourceforge.net
10//
11
12#include <owl/pch.h>
13
14#include <owl/codepages.h>
15#include <algorithm>
16#include <iterator>
17
18namespace owl
19{
20
21//
22// Hide the internal helper functions and variables so they are excluded from
23// public view by using an anonymous namespace.
24//
25namespace
26{
27
28 //
29 // Container for the code page list.
30 //
32
33 //
34 // Uses the binary search std routines to find the given code page identifier.
35 //
36 auto FindCodePage_(TCodePages::TCodePageId idCodePage) -> TCodePages::TCodePageList::iterator
37 {
38 return std::lower_bound(begin(CodePageList_), end(CodePageList_), TCodePages::TCodePage{idCodePage, false, tstring()});
39 }
40
41} // anonymous namespace
42
43//
44/// Contructs the code page list thread-safely, used only by GetInstance.
45//
46// Uses ::EnumSystemCodePages to query supported and installed code pages.
47//
48// Reference:
49// http://msdn.microsoft.com/en-us/library/windows/desktop/dd317825.aspx
50// http://msdn.microsoft.com/en-us/library/windows/desktop/dd317809.aspx
51//
52TCodePages::TCodePages()
53{
54 // Build the list of supported code pages sorted.
55 //
57 {
58 auto idCodePage = static_cast<const TCodePages::TCodePageId>(_ttol(codePageString));
59 auto s = tstring{TCodePages::GetCodePageInfoEx(idCodePage).CodePageName};
60 if (s.empty()) s = to_tstring(idCodePage); // Use code page value for empty names
61 CodePageList_.push_back({idCodePage, false, s});
62 return TRUE;
63 };
65 sort(begin(CodePageList_), end(CodePageList_));
66
67 // Update the list of supported code pages to indicate whether installed.
68 //
70 {
71 auto idCodePage = static_cast<const TCodePages::TCodePageId>(_ttol(codePageString));
73 if (found != end(CodePageList_)) found->Installed = true;
74 return TRUE;
75 };
77}
78
79//
80/// Searches the code page list for a code page identifier.
81//
82/// @param[in] idCodePage is the code page identifier to find.
83//
84/// \return the index of the code page list for the identifier or -1 if not found.
85//
87{
88 GetInstance();
90 return (found != end(CodePageList_)) ? static_cast<int>(distance(begin(CodePageList_), found)) : -1;
91}
92
93//
94/// Retrieves the code page list.
95//
96/// \return the code page list.
97//
99{
100 GetInstance();
101 return CodePageList_;
102}
103
104//
105// Define comparison operator overloads needed for std sorting and searching (sorts by code page identifier)
106//
108{ return lhs.CodePageId == rhs.CodePageId; }
109
111{ return lhs.CodePageId != rhs.CodePageId; }
112
114{ return lhs.CodePageId <= rhs.CodePageId; }
115
117{ return lhs.CodePageId >= rhs.CodePageId; }
118
120{ return lhs.CodePageId < rhs.CodePageId; }
121
123{ return lhs.CodePageId > rhs.CodePageId; }
124
125} // owl namespace
static auto GetIndex(TCodePageId idCodePage) -> int
Searches the code page list for a code page identifier.
Definition codepages.cpp:86
struct { TCodePageId CodePageId;bool Installed;tstring Name;} TCodePage
Definition codepages.h:68
std::vector< TCodePage > TCodePageList
Definition codepages.h:69
static auto GetCodePageList() -> const TCodePageList &
Retrieves the code page list.
Definition codepages.cpp:98
uint TCodePageId
Define the code page, code page object, and container types.
Definition codepages.h:67
static auto GetCodePageInfoEx(TCodePageId idCodePage) -> CPINFOEX
Retieves a CPINFOEX structure.
Definition codepages.h:152
Utilities for accessing and setting code page information.
#define _ttol
Definition cygwin.h:65
int operator!=(const TBitSet< T > &bs1, const TBitSet< T > &bs2)
Definition bitset.h:111
int operator==(const TBitSet< T > &bs1, const TBitSet< T > &bs2)
Definition bitset.h:86
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
auto operator<(const TCodePages::TCodePage &lhs, const TCodePages::TCodePage &rhs) -> bool
auto operator>=(const TCodePages::TCodePage &lhs, const TCodePages::TCodePage &rhs) -> bool
std::string tstring
Definition defs.h:79
auto operator>(const TCodePages::TCodePage &lhs, const TCodePages::TCodePage &rhs) -> bool
auto to_tstring(const T &v) -> tstring
Definition defs.h:82
auto operator<=(const TCodePages::TCodePage &lhs, const TCodePages::TCodePage &rhs) -> bool