cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bhigg...@apache.org
Subject [21/38] git commit: BB10: Implement numberToString.
Date Wed, 30 Jul 2014 19:06:38 GMT
BB10: Implement numberToString.


Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-globalization/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-globalization/commit/51ab1132
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-globalization/tree/51ab1132
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-globalization/diff/51ab1132

Branch: refs/heads/lyon-g11n
Commit: 51ab113206558a4ca9f97bf54cf690ef068d66be
Parents: 2a40b5b
Author: Lianghui Chen <liachen@blackberry.com>
Authored: Fri Jul 25 14:41:54 2014 -0400
Committer: Lianghui Chen <liachen@blackberry.com>
Committed: Mon Jul 28 15:10:46 2014 -0400

----------------------------------------------------------------------
 .../native/src/globalization_ndk.cpp            | 120 ++++++++++++++++++-
 1 file changed, 119 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-globalization/blob/51ab1132/src/blackberry10/native/src/globalization_ndk.cpp
----------------------------------------------------------------------
diff --git a/src/blackberry10/native/src/globalization_ndk.cpp b/src/blackberry10/native/src/globalization_ndk.cpp
index c6f09f2..a927709 100644
--- a/src/blackberry10/native/src/globalization_ndk.cpp
+++ b/src/blackberry10/native/src/globalization_ndk.cpp
@@ -623,9 +623,127 @@ std::string GlobalizationNDK::getFirstDayOfWeek()
     return resultInJson(d);
 }
 
+enum ENumberType {
+    kNumberDecimal,
+    kNumberCurrency,
+    kNumberPercent,
+    kNumberTypeCount
+};
+
+static bool handleNumberOptions(const Json::Value& options, ENumberType& type, std::string&
error)
+{
+    if (options.isNull())
+        return true;
+
+    if (!options.isObject()) {
+        slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::handleNumberOptions: invalid options
type: %d",
+                options.type());
+        error = "Invalid options type!";
+        return false;
+    }
+
+    Json::Value tv = options["type"];
+    if (tv.isNull()) {
+        slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::handleNumberOptions: No type found!");
+        error = "No type found!";
+        return false;
+    }
+
+    if (!tv.isString()) {
+        slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::handleNumberOptions: Invalid type
type: %d",
+                tv.type());
+        error = "Invalid type type!";
+        return false;
+    }
+
+    std::string tstr = tv.asString();
+    if (tstr.empty()) {
+        slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::handleNumberOptions: Empty type!");
+        error = "Empty type!";
+        return false;
+    }
+
+    if (tstr == "currency") {
+        type = kNumberCurrency;
+    } else if (tstr == "percent") {
+        type = kNumberPercent;
+    } else if (tstr == "decimal") {
+        type = kNumberDecimal;
+    } else {
+        slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::handleNumberOptions: unsupported
type: %s",
+                tstr.c_str());
+        error = "Unsupported type!";
+        return false;
+    }
+
+    return true;
+}
+
 std::string GlobalizationNDK::numberToString(const std::string& args)
 {
-    return errorInJson(UNKNOWN_ERROR, "Not supported!");
+    if (args.empty()) {
+        slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::numberToString: no arguments provided!");
+        return errorInJson(UNKNOWN_ERROR, "No arguments provided!");
+    }
+
+    Json::Reader reader;
+    Json::Value root;
+    bool parse = reader.parse(args, root);
+
+    if (!parse) {
+        slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::numberToString: invalid json data:
%s",
+                args.c_str());
+        return errorInJson(PARSING_ERROR, "Invalid json data!");
+    }
+
+    Json::Value nv = root["number"];
+    if (nv.isNull()) {
+        slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::numberToString: no number provided!");
+        return errorInJson(FORMATTING_ERROR, "No number provided!");
+    }
+
+    if (!nv.isNumeric()) {
+        slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::numberToString: invalid number
type: %d!",
+                nv.type());
+        return errorInJson(FORMATTING_ERROR, "Invalid number type!");
+    }
+
+    // This is the default value when no options provided.
+    ENumberType type = kNumberDecimal;
+
+    Json::Value options = root["options"];
+    std::string error;
+    if (!handleNumberOptions(options, type, error))
+        return errorInJson(PARSING_ERROR, error);
+
+    UErrorCode status = U_ZERO_ERROR;
+    NumberFormat* nf;
+    switch (type) {
+    case kNumberDecimal:
+    default:
+        nf = NumberFormat::createInstance(status);
+        break;
+    case kNumberCurrency:
+        nf = NumberFormat::createCurrencyInstance(status);
+        break;
+    case kNumberPercent:
+        nf = NumberFormat::createPercentInstance(status);
+        break;
+    }
+
+    if (!nf) {
+        slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::numberToString: failed to create
NumberFormat instance for type %d: %d",
+                status, type);
+        return errorInJson(UNKNOWN_ERROR, "Failed to create NumberFormat instance!");
+    }
+
+    UnicodeString result;
+    nf->format(nv.asDouble(), result);
+    std::string utf8;
+    result.toUTF8String(utf8);
+    delete nf;
+
+    return resultInJson(utf8);
 }
 
 std::string GlobalizationNDK::stringToNumber(const std::string& args)


Mime
View raw message