src/jdk.jpackage/windows/native/libjpackage/tstrings.cpp
author herrick
Mon, 17 Jun 2019 15:38:04 -0400
branchJDK-8200758-branch
changeset 57413 45c74e654794
permissions -rw-r--r--
8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files) Submitted-by: asemenyuk Reviewed-by: herrick, almatvee
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57413
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
     1
/*
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
     4
 *
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    10
 *
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    15
 * accompanied this code).
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    16
 *
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    20
 *
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    23
 * questions.
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    24
 */
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    25
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    26
#include <stdio.h>
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    27
#include <stdarg.h>
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    28
#include <stdexcept>
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    29
#include <algorithm>
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    30
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    31
#include "tstrings.h"
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    32
#include "ErrorHandling.h"
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    33
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    34
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    35
namespace tstrings {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    36
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    37
/* Create formatted string
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    38
 */
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    39
tstring unsafe_format(tstring::const_pointer format, ...) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    40
    if (!format) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    41
        throw std::invalid_argument("Destination buffer can't be NULL");
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    42
    }
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    43
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    44
    tstring fmtout;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    45
    int ret;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    46
    const int inc = 256;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    47
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    48
    va_list args;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    49
    va_start(args, format);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    50
    do {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    51
        fmtout.resize(fmtout.size() + inc);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    52
#ifdef _MSC_VER
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    53
        ret = _vsntprintf_s(&*fmtout.begin(), fmtout.size(), _TRUNCATE, format, args);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    54
#else
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    55
        // With g++ this compiles only with '-std=gnu++0x' option
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    56
        ret = vsnprintf(&*fmtout.begin(), fmtout.size(), format, args);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    57
#endif
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    58
    } while(-1 == ret);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    59
    va_end(args);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    60
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    61
    //update string size by actual value
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    62
    fmtout.resize(ret);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    63
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    64
    return fmtout;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    65
}
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    66
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    67
/*
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    68
 * Tests if two strings are equal according to CompareType.
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    69
 *
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    70
 * a - string to compare
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    71
 * b - string to compare
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    72
 * ct - CASE_SENSITIVE: case sensitive comparing type
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    73
 *      IGNORE_CASE: case insensitive comparing type
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    74
 */
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    75
bool equals(const tstring& a, const tstring& b, const CompareType ct) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    76
    if (IGNORE_CASE==ct) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    77
        return toLower(a) == toLower(b);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    78
    }
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    79
    return a == b;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    80
}
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    81
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    82
bool startsWith(const tstring &str, const tstring &substr, const CompareType ct)
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    83
{
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    84
    if (str.size() < substr.size()) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    85
        return false;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    86
    }
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    87
    const tstring startOfStr = str.substr(0, substr.size());
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    88
    return tstrings::equals(startOfStr, substr, ct);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    89
}
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    90
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    91
bool endsWith(const tstring &str, const tstring &substr, const CompareType ct)
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    92
{
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    93
    if (str.size() < substr.size()) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    94
        return false;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    95
    }
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    96
    const tstring endOfStr = str.substr(str.size() - substr.size());
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    97
    return tstrings::equals(endOfStr, substr, ct);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    98
}
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    99
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   100
/*
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   101
 * Split string into a vector with given delimiter string
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   102
 *
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   103
 * strVector - string vector to store split tstring
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   104
 * str - string to split
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   105
 * delimiter - delimiter to split the string around
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   106
 * st - ST_ALL: return value includes an empty string
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   107
 *      ST_EXCEPT_EMPTY_STRING: return value does not include an empty string
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   108
 *
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   109
 * Note: It does not support multiple delimiters
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   110
 */
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   111
void split(tstring_array &strVector, const tstring &str,
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   112
          const tstring &delimiter, const SplitType st) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   113
    tstring::size_type start = 0, end = 0, length = str.length();
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   114
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   115
    if (length == 0 || delimiter.length() == 0) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   116
        return;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   117
    }
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   118
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   119
    end = str.find(delimiter, start);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   120
    while(end != tstring::npos) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   121
        if(st == ST_ALL || end - start > 1 ) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   122
            strVector.push_back(str.substr(start, end == tstring::npos ?
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   123
                                                  tstring::npos : end - start));
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   124
        }
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   125
        start = end > (tstring::npos - delimiter.size()) ?
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   126
                tstring::npos : end + delimiter.size();
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   127
        end = str.find(delimiter, start);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   128
    }
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   129
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   130
    if(st == ST_ALL || start < length) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   131
        strVector.push_back(str.substr(start, length - start));
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   132
    }
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   133
}
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   134
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   135
/*
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   136
 * Convert uppercase letters to lowercase
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   137
 */
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   138
tstring toLower(const tstring& str) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   139
    tstring lower(str);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   140
    tstring::iterator ok = std::transform(lower.begin(), lower.end(),
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   141
                                          lower.begin(), tolower);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   142
    if (ok!=lower.end()) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   143
        lower.resize(0);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   144
    }
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   145
    return lower;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   146
}
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   147
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   148
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   149
/*
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   150
 * Replace all substring occurrences in a tstring.
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   151
 * If 'str' or 'search' is empty the function returns 'str'.
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   152
 * The given 'str' remains unchanged in any case.
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   153
 * The function returns changed copy of 'str'.
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   154
 */
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   155
tstring replace(const tstring &str, const tstring &search, const tstring &replace)
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   156
{
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   157
    if (search.empty()) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   158
        return str;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   159
    }
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   160
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   161
    tstring s(str);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   162
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   163
    for (size_t pos = 0; ; pos += replace.length()) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   164
        pos = s.find(search, pos);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   165
        if (pos == tstring::npos) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   166
            break;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   167
        }
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   168
        s.erase(pos, search.length());
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   169
        s.insert(pos, replace);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   170
    }
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   171
    return s;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   172
}
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   173
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   174
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   175
/*
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   176
 * Remove trailing spaces
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   177
 */
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   178
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   179
tstring trim(const tstring& str, const tstring& whitespace) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   180
    const size_t strBegin = str.find_first_not_of(whitespace);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   181
    if (strBegin == std::string::npos) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   182
        return tstring(); // no content
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   183
    }
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   184
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   185
    const size_t  strEnd = str.find_last_not_of(whitespace);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   186
    const size_t strRange = strEnd - strBegin + 1;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   187
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   188
    return str.substr(strBegin, strRange);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   189
}
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   190
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   191
} // namespace tstrings
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   192
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   193
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   194
#ifdef TSTRINGS_WITH_WCHAR
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   195
namespace tstrings {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   196
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   197
namespace {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   198
/*
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   199
 * Converts UTF16-encoded string into multi-byte string of the given encoding.
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   200
 */
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   201
std::string toMultiByte(const std::wstring& utf16str, int encoding) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   202
    std::string reply;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   203
    do {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   204
        int cm = WideCharToMultiByte(encoding,
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   205
                                    0,
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   206
                                    utf16str.c_str(),
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   207
                                    int(utf16str.size()),
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   208
                                    NULL,
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   209
                                    0,
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   210
                                    NULL,
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   211
                                    NULL);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   212
        if (cm < 0) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   213
            JP_THROW("Unexpected reply from WideCharToMultiByte()");
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   214
        }
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   215
        if (0 == cm) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   216
            break;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   217
        }
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   218
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   219
        reply.resize(cm);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   220
        int cm2 = WideCharToMultiByte(encoding,
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   221
                                    0,
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   222
                                    utf16str.c_str(),
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   223
                                    int(utf16str.size()),
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   224
                                    &*reply.begin(),
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   225
                                    cm,
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   226
                                    NULL,
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   227
                                    NULL);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   228
        if (cm != cm2) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   229
            JP_THROW("Unexpected reply from WideCharToMultiByte()");
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   230
        }
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   231
    } while(0);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   232
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   233
    return reply;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   234
}
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   235
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   236
/*
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   237
 * Converts multi-byte string of the given encoding into UTF16-encoded string.
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   238
 */
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   239
std::wstring fromMultiByte(const std::string& str, int encoding) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   240
    std::wstring utf16;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   241
    do {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   242
        int cw = MultiByteToWideChar(encoding,
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   243
                                    MB_ERR_INVALID_CHARS,
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   244
                                    str.c_str(),
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   245
                                    int(str.size()),
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   246
                                    NULL,
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   247
                                    0);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   248
        if (cw < 0) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   249
            JP_THROW("Unexpected reply from MultiByteToWideChar()");
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   250
        }
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   251
        if (0 == cw) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   252
            break;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   253
        }
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   254
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   255
        utf16.resize(cw);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   256
        int cw2 = MultiByteToWideChar(encoding,
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   257
                                    MB_ERR_INVALID_CHARS,
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   258
                                    str.c_str(),
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   259
                                    int(str.size()),
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   260
                                    &*utf16.begin(),
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   261
                                    cw);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   262
        if (cw != cw2) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   263
            JP_THROW("Unexpected reply from MultiByteToWideChar()");
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   264
        }
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   265
    } while(0);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   266
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   267
    return utf16;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   268
}
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   269
} // namespace
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   270
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   271
std::string toUtf8(const std::wstring& utf16str) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   272
    return toMultiByte(utf16str, CP_UTF8);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   273
}
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   274
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   275
std::wstring toUtf16(const std::string& utf8str) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   276
    return fromMultiByte(utf8str, CP_UTF8);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   277
}
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   278
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   279
} // namespace tstrings
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   280
#endif // ifdef TSTRINGS_WITH_WCHAR