Branimir Karadžić 9 лет назад
Родитель
Сommit
b5f66e8428

+ 1 - 0
3rdparty/ocornut-imgui/imgui.h

@@ -887,6 +887,7 @@ public:
     {
         if (new_capacity <= Capacity) return;
         T* new_data = (value_type*)ImGui::MemAlloc((size_t)new_capacity * sizeof(value_type));
+        memset(&new_data[Size], 0, (size_t)(new_capacity - Size) * sizeof(value_type)); // BK - clear garbage so that 0 initialized ImString works properly.
         memcpy(new_data, Data, (size_t)Size * sizeof(value_type));
         ImGui::MemFree(Data);
         Data = new_data;

+ 35 - 9
3rdparty/ocornut-imgui/imgui_user.h

@@ -1,17 +1,43 @@
+#include <stdint.h>
+
 namespace ImGui
 {
     struct Font
-	{
-		enum Enum
-		{
-			Regular,
-			Mono,
+    {
+        enum Enum
+        {
+            Regular,
+            Mono,
+
+            Count
+        };
+    };
+
+    void PushFont(Font::Enum _font);
+
+    // BK - simple string class for convenience.
+    class ImString
+    {
+    public:
+        ImString();
+        ImString(const ImString& rhs);
+        ImString(const char* rhs);
+        ~ImString();
+
+        ImString& operator=(const ImString& rhs);
+        ImString& operator=(const char* rhs);
+
+        void Clear();
+        bool IsEmpty() const;
 
-			Count
-		};
-	};
+        const char* CStr() const
+        {
+            return NULL == Ptr ? "" : Ptr;
+        }
 
-	void PushFont(Font::Enum _font);
+    private:
+        char* Ptr;
+    };
 
 } // namespace ImGui
 

+ 74 - 0
3rdparty/ocornut-imgui/imgui_user.inl

@@ -1,2 +1,76 @@
+namespace ImGui
+{
+    ImString::ImString()
+        : Ptr(NULL)
+    {
+    }
+
+    ImString::ImString(const ImString& rhs)
+        : Ptr(NULL)
+    {
+        if (NULL != rhs.Ptr
+            && 0 != strcmp(rhs.Ptr, ""))
+        {
+            Ptr = ImStrdup(rhs.Ptr);
+        }
+    }
+
+    ImString::ImString(const char* rhs)
+        : Ptr(NULL)
+    {
+        if (NULL != rhs
+            && 0 != strcmp(rhs, ""))
+        {
+            Ptr = ImStrdup(rhs);
+        }
+    }
+
+    ImString::~ImString()
+    {
+        Clear();
+    }
+
+    ImString& ImString::operator=(const ImString& rhs)
+    {
+        if (this != &rhs)
+        {
+            *this = rhs.Ptr;
+        }
+
+        return *this;
+    }
+
+    ImString& ImString::operator=(const char* rhs)
+    {
+        if (Ptr != rhs)
+        {
+            Clear();
+
+            if (NULL != rhs
+                && 0 != strcmp(rhs, ""))
+            {
+                Ptr = ImStrdup(rhs);
+            }
+        }
+
+        return *this;
+    }
+
+    void ImString::Clear()
+    {
+        if (NULL != Ptr)
+        {
+            MemFree(Ptr);
+            Ptr = NULL;
+        }
+    }
+
+    bool ImString::IsEmpty() const
+    {
+        return NULL == Ptr;
+    }
+} // namespace
+
+
 #include "widgets/file_list.inl"
 #include "widgets/memory_editor.inl"

+ 3 - 5
3rdparty/ocornut-imgui/widgets/file_list.h

@@ -1,20 +1,18 @@
-#include <stdint.h>
-
 namespace ImGui
 {
 	struct ImFileInfo
 	{
 		ImFileInfo(const char* name, int64_t size);
 		~ImFileInfo();
-		ImFileInfo& operator=(const ImFileInfo& rhs);
 
-		char* Name;
+		ImString Name;
 		int64_t Size;
 	};
 
 	struct ImFileList
 	{
-		ImVector<ImFileInfo> FileList;
+		typedef ImVector<ImFileInfo> FileInfoArray;
+		FileInfoArray FileList;
 		int Pos;
 
 		ImFileList(const char* path = ".")

+ 10 - 26
3rdparty/ocornut-imgui/widgets/file_list.inl

@@ -4,26 +4,13 @@
 namespace ImGui
 {
 	ImFileInfo::ImFileInfo(const char* name, int64_t size)
+		: Name(name)
+		, Size(size)
 	{
-		Name = ImStrdup(name);
-		Size = size;
 	}
 
 	ImFileInfo::~ImFileInfo()
 	{
-		MemFree(Name);
-		Name = NULL;
-	}
-
-	ImFileInfo& ImFileInfo::operator=(const ImFileInfo& rhs)
-	{
-		if (this != &rhs)
-		{
-			Name = ImStrdup(rhs.Name);
-			Size = rhs.Size;
-		}
-
-		return *this;
 	}
 
 	void ImFileList::ChDir(const char* path)
@@ -68,11 +55,11 @@ namespace ImGui
 		{
 			const float lineHeight = GetTextLineHeightWithSpacing();
 
-			char* chdir = NULL;
+			ImString chdir;
 
 			int pos = 0;
 			ImGuiListClipper clipper(FileList.size(), lineHeight);
-			for (ImVector<ImFileInfo>::const_iterator it = FileList.begin(), itEnd = FileList.end()
+			for (FileInfoArray::const_iterator it = FileList.begin(), itEnd = FileList.end()
 				; it != itEnd
 				; ++it
 				)
@@ -85,7 +72,7 @@ namespace ImGui
 					const bool isDir = -1 == it->Size;
 					bool isSelected  = Pos == pos;
 
-					bool clicked = Selectable(it->Name, &isSelected, 0, ImVec2(150.0f, 0.0f) );
+					bool clicked = Selectable(it->Name.CStr(), &isSelected, 0, ImVec2(150.0f, 0.0f) );
 					SameLine();
 					if (isDir)
 					{
@@ -98,18 +85,16 @@ namespace ImGui
 
 					if (clicked)
 					{
-						if (0 == ImStricmp(it->Name, "..") )
+						if (0 == strcmp(it->Name.CStr(), "..") )
 						{
-							MemFree(chdir);
-							chdir = ImStrdup(it->Name);
+							chdir = it->Name;
 						}
 
 						Pos = pos;
 
 						if (isDir)
 						{
-							MemFree(chdir);
-							chdir = ImStrdup(it->Name);
+							chdir = it->Name;
 						}
 					}
 
@@ -121,10 +106,9 @@ namespace ImGui
 
 			ListBoxFooter();
 
-			if (NULL != chdir)
+			if (!chdir.IsEmpty() )
 			{
-				ChDir(chdir);
-				MemFree(chdir);
+				ChDir(chdir.CStr() );
 			}
 		}