Skip to content

Commit 295abcc

Browse files
committed
[FIX] : fix serialization / deserialization and C Api for Places
1 parent cb94d06 commit 295abcc

2 files changed

Lines changed: 116 additions & 44 deletions

File tree

ImGuiFileDialog.cpp

Lines changed: 87 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,27 @@ bool IGFD::Utils::ReplaceString(std::string& str, const ::std::string& oldStr, c
837837
return false;
838838
}
839839

840+
std::vector<std::string> IGFD::Utils::SplitStringToVector(const std::string& vText, const std::string& vDelimiterPattern, const bool& vPushEmpty) {
841+
std::vector<std::string> arr;
842+
if (!vText.empty()) {
843+
size_t start = 0;
844+
size_t end = vText.find(vDelimiterPattern, start);
845+
while (end != std::string::npos) {
846+
auto token = vText.substr(start, end - start);
847+
if (!token.empty() || (token.empty() && vPushEmpty)) { //-V728
848+
arr.push_back(token);
849+
}
850+
start = end + vDelimiterPattern.size();
851+
end = vText.find(vDelimiterPattern, start);
852+
}
853+
auto token = vText.substr(start);
854+
if (!token.empty() || (token.empty() && vPushEmpty)) { //-V728
855+
arr.push_back(token);
856+
}
857+
}
858+
return arr;
859+
}
860+
840861
std::vector<std::string> IGFD::Utils::SplitStringToVector(const std::string& vText, const char& vDelimiter, const bool& vPushEmpty) {
841862
std::vector<std::string> arr;
842863
if (!vText.empty()) {
@@ -3071,45 +3092,52 @@ bool IGFD::PlacesFeature::m_DrawPlacesPane(FileDialogInternal& vFileDialogIntern
30713092
}
30723093

30733094
std::string IGFD::PlacesFeature::SerializePlaces(const bool& vForceSerialisationForAll) {
3074-
// todo : do the code
30753095
std::string res;
3076-
/*size_t idx = 0;
3077-
for (auto& it : m_Groups) {
3078-
if (vForceSerialisationForAll && it.canBeSaved) continue;
3079-
if (idx++ != 0) res += "##"; // ## because reserved by imgui, so an input text cant have ##
3080-
res += it.name + "##" + it.path;
3081-
}*/
3096+
size_t idx = 0;
3097+
for (const auto& group : m_Groups) {
3098+
if (group.second->canBeSaved) {
3099+
// ## is used because reserved by imgui, so an input text cannot have ##
3100+
res += "###" + group.first + "###";
3101+
for (const auto& place : group.second->places) {
3102+
if (place.canBeSaved) {
3103+
if (idx++ != 0) res += "##";
3104+
res += place.name + "##" + place.path;
3105+
}
3106+
}
3107+
}
3108+
}
30823109
return res;
30833110
}
30843111

30853112
void IGFD::PlacesFeature::DeserializePlaces(const std::string& vPlaces) {
3086-
// todo : do the code
3087-
/*if (!vPlaces.empty()) {
3088-
m_Groups.clear();
3089-
auto arr = IGFD::Utils::SplitStringToVector(vPlaces, '#', false);
3090-
for (size_t i = 0; i < arr.size(); i += 2) {
3091-
if (i + 1 < arr.size()) { // for avoid crash if arr size is impair due to user mistake after edition
3092-
PlaceStruct place;
3093-
place.name = arr[i];
3094-
// if bad format we jump this place
3095-
place.path = arr[i + 1];
3096-
m_Bookmarks.push_back(place);
3113+
if (!vPlaces.empty()) {
3114+
const auto& groups = IGFD::Utils::SplitStringToVector(vPlaces, "###", false);
3115+
if (groups.size() > 1) {
3116+
for (size_t i = 0; i < groups.size(); i += 2) {
3117+
auto group_ptr = GetPlacesGroupPtr(groups[i]);
3118+
if (group_ptr != nullptr) {
3119+
const auto& places = IGFD::Utils::SplitStringToVector(groups[i + 1], "##", false);
3120+
if (places.size() > 1) {
3121+
for (size_t j = 0; j < places.size(); j += 2) {
3122+
group_ptr->AddPlace(places[j], places[j + 1], true); // was saved so we set canBeSaved to true
3123+
}
3124+
}
3125+
}
30973126
}
30983127
}
3099-
}*/
3128+
}
31003129
}
31013130

31023131
bool IGFD::PlacesFeature::AddPlacesGroup(const std::string& vGroupName, const size_t& vDisplayOrder, const bool& vCanBeEdited) {
31033132
if (vGroupName.empty()) {
31043133
return false;
31053134
}
3106-
auto group_ptr = std::make_shared<GroupStruct>();
3107-
group_ptr->displayOrder = vDisplayOrder;
3135+
auto group_ptr = std::make_shared<GroupStruct>();
3136+
group_ptr->displayOrder = vDisplayOrder;
31083137
group_ptr->name = vGroupName;
3109-
group_ptr->canBeEdited = vCanBeEdited;
3138+
group_ptr->canBeSaved = group_ptr->canBeEdited = vCanBeEdited; // can be user edited mean can be saved
31103139
m_Groups[vGroupName] = group_ptr;
3111-
// tofix the set of existing order number
3112-
m_OrderedGroups[group_ptr->displayOrder] = group_ptr;
3140+
m_OrderedGroups[group_ptr->displayOrder] = group_ptr; // an exisitng display order will be overwrote for code simplicity
31133141
return true;
31143142
}
31153143

@@ -3131,7 +3159,10 @@ IGFD::PlacesFeature::GroupStruct* IGFD::PlacesFeature::GetPlacesGroupPtr(const s
31313159
}
31323160

31333161
bool IGFD::PlacesFeature::GroupStruct::AddPlace(const std::string& vPlaceName, const std::string& vPlacePath, const bool& vCanBeSaved, const FileStyle& vStyle) {
3134-
if (vPlaceName.empty() || vPlacePath.empty()) return false;
3162+
if (vPlaceName.empty() || vPlacePath.empty()) {
3163+
return false;
3164+
}
3165+
canBeSaved |= vCanBeSaved; // if one place must be saved so we mark the group to be saved
31353166
PlaceStruct place;
31363167
place.name = vPlaceName;
31373168
place.path = vPlacePath;
@@ -3141,7 +3172,9 @@ bool IGFD::PlacesFeature::GroupStruct::AddPlace(const std::string& vPlaceName, c
31413172
}
31423173

31433174
bool IGFD::PlacesFeature::GroupStruct::RemovePlace(const std::string& vPlaceName) {
3144-
if (vPlaceName.empty()) return false;
3175+
if (vPlaceName.empty()) {
3176+
return false;
3177+
}
31453178
for (auto places_it = places.begin(); places_it != places.end(); ++places_it) {
31463179
if ((*places_it).name == vPlaceName) {
31473180
places.erase(places_it);
@@ -4931,20 +4964,42 @@ IGFD_C_API void IGFD_DeserializePlaces(ImGuiFileDialog* vContextPtr, const char*
49314964
}
49324965
}
49334966

4934-
IGFD_C_API void IGFD_AddPlace(ImGuiFileDialog* vContextPtr, const char* vPlaceName, const char* vPlacePath) {
4967+
IGFD_C_API bool IGFD_AddPlacesGroup(ImGuiFileDialog* vContextPtr, const char* vGroupName, size_t vDisplayOrder, bool vCanBeEdited) {
4968+
if (vContextPtr != nullptr) {
4969+
return vContextPtr->AddPlacesGroup(vGroupName, vDisplayOrder, vCanBeEdited);
4970+
}
4971+
return false;
4972+
}
4973+
4974+
IGFD_C_API bool IGFD_RemovePlacesGroup(ImGuiFileDialog* vContextPtr, const char* vGroupName) {
49354975
if (vContextPtr != nullptr) {
4936-
// todo : do the code
4937-
//vContextPtr->AddPlace(vPlaceName, vPlacePath);
4976+
return vContextPtr->RemovePlacesGroup(vGroupName);
49384977
}
4978+
return false;
49394979
}
49404980

4941-
IGFD_C_API void IGFD_RemovePlace(ImGuiFileDialog* vContextPtr, const char* vPlaceName) {
4981+
IGFD_C_API bool IGFD_AddPlace(ImGuiFileDialog* vContextPtr, const char* vGroupName, const char* vPlaceName, const char* vPlacePath, bool vCanBeSaved, const char* vIconText) {
49424982
if (vContextPtr != nullptr) {
4943-
//todo : do the code
4944-
//vContextPtr->RemovePlace(vPlaceName);
4983+
auto group_ptr = vContextPtr->GetPlacesGroupPtr(vGroupName);
4984+
if (group_ptr != nullptr) {
4985+
IGFD::FileStyle style;
4986+
style.icon = vIconText;
4987+
return group_ptr->AddPlace(vPlaceName, vPlacePath, vCanBeSaved, style);
4988+
}
49454989
}
4990+
return false;
49464991
}
49474992

4993+
IGFD_C_API bool IGFD_RemovePlace(ImGuiFileDialog* vContextPtr, const char* vGroupName, const char* vPlaceName) {
4994+
if (vContextPtr != nullptr) {
4995+
auto group_ptr = vContextPtr->GetPlacesGroupPtr(vGroupName);
4996+
if (group_ptr != nullptr) {
4997+
return group_ptr->RemovePlace(vPlaceName);
4998+
}
4999+
}
5000+
return false;
5001+
}
5002+
49485003
#endif
49495004

49505005
#ifdef USE_THUMBNAILS

ImGuiFileDialog.h

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,6 +1514,7 @@ class IGFD_API Utils {
15141514
static void SetBuffer(char* vBuffer, size_t vBufferLen, const std::string& vStr);
15151515
static std::string UTF8Encode(const std::wstring& wstr);
15161516
static std::wstring UTF8Decode(const std::string& str);
1517+
static std::vector<std::string> SplitStringToVector(const std::string& vText, const std::string& vDelimiterPattern, const bool& vPushEmpty);
15171518
static std::vector<std::string> SplitStringToVector(const std::string& vText, const char& vDelimiter, const bool& vPushEmpty);
15181519
static std::string LowerCaseString(const std::string& vString); // turn all text in lower case for search facilitie
15191520
static size_t GetCharCountInString(const std::string& vString, const char& vChar);
@@ -2038,6 +2039,7 @@ class IGFD_API PlacesFeature {
20382039
};
20392040

20402041
struct GroupStruct {
2042+
bool canBeSaved = false; // defined by code, can be used for prevent serialization / deserialization
20412043
size_t displayOrder = 0U; // the display order will be usedf first, then alphanumeric
20422044
std::string name; // the group name, will be displayed
20432045
std::vector<PlaceStruct> places; // the places (name + path)
@@ -2068,14 +2070,14 @@ class IGFD_API PlacesFeature {
20682070
bool m_DrawPlacesPane(FileDialogInternal& vFileDialogInternal, const ImVec2& vSize); // draw place Pane
20692071

20702072
public:
2071-
std::string SerializePlaces( // serialize place : return place buffer to save in a file
2072-
const bool& vForceSerialisationForAll = true); // for avoid serialization of places with flag canBeSaved to false
2073-
void DeserializePlaces( // deserialize place : load place buffer to load in the dialog (saved from
2074-
const std::string& vPlaces); // previous use with SerializePlaces()) place buffer to load
2073+
std::string SerializePlaces( // serialize place : return place buffer to save in a file
2074+
const bool& vForceSerialisationForAll = true); // for avoid serialization of places with flag canBeSaved to false
2075+
void DeserializePlaces( // deserialize place : load place buffer to load in the dialog (saved from
2076+
const std::string& vPlaces); // previous use with SerializePlaces()) place buffer to load
20752077
bool AddPlacesGroup( // add a group
2076-
const std::string& vGroupName, // the group name
2077-
const size_t& vDisplayOrder, // the display roder of the group
2078-
const bool& vCanBeEdited); // let the user add/remove place in the group
2078+
const std::string& vGroupName, // the group name
2079+
const size_t& vDisplayOrder, // the display roder of the group
2080+
const bool& vCanBeEdited); // let the user add/remove place in the group
20792081
bool RemovePlacesGroup(const std::string& vGroupName); // remove the group
20802082
GroupStruct* GetPlacesGroupPtr(const std::string& vGroupName); // get the group, if not existed, will be created
20812083
#endif // USE_PLACES_FEATURE
@@ -2447,14 +2449,29 @@ IGFD_C_API void IGFD_DeserializePlaces( // deserialize place : load bookmar buf
24472449
ImGuiFileDialog* vContextPtr, // ImGuiFileDialog context
24482450
const char* vPlaces); // place buffer to load
24492451

2450-
IGFD_C_API void IGFD_AddPlace( // add a place by code
2452+
IGFD_C_API bool IGFD_AddPlacesGroup( // add a places group by code
2453+
ImGuiFileDialog* vContextPtr, // ImGuiFileDialog context
2454+
const char* vGroupName, // the group name
2455+
size_t vDisplayOrder, // the display roder of the group
2456+
bool vCanBeEdited); // let the user add/remove place in the group
2457+
2458+
IGFD_C_API bool IGFD_RemovePlacesGroup( // remove a place group by code, return true if succeed
2459+
ImGuiFileDialog* vContextPtr, // ImGuiFileDialog context
2460+
const char* vGroupName); // place name to remove
2461+
2462+
IGFD_C_API bool IGFD_AddPlace( // add a place by code
24512463
ImGuiFileDialog* vContextPtr, // ImGuiFileDialog context
2452-
const char* vPlaceName, // place name
2453-
const char* vPlacePath); // place path
2464+
const char* vGroupName, // the group name
2465+
const char* vPlaceName, // place name
2466+
const char* vPlacePath, // place path
2467+
bool vCanBeSaved, // place can be saved
2468+
const char* vIconText); // wanted text or icon of the file with extention filter (can be used with font icon)
24542469

2455-
IGFD_C_API void IGFD_RemovePlace( // remove a place by code, return true if succeed
2456-
ImGuiFileDialog* vContextPtr, // ImGuiFileDialog context
2470+
IGFD_C_API bool IGFD_RemovePlace( // remove a place by code, return true if succeed
2471+
ImGuiFileDialog* vContextPtr, // ImGuiFileDialog context
2472+
const char* vGroupName, // the group name
24572473
const char* vPlaceName); // place name to remove
2474+
24582475
#endif
24592476

24602477
#ifdef USE_THUMBNAILS

0 commit comments

Comments
 (0)