MCPcopy Create free account
hub / github.com/InteractiveComputerGraphics/SPlisHSPlasH / AddFiltersToDialog

Function AddFiltersToDialog

extern/nfd/nfd_win.cpp:81–176  ·  view source on GitHub ↗

helper functions */

Source from the content-addressed store, hash-verified

79
80/* helper functions */
81nfdresult_t AddFiltersToDialog(::IFileDialog* fileOpenDialog,
82 const nfdnfilteritem_t* filterList,
83 nfdfiltersize_t filterCount) {
84 /* filterCount plus 1 because we hardcode the *.* wildcard after the while loop */
85 COMDLG_FILTERSPEC* specList =
86 NFDi_Malloc<COMDLG_FILTERSPEC>(sizeof(COMDLG_FILTERSPEC) * (filterCount + 1));
87 if (!specList) {
88 return NFD_ERROR;
89 }
90
91 /* ad-hoc RAII object to free memory when destructing */
92 struct COMDLG_FILTERSPEC_Guard {
93 COMDLG_FILTERSPEC* _specList;
94 nfdfiltersize_t index;
95 COMDLG_FILTERSPEC_Guard(COMDLG_FILTERSPEC* specList) noexcept
96 : _specList(specList), index(0) {}
97 ~COMDLG_FILTERSPEC_Guard() {
98 for (--index; index != static_cast<nfdfiltersize_t>(-1); --index) {
99 NFDi_Free(const_cast<nfdnchar_t*>(_specList[index].pszSpec));
100 }
101 NFDi_Free(_specList);
102 }
103 };
104
105 COMDLG_FILTERSPEC_Guard specListGuard(specList);
106
107 if (filterCount) {
108 assert(filterList);
109
110 // we have filters to add ... format and add them
111
112 // use the index that comes from the RAII object (instead of making a copy), so the RAII
113 // object will know which memory to free
114 nfdfiltersize_t& index = specListGuard.index;
115
116 for (; index != filterCount; ++index) {
117 // set the friendly name of this filter
118 specList[index].pszName = filterList[index].name;
119
120 // set the specification of this filter...
121
122 // count number of file extensions
123 size_t sep = 1;
124 for (const nfdnchar_t* p_spec = filterList[index].spec; *p_spec; ++p_spec) {
125 if (*p_spec == L',') {
126 ++sep;
127 }
128 }
129
130 // calculate space needed (including the trailing '\0')
131 size_t specSize = sep * 2 + wcslen(filterList[index].spec) + 1;
132
133 // malloc the required memory and populate it
134 nfdnchar_t* specBuf = NFDi_Malloc<nfdnchar_t>(sizeof(nfdnchar_t) * specSize);
135
136 if (!specBuf) {
137 // automatic freeing of memory via COMDLG_FILTERSPEC_Guard
138 return NFD_ERROR;

Callers 3

NFD_OpenDialogNFunction · 0.70
NFD_OpenDialogMultipleNFunction · 0.70
NFD_SaveDialogNFunction · 0.70

Calls 1

NFDi_SetErrorFunction · 0.70

Tested by

no test coverage detected