DXGL r721 - Code Review

Jump to navigation Jump to search
Repository:DXGL
Revision:r720‎ | r721 | r722 >
Date:05:58, 9 July 2017
Author:admin
Status:new
Tags:
Comment:
Create stub to read dxgl.ini in application folder.
Add proposed PR #58 to inih - see github.com/benhoyt/inih/pull/58
Modified paths:
  • /cfgmgr/cfgmgr.c (modified) (history)
  • /cfgmgr/cfgmgr.vcxproj (modified) (history)
  • /cfgmgr/cfgmgr.vcxproj.filters (modified) (history)
  • /cfgmgr/inih/ini.c (modified) (history)
  • /cfgmgr/inih/ini.h (modified) (history)

Diff [purge]

Index: cfgmgr/cfgmgr.c
@@ -32,6 +32,7 @@
3333 #include <tchar.h>
3434 #include <math.h>
3535 #include <stdarg.h>
 36+#include "inih/ini.h"
3637
3738 TCHAR regkeyglobal[] = _T("Software\\DXGL\\Global");
3839 TCHAR regkeybase[] = _T("Software\\DXGL\\");
@@ -756,6 +757,36 @@
757758 return newregname;
758759 }
759760
 761+void GetDefaultConfig(DXGLCFG *cfg)
 762+{
 763+ ZeroMemory(cfg, sizeof(DXGLCFG));
 764+ cfg->DPIScale = 1;
 765+ cfg->AddModes = 1;
 766+}
 767+
 768+void ReadINICallback(DXGLCFG *cfg, const char *section, const char *name,
 769+ const char *value)
 770+{
 771+ // Macro based on example from the inih README.md
 772+ #define MATCH(s, n) stricmp(section, s) == 0 && stricmp(name, n) == 0
 773+ return;
 774+}
 775+
 776+#ifdef UNICODE
 777+#define INIPARSE ini_parse_unicode
 778+#else
 779+#define INIPARSE ini_parse
 780+#endif
 781+
 782+void ReadINI(DXGLCFG *cfg, BOOL retry)
 783+{
 784+ TCHAR inipath[MAX_PATH + 10];
 785+ GetModuleFileName(NULL, inipath, MAX_PATH);
 786+ GetDirFromPath(inipath);
 787+ _tcscat(inipath, _T("\\dxgl.ini"));
 788+ INIPARSE(inipath, ReadINICallback, cfg);
 789+}
 790+
760791 void GetCurrentConfig(DXGLCFG *cfg, BOOL initial)
761792 {
762793 HKEY hKey;
@@ -790,6 +821,7 @@
791822 sha256string[256 / 4] = 0;
792823 _tcscat(regkey,sha256string);
793824 GetGlobalConfig(cfg, initial);
 825+ ReadINI(cfg, FALSE);
794826 if (initial) RegOpenKeyEx(HKEY_CURRENT_USER, regkey, 0, KEY_READ, &hKey);
795827 else RegCreateKeyEx(HKEY_CURRENT_USER,regkey,0,NULL,0,KEY_ALL_ACCESS,NULL,&hKey,NULL);
796828 if (hKey)
Index: cfgmgr/cfgmgr.vcxproj
@@ -149,11 +149,13 @@
150150 <ItemGroup>
151151 <ClInclude Include="cfgmgr.h" />
152152 <ClInclude Include="crc32.h" />
 153+ <ClInclude Include="inih\ini.h" />
153154 <ClInclude Include="LibSha256.h" />
154155 </ItemGroup>
155156 <ItemGroup>
156157 <ClCompile Include="cfgmgr.c" />
157158 <ClCompile Include="crc32.c" />
 159+ <ClCompile Include="inih\ini.c" />
158160 <ClCompile Include="LibSha256.c" />
159161 </ItemGroup>
160162 <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
Index: cfgmgr/cfgmgr.vcxproj.filters
@@ -13,6 +13,12 @@
1414 <UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
1515 <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
1616 </Filter>
 17+ <Filter Include="Source Files\inih">
 18+ <UniqueIdentifier>{52f34bb7-dc10-4142-b67d-6720e9be7d4c}</UniqueIdentifier>
 19+ </Filter>
 20+ <Filter Include="Header Files\inih">
 21+ <UniqueIdentifier>{e547f87f-ed45-4367-8baf-ad2ddbbd5100}</UniqueIdentifier>
 22+ </Filter>
1723 </ItemGroup>
1824 <ItemGroup>
1925 <None Include="ReadMe.txt" />
@@ -27,6 +33,9 @@
2834 <ClInclude Include="LibSha256.h">
2935 <Filter>Header Files</Filter>
3036 </ClInclude>
 37+ <ClInclude Include="inih\ini.h">
 38+ <Filter>Header Files\inih</Filter>
 39+ </ClInclude>
3140 </ItemGroup>
3241 <ItemGroup>
3342 <ClCompile Include="crc32.c">
@@ -38,5 +47,8 @@
3948 <ClCompile Include="LibSha256.c">
4049 <Filter>Source Files</Filter>
4150 </ClCompile>
 51+ <ClCompile Include="inih\ini.c">
 52+ <Filter>Source Files\inih</Filter>
 53+ </ClCompile>
4254 </ItemGroup>
4355 </Project>
\ No newline at end of file
Index: cfgmgr/inih/ini.c
@@ -206,6 +206,22 @@
207207 return error;
208208 }
209209
 210+#if INI_ENABLE_UNICODE
 211+/* See documentation in header file. */
 212+int ini_parse_unicode(const wchar_t* filename, ini_handler handler, void* user)
 213+{
 214+ FILE* file;
 215+ int error;
 216+
 217+ file = _wfopen(filename, L"r");
 218+ if (!file)
 219+ return -1;
 220+ error = ini_parse_file(file, handler, user);
 221+ fclose(file);
 222+ return error;
 223+}
 224+#endif
 225+
210226 /* An ini_reader function to read the next line from a string buffer. This
211227 is the fgets() equivalent used by ini_parse_string(). */
212228 static char* ini_reader_string(char* str, int num, void* stream) {
Index: cfgmgr/inih/ini.h
@@ -22,6 +22,11 @@
2323 #define INI_HANDLER_LINENO 0
2424 #endif
2525
 26+/* Nonzero to compile ini_parse_unicode, zero to skip */
 27+#ifndef INI_ENABLE_UNICODE
 28+#define INI_ENABLE_UNICODE 1
 29+#endif
 30+
2631 /* Typedef for prototype of handler function. */
2732 #if INI_HANDLER_LINENO
2833 typedef int (*ini_handler)(void* user, const char* section,
@@ -50,6 +55,26 @@
5156 */
5257 int ini_parse(const char* filename, ini_handler handler, void* user);
5358
 59+/* Parse given INI-style file. May have [section]s, name=value pairs
 60+ (whitespace stripped), and comments starting with ';' (semicolon). Section
 61+ is "" if name=value pair parsed before any section heading. name:value
 62+ pairs are also supported as a concession to Python's configparser.
 63+
 64+ For each name=value pair parsed, call handler function with given user
 65+ pointer as well as section, name, and value (data only valid for duration
 66+ of handler call). Handler should return nonzero on success, zero on error.
 67+
 68+ Returns 0 on success, line number of first error on parse error (doesn't
 69+ stop on first error), -1 on file open error, or -2 on memory allocation
 70+ error (only when INI_USE_STACK is zero).
 71+
 72+ This version of the function loads an INI-style file from a Unicode path;
 73+ however the callback still uses ANSI style strings.
 74+*/
 75+#if INI_ENABLE_UNICODE
 76+int ini_parse_unicode(const wchar_t* filename, ini_handler handler, void* user);
 77+#endif
 78+
5479 /* Same as ini_parse(), but takes a FILE* instead of filename. This doesn't
5580 close the file when it's finished -- the caller must do that. */
5681 int ini_parse_file(FILE* file, ini_handler handler, void* user);