Skip to content

Commit 9ea03f3

Browse files
committed
- started to adapt for unicode filenames to be displayed in menu list
- removed trailing whitespaces - added workaround to load unicode scripts via the short name - used char(UTF8) to store config file on disk, due to issue with TCHAR, see https://www.codeproject.com/Articles/38242/Reading-UTF-with-C-streams
1 parent ac02c1a commit 9ea03f3

File tree

11 files changed

+430
-398
lines changed

11 files changed

+430
-398
lines changed

PythonScript/src/ConfigFile.cpp

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ ConfigFile::ConfigFile(const TCHAR *configDir, const TCHAR *pluginDir, HINSTANCE
2222
m_configDir(configDir)
2323
{
2424
m_configFilename.append(_T("\\PythonScriptStartup.cnf"));
25-
25+
2626
m_machineScriptsDir.append(_T("\\PythonScript\\scripts"));
2727
m_userScriptsDir.append(_T("\\PythonScript\\scripts"));
2828

@@ -33,61 +33,63 @@ ConfigFile::ConfigFile(const TCHAR *configDir, const TCHAR *pluginDir, HINSTANCE
3333
ConfigFile::~ConfigFile()
3434
{
3535
m_hInst = NULL;
36-
// TODO: Clean up
36+
// TODO: Clean up
3737
// DeleteImage
38-
//
39-
38+
//
39+
4040
}
4141

4242

4343
void ConfigFile::readConfig()
4444
{
45-
std::basic_ifstream<TCHAR> startupFile(m_configFilename.c_str());
46-
47-
TCHAR buffer[500];
48-
49-
45+
//just char(UTF8) as TCHAR is not working as expected, because stream is converted to char implicitly
46+
//see also https://www.codeproject.com/Articles/38242/Reading-UTF-with-C-streams
47+
std::ifstream startupFile(m_configFilename.c_str());
48+
49+
char buffer[500];
50+
51+
5052
HBITMAP hIcon;
5153

5254
while (startupFile.good())
5355
{
5456
startupFile.getline(buffer, 500);
55-
TCHAR *context;
56-
TCHAR *element = _tcstok_s(buffer, _T("/"), &context);
57+
char *context;
58+
char *element = strtok_s(buffer, "/", &context);
5759
if (element)
5860
{
5961

6062
// Menu item
61-
if (0 == _tcscmp(element, _T("ITEM")))
63+
if (0 == strcmp(element, "ITEM"))
6264
{
63-
element = _tcstok_s(NULL, _T("/"), &context);
64-
m_menuItems.push_back(tstring(element));
65-
m_menuScripts.push_back(std::string(WcharMbcsConverter::tchar2char(element).get()));
65+
element = strtok_s(NULL, "/", &context);
66+
m_menuItems.push_back(tstring(WcharMbcsConverter::char2tchar(element).get()));
67+
m_menuScripts.push_back(tstring(WcharMbcsConverter::char2tchar(element).get()));
6668
}
67-
69+
6870
// Toolbar item
69-
else if (0 == _tcscmp(element, _T("TOOLBAR")))
71+
else if (0 == strcmp(element, "TOOLBAR"))
7072
{
71-
element = _tcstok_s(NULL, _T("/"), &context);
72-
TCHAR *iconPath = _tcstok_s(NULL, _T("/"), &context);
73+
element = strtok_s(NULL, "/", &context);
74+
char *iconPath = strtok_s(NULL, "/", &context);
7375
if (!iconPath || !(*iconPath))
7476
{
7577
hIcon = static_cast<HBITMAP>(LoadImage(m_hInst, MAKEINTRESOURCE(IDB_PYTHON), IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE));
7678
iconPath = NULL;
7779
}
78-
else
80+
else
7981
{
80-
hIcon = static_cast<HBITMAP>(LoadImage(NULL, iconPath, IMAGE_BITMAP, 16, 16, LR_LOADMAP3DCOLORS | LR_LOADFROMFILE));
82+
hIcon = static_cast<HBITMAP>(LoadImage(NULL, WcharMbcsConverter::char2tchar(iconPath).get(), IMAGE_BITMAP, 16, 16, LR_LOADMAP3DCOLORS | LR_LOADFROMFILE));
8183
}
8284

83-
84-
m_toolbarItems.push_back(std::pair<tstring, std::pair<HBITMAP, tstring> >(tstring(element), std::pair<HBITMAP, tstring>(hIcon, iconPath ? tstring(iconPath) : tstring())));
85+
86+
m_toolbarItems.push_back(std::pair<tstring, std::pair<HBITMAP, tstring> >(tstring(WcharMbcsConverter::char2tchar(element).get()), std::pair<HBITMAP, tstring>(hIcon, iconPath ? tstring(WcharMbcsConverter::char2tchar(iconPath).get()) : tstring())));
8587
}
86-
else if (0 == _tcscmp(element, _T("SETTING")))
88+
else if (0 == strcmp(element, "SETTING"))
8789
{
88-
element = _tcstok_s(NULL, _T("/"), &context);
89-
TCHAR *settingValue = _tcstok_s(NULL, _T("/"), &context);
90-
m_settings.insert(std::pair<tstring, tstring>(tstring(element), tstring(settingValue)));
90+
element = strtok_s(NULL, "/", &context);
91+
char *settingValue = strtok_s(NULL, "/", &context);
92+
m_settings.insert(std::pair<tstring, tstring>(tstring(WcharMbcsConverter::char2tchar(element).get()), tstring(WcharMbcsConverter::char2tchar(settingValue).get())));
9193
}
9294
}
9395

@@ -104,20 +106,22 @@ void ConfigFile::clearItems()
104106

105107
void ConfigFile::save()
106108
{
107-
std::basic_ofstream<TCHAR> startupFile(m_configFilename.c_str(), std::ios_base::out | std::ios_base::trunc);
109+
//just char(UTF8) as TCHAR is not working as expected, because stream is converted to char implicitly
110+
//see also https://www.codeproject.com/Articles/38242/Reading-UTF-with-C-streams
111+
std::ofstream startupFile(m_configFilename.c_str(), std::ios_base::out | std::ios_base::trunc);
108112
for(MenuItemsTD::iterator it = m_menuItems.begin(); it != m_menuItems.end(); ++it)
109113
{
110-
startupFile << "ITEM/" << (*it) << "\n";
114+
startupFile << "ITEM/" << WcharMbcsConverter::tchar2char((*it).c_str()).get() << "\n";
111115
}
112116

113117
for(ToolbarItemsTD::iterator it = m_toolbarItems.begin(); it != m_toolbarItems.end(); ++it)
114118
{
115-
startupFile << _T("TOOLBAR/") << it->first << _T("/") << it->second.second << _T("\n");
119+
startupFile << "TOOLBAR/" << WcharMbcsConverter::tchar2char((it->first).c_str()).get() << "/" << WcharMbcsConverter::tchar2char((it->second.second).c_str()).get() << "\n";
116120
}
117121

118122
for(SettingsTD::iterator it = m_settings.begin(); it != m_settings.end(); ++it)
119123
{
120-
startupFile << _T("SETTING/") << it->first << _T("/") << it->second << _T("\n");
124+
startupFile << "SETTING/" << WcharMbcsConverter::tchar2char((it->first).c_str()).get() << "/" << WcharMbcsConverter::tchar2char((it->second).c_str()).get() << "\n";
121125
}
122126

123127
startupFile.close();
@@ -128,7 +132,7 @@ void ConfigFile::save()
128132
void ConfigFile::addMenuItem(const tstring scriptPath)
129133
{
130134
m_menuItems.push_back(scriptPath);
131-
m_menuScripts.push_back(std::string(WcharMbcsConverter::tchar2char(scriptPath.c_str()).get()));
135+
m_menuScripts.push_back(scriptPath);
132136
}
133137

134138
void ConfigFile::addToolbarItem(const tstring scriptPath, const tstring iconPath)
@@ -147,11 +151,11 @@ const tstring& ConfigFile::getSetting(const TCHAR *settingName)
147151
return m_settings[tstring(settingName)];
148152
}
149153

150-
const std::string& ConfigFile::getMenuScript(idx_t index) const
151-
{
154+
const tstring& ConfigFile::getMenuScript(idx_t index) const
155+
{
152156
if (m_menuScripts.size() > index)
153157
{
154-
return m_menuScripts[index];
158+
return m_menuScripts[index];
155159
}
156160
else
157161
{

PythonScript/src/ConfigFile.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#ifndef _CONFIGFILE_H
1+
#ifndef _CONFIGFILE_H
22
#define _CONFIGFILE_H
33

44
class ConfigFile
@@ -14,13 +14,13 @@ class ConfigFile
1414
static ConfigFile* create(const TCHAR *configDir, const TCHAR *pluginDir, HINSTANCE hInst);
1515
static ConfigFile* getInstance() { return s_instance; };
1616

17-
17+
1818

1919
// TODO: Need to make these pointers
2020
MenuItemsTD getMenuItems() { return m_menuItems; };
2121
ToolbarItemsTD getToolbarItems() { return m_toolbarItems; };
22-
const std::string& getMenuScript(idx_t index) const;
23-
22+
const tstring& getMenuScript(idx_t index) const;
23+
2424
void addMenuItem(const tstring scriptPath);
2525
void addToolbarItem(const tstring scriptPath, const tstring iconPath);
2626
void setSetting(const tstring& settingName, const tstring settingValue);
@@ -30,7 +30,7 @@ class ConfigFile
3030
void save();
3131

3232
void refresh() { clearItems(); readConfig(); };
33-
33+
3434

3535
const tstring& getMachineScriptsDir() { return m_machineScriptsDir; };
3636
const tstring& getUserScriptsDir() { return m_userScriptsDir; };
@@ -53,13 +53,13 @@ class ConfigFile
5353
tstring m_configDir;
5454

5555
MenuItemsTD m_menuItems;
56-
std::vector< std::string > m_menuScripts;
57-
56+
std::vector< tstring > m_menuScripts;
57+
5858
// Used in case an invalid script number is requested
5959
// so we can return a reference to this puppy instead.
60-
std::string m_emptyString;
61-
62-
60+
tstring m_emptyString;
61+
62+
6363
ToolbarItemsTD m_toolbarItems;
6464
std::map< tstring, HICON > m_icons;
6565
SettingsTD m_settings;

0 commit comments

Comments
 (0)