From 83f9f0f1bdf62122abb88798cd2b148646f1ee6d Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Sun, 3 Dec 2017 20:27:08 +1030 Subject: [PATCH 09/13] UTF: replace gmalloc/gfree with new/delete --- goo/UTF.cc | 7 +++---- goo/UTF.h | 6 +++--- goo/gfile.cc | 8 ++++---- poppler/PDFDoc.cc | 2 +- qt5/tests/check_utf_conversion.cpp | 4 ++-- utils/Win32Console.cc | 2 +- 6 files changed, 14 insertions(+), 15 deletions(-) diff --git a/goo/UTF.cc b/goo/UTF.cc index 2685bd06..65422660 100644 --- a/goo/UTF.cc +++ b/goo/UTF.cc @@ -24,7 +24,6 @@ // //======================================================================== -#include "gmem.h" #include "UTF.h" bool UnicodeIsValid(Unicode ucs4) @@ -52,7 +51,7 @@ int UTF16toUCS4(const Unicode *utf16, int utf16Len, Unicode **ucs4) if (ucs4 == NULL) return len; - u = (Unicode*)gmallocn(len, sizeof(Unicode)); + u = new Unicode[len]; n = 0; // convert string for (i = 0; i < utf16Len; i++) { @@ -241,7 +240,7 @@ uint16_t *utf8ToUtf16(const char *utf8, int *len) int n = utf8CountUtf16CodeUnits(utf8); if (len) *len = n; - uint16_t *utf16 = (uint16_t*)gmallocn(n + 1, sizeof(uint16_t)); + uint16_t *utf16 = new uint16_t[n + 1]; utf8ToUtf16(utf8, utf16); return utf16; } @@ -383,7 +382,7 @@ char *utf16ToUtf8(const uint16_t *utf16, int *len) int n = utf16CountUtf8Bytes(utf16); if (len) *len = n; - char *utf8 = (char*)gmalloc(n + 1); + char *utf8 = new char[n + 1]; utf16ToUtf8(utf16, utf8); return utf8; } diff --git a/goo/UTF.h b/goo/UTF.h index 58865a8d..2597bad7 100644 --- a/goo/UTF.h +++ b/goo/UTF.h @@ -20,7 +20,7 @@ // Convert a UTF-16 string to a UCS-4 // utf16 - utf16 bytes // utf16_len - number of UTF-16 characters -// ucs4_out - if not NULL, allocates and returns UCS-4 string. Free with gfree. +// ucs4_out - if not NULL, allocates and returns UCS-4 string. Free with delete. // returns number of UCS-4 characters int UTF16toUCS4(const Unicode *utf16, int utf16_len, Unicode **ucs4_out); @@ -43,7 +43,7 @@ int utf8CountUtf16CodeUnits(const char *utf8); // Returns number of UTF-16 code units written (excluding NULL). int utf8ToUtf16(const char *utf8, uint16_t *utf16, int maxUtf16 = INT_MAX, int maxUtf8 = INT_MAX); -// Allocate utf16 string and convert utf8 into it. +// Allocate utf16 string and convert utf8 into it. Free with delete. uint16_t *utf8ToUtf16(const char *utf8, int *len = nullptr); // Count number of UTF-8 bytes required to convert a UTF-16 string to @@ -60,7 +60,7 @@ int utf16CountUtf8Bytes(const uint16_t *utf16); // Returns number of UTF-8 bytes written (excluding NULL). int utf16ToUtf8(const uint16_t *utf16, char *utf8, int maxUtf8 = INT_MAX, int maxUtf16 = INT_MAX); -// Allocate utf8 string and convert utf16 into it. +// Allocate utf8 string and convert utf16 into it. Free with delete. char *utf16ToUtf8(const uint16_t *utf16, int *len = nullptr); #endif diff --git a/goo/gfile.cc b/goo/gfile.cc index 05ffed81..3e5892c8 100644 --- a/goo/gfile.cc +++ b/goo/gfile.cc @@ -702,7 +702,7 @@ GDirEntry::GDirEntry(char *dirPath, char *nameA, GBool doStat) { #ifdef _WIN32 wchar_t *tmp_w = (wchar_t *)utf8ToUtf16(fullPath->getCString()); fa = GetFileAttributesW(tmp_w); - gfree(tmp_w); + delete tmp_w; dir = (fa != 0xFFFFFFFF && (fa & FILE_ATTRIBUTE_DIRECTORY)); #else if (stat(fullPath->getCString(), &st) == 0) @@ -744,7 +744,7 @@ GDir::GDir(char *name, GBool doStatA) { tmp->append("/*.*"); wchar_t *tmp_w = (wchar_t *)utf8ToUtf16(tmp->getCString()); priv->hnd = FindFirstFileW(tmp_w, &priv->ffd); - gfree(tmp_w); + delete tmp_w; delete tmp; #elif defined(ACORN) #elif defined(MACOS) @@ -778,7 +778,7 @@ GDirEntry *GDir::getNextEntry() { if (priv->hnd != INVALID_HANDLE_VALUE) { char *name = utf16ToUtf8((uint16_t *)priv->ffd.cFileName); e = new GDirEntry(priv->path->getCString(), name, priv->doStat); - gfree(name); + delete name; if (!FindNextFileW(priv->hnd, &priv->ffd)) { FindClose(priv->hnd); priv->hnd = INVALID_HANDLE_VALUE; @@ -825,7 +825,7 @@ void GDir::rewind() { tmp->append("/*.*"); wchar_t *tmp_w = (wchar_t *)utf8ToUtf16(tmp->getCString()); priv->hnd = FindFirstFileW(tmp_w, &priv->ffd); - gfree(tmp_w); + delete tmp_w; delete tmp; #elif defined(ACORN) #elif defined(MACOS) diff --git a/poppler/PDFDoc.cc b/poppler/PDFDoc.cc index 90f039d8..7339762a 100644 --- a/poppler/PDFDoc.cc +++ b/poppler/PDFDoc.cc @@ -158,7 +158,7 @@ PDFDoc::PDFDoc(GooString *fileNameA, GooString *ownerPassword, #ifdef _WIN32 wchar_t *wFileName = (wchar_t*)utf8ToUtf16(fileName->getCString()); file = GooFile::open(wFileName); - gfree(wFileName); + delete wFileName; #else file = GooFile::open(fileName); #endif diff --git a/qt5/tests/check_utf_conversion.cpp b/qt5/tests/check_utf_conversion.cpp index d344434d..fbd66b6d 100644 --- a/qt5/tests/check_utf_conversion.cpp +++ b/qt5/tests/check_utf_conversion.cpp @@ -67,7 +67,7 @@ void TestUTFConversion::testUTF() utf16String = utf8ToUtf16(str); QVERIFY( compare(utf16String, s.utf16()) ); - free (utf16String); + delete utf16String; // UTF-16 to UTF-8 @@ -81,7 +81,7 @@ void TestUTFConversion::testUTF() utf8String = utf16ToUtf8(s.utf16() ); QVERIFY( compare(utf8String, str) ); - free (utf8String); + delete utf8String; free(str); } diff --git a/utils/Win32Console.cc b/utils/Win32Console.cc index 4db92de6..350165a8 100644 --- a/utils/Win32Console.cc +++ b/utils/Win32Console.cc @@ -158,7 +158,7 @@ Win32Console::~Win32Console() flush(true); if (argList) { for (int i = 0; i < numArgs; i++) - gfree(privateArgList[i]); + delete privateArgList[i]; delete[] argList; delete[] privateArgList; } -- 2.11.0