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
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
17namespace owl
18{
19
20//
21// Hide the internal helper functions and variables so they are excluded from
22// public view by using an anonymous namespace.
23//
24namespace
25{
26
27 //
28 // Container for the code page list.
29 //
31
32 //
33 // Uses the binary search std routines to find the given code page identifier.
34 //
35 auto FindCodePage_(TCodePages::TCodePageId idCodePage) -> TCodePages::TCodePageList::iterator
36 {
37 return std::lower_bound(begin(CodePageList_), end(CodePageList_), TCodePages::TCodePage{idCodePage, false, tstring()});
38 }
39
40} // anonymous namespace
41
42//
43/// Contructs the code page list thread-safely, used only by GetInstance.
44//
45// Uses ::EnumSystemCodePages to query supported and installed code pages.
46//
47// Reference:
48// http://msdn.microsoft.com/en-us/library/windows/desktop/dd317825.aspx
49// http://msdn.microsoft.com/en-us/library/windows/desktop/dd317809.aspx
50//
51TCodePages::TCodePages()
52{
53 struct TLocal
54 {
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 }
64
66 {
67 auto idCodePage = static_cast<const TCodePages::TCodePageId>(_ttol(codePageString));
69 if (found != end(CodePageList_)) found->Installed = true;
70 return TRUE;
71 }
72
73 };
74
75 // Build the list of supported code pages sorted.
76 //
77 ::EnumSystemCodePages(&TLocal::AddSupported, CP_SUPPORTED);
78 sort(begin(CodePageList_), end(CodePageList_));
79
80 // Update the list of supported code pages to indicate whether installed.
81 //
82 ::EnumSystemCodePages(&TLocal::InitInstalled, CP_INSTALLED);
83}
84
85//
86/// Searches the code page list for a code page identifier.
87//
88/// @param[in] idCodePage is the code page identifier to find.
89//
90/// \return the index of the code page list for the identifier or -1 if not found.
91//
93{
94 GetInstance();
96 return (found != end(CodePageList_)) ? static_cast<int>(distance(begin(CodePageList_), found)) : -1;
97}
98
99//
100/// Retrieves the code page list.
101//
102/// \return the code page list.
103//
105{
106 GetInstance();
107 return CodePageList_;
108}
109
110//
111// Define comparison operator overloads needed for std sorting and searching (sorts by code page identifier)
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
126{ return lhs.CodePageId < rhs.CodePageId; }
127
129{ return lhs.CodePageId > rhs.CodePageId; }
130
131} // owl namespace
static auto GetIndex(TCodePageId idCodePage) -> int
Searches the code page list for a code page identifier.
Definition codepages.cpp:92
struct { TCodePageId CodePageId;bool Installed;tstring Name;} TCodePage
Definition codepages.h:69
std::vector< TCodePage > TCodePageList
Definition codepages.h:70
static auto GetCodePageList() -> const TCodePageList &
Retrieves the code page list.
uint TCodePageId
Define the code page, code page object, and container types.
Definition codepages.h:68
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