diff --git a/src/common/chapter_writer.cpp b/src/common/chapter_writer.cpp index a0dd8eae2..764ff9288 100644 --- a/src/common/chapter_writer.cpp +++ b/src/common/chapter_writer.cpp @@ -60,14 +60,14 @@ static vector chapter_entries; static void handle_name(int level, - const char *name) { + const string &name) { int i, j; vector::iterator itr; for (i = 0; i < chapter_start_times.size(); i++) { chapter_entry_c &e = chapter_start_times[i]; if (e.level == level) { - chapter_entries.push_back(chapter_entry_c(string(name), e.start, level)); + chapter_entries.push_back(chapter_entry_c(name, e.start, level)); itr = chapter_start_times.begin(); for (j = 0; j < i; j++) itr++; @@ -107,7 +107,7 @@ write_chapter_display_simple(KaxChapterDisplay *display, int level) { int i; EbmlElement *e; - char *s; + string s; for (i = 0; i < display->ListSize(); i++) { e = (*display)[i]; @@ -115,7 +115,6 @@ write_chapter_display_simple(KaxChapterDisplay *display, s = UTFstring_to_cstrutf8(UTFstring(*static_cast (e)).c_str()); handle_name(level - 1, s); - safefree(s); } else if (is_id(e, KaxChapterAtom)) write_chapter_atom_simple((KaxChapterAtom *)e, level + 1); @@ -230,8 +229,7 @@ write_xml_element_rec(int level, EbmlMaster *m; int elt_idx, i; bool found; - char *s; - string x; + string s; elt_idx = parent_idx; found = false; @@ -286,16 +284,15 @@ write_xml_element_rec(int level, break; case ebmlt_string: - x = escape_xml(string(*dynamic_cast(e)).c_str()); - out->printf("%s\n", x.c_str(), chapter_elements[elt_idx].name); + s = escape_xml(string(*dynamic_cast(e))); + out->printf("%s\n", s.c_str(), chapter_elements[elt_idx].name); break; case ebmlt_ustring: s = UTFstring_to_cstrutf8(UTFstring(*static_cast (e)).c_str()); - x = escape_xml(s); - out->printf("%s\n", x.c_str(), chapter_elements[elt_idx].name); - safefree(s); + s = escape_xml(s); + out->printf("%s\n", s.c_str(), chapter_elements[elt_idx].name); break; case ebmlt_time: diff --git a/src/common/common.cpp b/src/common/common.cpp index e76ffb061..a677d88af 100644 --- a/src/common/common.cpp +++ b/src/common/common.cpp @@ -951,10 +951,11 @@ strip(vector &v, } string -escape(const char *src) { +escape(const string &source) { string dst; + string::const_iterator src; - while (*src != 0) { + for (src = source.begin(); src < source.end(); src++) { if (*src == '\\') dst += "\\\\"; else if (*src == '"') @@ -963,21 +964,21 @@ escape(const char *src) { dst += "\\s"; else dst += *src; - src++; } return dst; } string -unescape(const char *src) { +unescape(const string &source) { string dst; - const char *next_char; + string::const_iterator src, next_char; + src = source.begin(); next_char = src + 1; - while (*src != 0) { + while (src != source.end()) { if (*src == '\\') { - if (*next_char == 0) // This is an error... + if (next_char == source.end()) // This is an error... dst += '\\'; else { if (*next_char == '2') @@ -998,10 +999,12 @@ unescape(const char *src) { } string -escape_xml(const char *src) { +escape_xml(const string &source) { string dst; + string::const_iterator src; - while (*src != 0) { + src = source.begin(); + while (src != source.end()) { if (*src == '&') dst += "&"; else if (*src == '>') diff --git a/src/common/common.h b/src/common/common.h index db7b239f5..9f45298ab 100644 --- a/src/common/common.h +++ b/src/common/common.h @@ -219,9 +219,9 @@ vector MTX_DLL_API split(const char *src, const char *pattern = ",", string MTX_DLL_API join(const char *pattern, vector &strings); void MTX_DLL_API strip(string &s, bool newlines = false); void MTX_DLL_API strip(vector &v, bool newlines = false); -string MTX_DLL_API escape(const char *src); -string MTX_DLL_API escape_xml(const char *src); -string MTX_DLL_API unescape(const char *src); +string MTX_DLL_API escape(const string &src); +string MTX_DLL_API escape_xml(const string &src); +string MTX_DLL_API unescape(const string &src); bool MTX_DLL_API starts_with(const string &s, const char *start); bool MTX_DLL_API starts_with(const string &s, const string &start); bool MTX_DLL_API starts_with_case(const string &s, const char *start); diff --git a/src/common/commonebml.cpp b/src/common/commonebml.cpp index 7170cfbf4..2776a5abd 100644 --- a/src/common/commonebml.cpp +++ b/src/common/commonebml.cpp @@ -54,40 +54,31 @@ __debug_dump_elements(EbmlElement *e, */ UTFstring -cstr_to_UTFstring(const char *c) { +cstr_to_UTFstring(const string &c) { + wchar_t *new_string; + UTFstring u; + int len; + + len = c.length(); + new_string = (wchar_t *)safemalloc((len + 1) * sizeof(wchar_t)); #if defined(COMP_MSC) || defined(COMP_MINGW) - wchar_t *new_string; - int len; - UTFstring u; - - len = strlen(c); - new_string = (wchar_t *)safemalloc((len + 1) * sizeof(wchar_t)); MultiByteToWideChar(CP_ACP, 0, c, -1, new_string, len + 1); - u = new_string; - safefree(new_string); - - return u; #else - wchar_t *new_string; char *old_locale; - UTFstring u; - int len; - len = strlen(c); - new_string = (wchar_t *)safemalloc((len + 1) * sizeof(wchar_t)); memset(new_string, 0, (len + 1) * sizeof(wchar_t)); new_string[len] = L'\0'; old_locale = safestrdup(setlocale(LC_CTYPE, NULL)); setlocale(LC_CTYPE, ""); - mbstowcs(new_string, c, len); + mbstowcs(new_string, c.c_str(), len); setlocale(LC_CTYPE, old_locale); safefree(old_locale); u = UTFstring(new_string); +#endif safefree(new_string); return u; -#endif } static int @@ -132,12 +123,12 @@ wchar_to_utf8_byte_length(uint32_t w) { } UTFstring -cstrutf8_to_UTFstring(const char *c) { +cstrutf8_to_UTFstring(const string &c) { wchar_t *new_string; int slen, dlen, src, dst, clen; UTFstring u; - slen = strlen(c); + slen = c.length(); dlen = 0; for (src = 0; src < slen; dlen++) { clen = utf8_byte_length(c[src]); @@ -201,23 +192,22 @@ cstrutf8_to_UTFstring(const char *c) { return u; } -char * +string UTFstring_to_cstr(const UTFstring &u) { -#if defined(COMP_MSC) || defined(COMP_MINGW) + string retval; char *new_string; int len; + +#if defined(COMP_MSC) || defined(COMP_MINGW) BOOL dummy; len = u.length(); new_string = (char *)safemalloc(len + 1); WideCharToMultiByte(CP_ACP, 0, u.c_str(), -1, new_string, len + 1, " ", &dummy); - - return new_string; #else const wchar_t *sptr; - char *new_string, *old_locale; - int len; + char *old_locale; len = u.length(); new_string = (char *)safemalloc(len * 4 + 1); @@ -229,16 +219,19 @@ UTFstring_to_cstr(const UTFstring &u) { new_string[len * 4] = 0; setlocale(LC_CTYPE, old_locale); safefree(old_locale); - - return new_string; #endif + retval = new_string; + safefree(new_string); + + return retval; } -char * +string UTFstring_to_cstrutf8(const UTFstring &u) { int src, dst, dlen, slen, clen; unsigned char *new_string; uint32_t uc; + string retval; dlen = 0; slen = u.length(); @@ -246,7 +239,7 @@ UTFstring_to_cstrutf8(const UTFstring &u) { for (src = 0, dlen = 0; src < slen; src++) dlen += wchar_to_utf8_byte_length((uint32_t)u[src]); - new_string = (unsigned char *)malloc(dlen + 1); + new_string = (unsigned char *)safemalloc(dlen + 1); for (src = 0, dst = 0; src < slen; src++) { uc = (uint32_t)u[src]; @@ -291,16 +284,18 @@ UTFstring_to_cstrutf8(const UTFstring &u) { } new_string[dst] = 0; + retval = (char *)new_string; + safefree(new_string); - return (char *)new_string; + return retval; } bool -is_valid_utf8_string(const char *c) { +is_valid_utf8_string(const string &c) { int slen, dlen, src, clen; UTFstring u; - slen = strlen(c); + slen = c.length(); dlen = 0; for (src = 0; src < slen; dlen++) { clen = utf8_byte_length(c[src]); diff --git a/src/common/commonebml.h b/src/common/commonebml.h index 261022a8a..4749c4618 100644 --- a/src/common/commonebml.h +++ b/src/common/commonebml.h @@ -18,9 +18,12 @@ #include "os.h" +#include + #include #include +using namespace std; using namespace libebml; #if defined(DEBUG) @@ -32,11 +35,11 @@ void MTX_DLL_API __debug_dump_elements(EbmlElement *e, int level); #define can_be_cast(c, e) (dynamic_cast(e) != NULL) -bool MTX_DLL_API is_valid_utf8_string(const char *c); -UTFstring MTX_DLL_API cstr_to_UTFstring(const char *c); -UTFstring MTX_DLL_API cstrutf8_to_UTFstring(const char *c); -char *MTX_DLL_API UTFstring_to_cstr(const UTFstring &u); -char *MTX_DLL_API UTFstring_to_cstrutf8(const UTFstring &u); +bool MTX_DLL_API is_valid_utf8_string(const string &c); +UTFstring MTX_DLL_API cstr_to_UTFstring(const string &c); +UTFstring MTX_DLL_API cstrutf8_to_UTFstring(const string &c); +string MTX_DLL_API UTFstring_to_cstr(const UTFstring &u); +string MTX_DLL_API UTFstring_to_cstrutf8(const UTFstring &u); #define is_id(e, ref) (e->Generic().GlobalId == ref::ClassInfos.GlobalId) diff --git a/src/common/tagwriter.cpp b/src/common/tagwriter.cpp index b388e5e92..9dbf13e90 100644 --- a/src/common/tagwriter.cpp +++ b/src/common/tagwriter.cpp @@ -91,8 +91,7 @@ write_xml_element_rec(int level, EbmlMaster *m; int elt_idx, i; bool found; - char *s; - string x; + string s; elt_idx = parent_idx; found = false; @@ -146,16 +145,15 @@ write_xml_element_rec(int level, break; case ebmlt_string: - x = escape_xml(string(*dynamic_cast(e)).c_str()); - out->printf("%s\n", x.c_str(), tag_elements[elt_idx].name); + s = escape_xml(string(*dynamic_cast(e))); + out->printf("%s\n", s.c_str(), tag_elements[elt_idx].name); break; case ebmlt_ustring: s = UTFstring_to_cstrutf8(UTFstring(*static_cast (e)).c_str()); - x = escape_xml(s); - out->printf("%s\n", x.c_str(), tag_elements[elt_idx].name); - safefree(s); + s = escape_xml(s); + out->printf("%s\n", s.c_str(), tag_elements[elt_idx].name); break; case ebmlt_time: diff --git a/src/extract/attachments.cpp b/src/extract/attachments.cpp index 6ccd85087..779a84d5b 100644 --- a/src/extract/attachments.cpp +++ b/src/extract/attachments.cpp @@ -78,7 +78,6 @@ handle_attachments(KaxAttachments *atts) { int i, k; string name, type; int64_t size, id; - char *str; bool found; mm_io_c *out; @@ -98,9 +97,7 @@ handle_attachments(KaxAttachments *atts) { if (EbmlId(*e) == KaxFileName::ClassInfos.GlobalId) { KaxFileName &fname = *static_cast(e); - str = UTFstring_to_cstr(UTFstring(fname)); - name = str; - safefree(str); + name = UTFstring_to_cstr(UTFstring(fname)); } else if (EbmlId(*e) == KaxMimeType::ClassInfos.GlobalId) { KaxMimeType &mtype = *static_cast(e); diff --git a/src/extract/cuesheets.cpp b/src/extract/cuesheets.cpp index 03e86873d..2c857f92a 100644 --- a/src/extract/cuesheets.cpp +++ b/src/extract/cuesheets.cpp @@ -80,18 +80,12 @@ get_simple_tag(const char *name, if (utname == uname) { KaxTagString *tvalue; tvalue = FINDFIRST(m[i], KaxTagString); - if (tvalue != NULL) { - char *value; - - value = + if (tvalue != NULL) + return UTFstring_to_cstrutf8(UTFstring(*static_cast (tvalue))); - rvalue = value; - safefree(value); - return rvalue; - } else { + else return ""; - } } } @@ -194,33 +188,23 @@ get_chapter_index(int idx, static string get_simple_tag_name(KaxTagSimple &tag) { KaxTagName *tname; - string result; - char *name; tname = FINDFIRST(&tag, KaxTagName); if (tname == NULL) return ""; - name = UTFstring_to_cstrutf8(UTFstring(*static_cast + return UTFstring_to_cstrutf8(UTFstring(*static_cast (tname))); - result = name; - safefree(name); - return result; } static string get_simple_tag_value(KaxTagSimple &tag) { KaxTagString *tstring; - string result; - char *value; tstring = FINDFIRST(&tag, KaxTagString); if (tstring == NULL) return ""; - value = UTFstring_to_cstrutf8(UTFstring(*static_cast - (tstring))); - result = value; - safefree(value); - return result; + return UTFstring_to_cstrutf8(UTFstring(*static_cast + (tstring))); } #define print_if_global(name, format) \ diff --git a/src/input/r_matroska.cpp b/src/input/r_matroska.cpp index 610418bcd..bdc28ebf9 100644 --- a/src/input/r_matroska.cpp +++ b/src/input/r_matroska.cpp @@ -894,14 +894,10 @@ kax_reader_c::read_headers() { ktitle = FINDFIRST(l1, KaxTitle); if (ktitle != NULL) { - char *tmp; - tmp = UTFstring_to_cstr(UTFstring(*ktitle)); + title = UTFstring_to_cstr(UTFstring(*ktitle)); if (verbose > 1) - mxinfo(PFX "| + title: %s\n", tmp); - safefree(tmp); - tmp = UTFstring_to_cstrutf8(UTFstring(*ktitle)); - title = tmp; - safefree(tmp); + mxinfo(PFX "| + title: %s\n", title.c_str()); + title = UTFstring_to_cstrutf8(UTFstring(*ktitle)); } // Let's try to parse the "writing application" string. This usually @@ -920,17 +916,14 @@ kax_reader_c::read_headers() { // store a lower case version of the first as the application's name. kwriting_app = FINDFIRST(l1, KaxWritingApp); if (kwriting_app != NULL) { - char *tmp; int idx; vector parts, ver_parts; string s; - tmp = UTFstring_to_cstrutf8(UTFstring(*kwriting_app)); - if (verbose > 1) - mxinfo(PFX "| + writing app: %s\n", tmp); - s = tmp; - safefree(tmp); + s = UTFstring_to_cstrutf8(UTFstring(*kwriting_app)); strip(s); + if (verbose > 1) + mxinfo(PFX "| + writing app: %s\n", s.c_str()); if (starts_with_case(s, "avi-mux gui")) s.replace(0, strlen("avi-mux gui"), "avimuxgui"); @@ -1252,14 +1245,12 @@ kax_reader_c::read_headers() { ktname = FINDFIRST(ktentry, KaxTrackName); if (ktname != NULL) { - char *tmp; safefree(track->track_name); track->track_name = - UTFstring_to_cstrutf8(UTFstring(*ktname)); - tmp = UTFstring_to_cstr(UTFstring(*ktname)); + safestrdup(UTFstring_to_cstrutf8(UTFstring(*ktname)).c_str()); if (verbose > 1) - mxinfo(PFX "| + Name: %s\n", tmp); - safefree(tmp); + mxinfo(PFX "| + Name: %s\n", + UTFstring_to_cstr(UTFstring(*ktname)).c_str()); } kcencodings = FINDFIRST(ktentry, KaxContentEncodings); @@ -2115,11 +2106,10 @@ void kax_reader_c::identify() { int i; string info; - char *str; if (identify_verbose && (title.length() > 0)) mxinfo("File '%s': container: Matroska [title:%s]\n", ti->fname, - escape(title.c_str()).c_str()); + escape(title).c_str()); else mxinfo("File '%s': container: Matroska\n", ti->fname); for (i = 0; i < tracks.size(); i++) @@ -2158,18 +2148,14 @@ kax_reader_c::identify() { mxinfo("Attachment ID %lld: type '%s', size %lld bytes, ", attachments[i].id, attachments[i].mime_type.c_str(), attachments[i].size); - if (attachments[i].description.length() > 0) { - str = UTFstring_to_cstr(attachments[i].description.c_str()); - mxinfo("description '%s', ", str); - safefree(str); - } + if (attachments[i].description.length() > 0) + mxinfo("description '%s', ", + UTFstring_to_cstr(attachments[i].description).c_str()); if (attachments[i].name.length() == 0) mxinfo("no file name given\n"); - else { - str = UTFstring_to_cstr(attachments[i].name.c_str()); - mxinfo("file name '%s'\n", str); - safefree(str); - } + else + mxinfo("file name '%s'\n", + UTFstring_to_cstr(attachments[i].name).c_str()); } } diff --git a/src/mkvinfo.cpp b/src/mkvinfo.cpp index 1d052414e..c50d8c2a2 100644 --- a/src/mkvinfo.cpp +++ b/src/mkvinfo.cpp @@ -157,31 +157,15 @@ usage() { #endif } -string -UTFstring_to_string_local(const UTFstring &src) { - string retval; - char *str; - str = UTFstring_to_cstr(src); - retval = str; - safefree(str); - return retval; -} - #if defined(HAVE_WXWINDOWS) && WXUNICODE string UTFstring_to_string(const UTFstring &src) { - string retval; - char *str; - if (!use_gui) - return UTFstring_to_string_local(src); - str = UTFstring_to_cstrutf8(src); - retval = str; - safefree(str); - return retval; + return UTFstring_to_cstr(src); + return UTFstring_to_cstrutf8(src); } #else // HAVE_WXWINDOWS && WXUNICODE -#define UTFstring_to_string(src) UTFstring_to_string_local(src) +#define UTFstring_to_string(src) UTFstring_to_cstr(src) #endif // HAVE_WXWINDOWS && WXUNICODE #define UTF2STR(s) UTFstring_to_string(UTFstring(s)).c_str() @@ -1654,8 +1638,7 @@ handle_elements_rec(EbmlStream *es, EbmlMaster *m; int elt_idx, i; bool found; - string format, s2; - char *s; + string format, s; found = false; for (elt_idx = 0; mapping[elt_idx].name != NULL; elt_idx++) @@ -1695,8 +1678,7 @@ handle_elements_rec(EbmlStream *es, format += ": %s"; s = UTFstring_to_cstr(UTFstring(*static_cast (e)).c_str()); - show_element(e, level, format.c_str(), s); - safefree(s); + show_element(e, level, format.c_str(), s.c_str()); break; case ebmlt_time: @@ -1707,8 +1689,8 @@ handle_elements_rec(EbmlStream *es, case ebmlt_binary: format += ": %s"; - s2 = format_binary(*static_cast(e)); - show_element(e, level, format.c_str(), s2.c_str()); + s = format_binary(*static_cast(e)); + show_element(e, level, format.c_str(), s.c_str()); break; default: diff --git a/src/mkvmerge.cpp b/src/mkvmerge.cpp index 4c97264f6..e63dcee22 100644 --- a/src/mkvmerge.cpp +++ b/src/mkvmerge.cpp @@ -694,8 +694,6 @@ convert_old_tags(KaxTag *tag) { KaxTagName *kname; KaxTagString *kvalue; string name, value; - char *tmp; - bool found; int i; targets = &GetChild(*tag); @@ -709,12 +707,8 @@ convert_old_tags(KaxTag *tag) { kname = FINDFIRST(simple, KaxTagName); kvalue = FINDFIRST(simple, KaxTagString); if ((kname != NULL) && (kvalue != NULL)) { - tmp = UTFstring_to_cstrutf8((const UTFstring &)(*kname)); - name = tmp; - safefree(tmp); - tmp = UTFstring_to_cstrutf8((const UTFstring &)(*kvalue)); - value = tmp; - safefree(tmp); + name = UTFstring_to_cstrutf8((const UTFstring &)(*kname)); + value = UTFstring_to_cstrutf8((const UTFstring &)(*kvalue)); if (name == "CATALOG") *static_cast(kname) = cstrutf8_to_UTFstring("CATALOG_NUMBER"); diff --git a/src/mmg/mmg.cpp b/src/mmg/mmg.cpp index 728d43deb..0f6e268b1 100644 --- a/src/mmg/mmg.cpp +++ b/src/mmg/mmg.cpp @@ -258,12 +258,7 @@ UTFstring_to_wxString(const UTFstring &u) { #else wxString UTFstring_to_wxString(const UTFstring &u) { - wxString retval; - char *c; - c = UTFstring_to_cstr(u); - retval = c; - safefree(c); - return retval; + return wxString(UTFstring_to_cstr(u).c_str()); } #endif