U_CAPI int32_t U_EXPORT2 unum_parseDecimal(const UNumberFormat* fmt, const UChar* text, int32_t textLength, int32_t *parsePos /* 0 = start */, char *outBuf, int32_t outBufLength, UErrorCode *status) { if (U_FAILURE(*status)) { return -1; } if ((outBuf == NULL && outBufLength != 0) || outBufLength < 0) { *status = U_ILLEGAL_ARGUMENT_ERROR; return -1; } Formattable res; parseRes(res, fmt, text, textLength, parsePos, status); StringPiece sp = res.getDecimalNumber(*status); if (U_FAILURE(*status)) { return -1; } else if (sp.size() > outBufLength) { *status = U_BUFFER_OVERFLOW_ERROR; } else if (sp.size() == outBufLength) { uprv_strncpy(outBuf, sp.data(), sp.size()); *status = U_STRING_NOT_TERMINATED_WARNING; } else { U_ASSERT(outBufLength > 0); uprv_strcpy(outBuf, sp.data()); } return sp.size(); }
U_CAPI double U_EXPORT2 unum_parseDouble( const UNumberFormat* fmt, const UChar* text, int32_t textLength, int32_t *parsePos /* 0 = start */, UErrorCode *status) { Formattable res; parseRes(res, fmt, text, textLength, parsePos, FALSE, status); return res.getDouble(*status); }
U_CAPI int64_t U_EXPORT2 unum_parseInt64( const UNumberFormat* fmt, const UChar* text, int32_t textLength, int32_t *parsePos /* 0 = start */, UErrorCode *status) { Formattable res; parseRes(res, fmt, text, textLength, parsePos, status); return res.getInt64(*status); }
U_CAPI double U_EXPORT2 unum_parseDoubleCurrency(const UNumberFormat* fmt, const UChar* text, int32_t textLength, int32_t* parsePos, /* 0 = start */ UChar* currency, UErrorCode* status) { Formattable res; parseRes(res, fmt, text, textLength, parsePos, TRUE, status); currency[0] = 0; if (res.getType() == Formattable::kObject && res.getObject()->getDynamicClassID() == CurrencyAmount::getStaticClassID()) { const CurrencyAmount* c = (const CurrencyAmount*) res.getObject(); u_strcpy(currency, c->getISOCurrency()); } return res.getDouble(*status); }
U_CAPI double U_EXPORT2 unum_parseDoubleCurrency(const UNumberFormat* fmt, const UChar* text, int32_t textLength, int32_t* parsePos, /* 0 = start */ UChar* currency, UErrorCode* status) { Formattable res; parseRes(res, fmt, text, textLength, parsePos, TRUE, status); currency[0] = 0; const CurrencyAmount* c; if (res.getType() == Formattable::kObject && (c = dynamic_cast<const CurrencyAmount*>(res.getObject())) != NULL) { u_strcpy(currency, c->getISOCurrency()); } return res.getDouble(*status); }
U_INTERNAL UFormattable * U_EXPORT2 unum_parseToUFormattable(const UNumberFormat* fmt, UFormattable *result, const UChar* text, int32_t textLength, int32_t* parsePos, /* 0 = start */ UErrorCode* status) { UFormattable *newFormattable = NULL; if (U_FAILURE(*status)) return result; if (fmt == NULL || (text==NULL && textLength!=0)) { *status = U_ILLEGAL_ARGUMENT_ERROR; return result; } if (result == NULL) { // allocate if not allocated. newFormattable = result = ufmt_open(status); } parseRes(*(Formattable::fromUFormattable(result)), fmt, text, textLength, parsePos, status); if (U_FAILURE(*status) && newFormattable != NULL) { ufmt_close(newFormattable); result = NULL; // deallocate if there was a parse error } return result; }
int main(int argc, char *argv[]) { int sockFd; struct sockaddr_in servAddr; int n; int msgLen; /* a simple command line argument processing */ if (argc != 2) { fprintf(stderr, "Usage: %s <IPaddress>", argv[0]); exit(1); } /* open a socket */ if ((sockFd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("socket error"); exit(1); } /* setup the server address and connect to it */ bzero(&servAddr, sizeof(servAddr)); servAddr.sin_family = AF_INET; servAddr.sin_port = htons(NETCALC_SERV_PORT); /* * Inet_pton(AF_INET, argv[1], &servAddr.sin_addr) is only supported by * the book lib. There is no inet_pton in native Solaris. */ servAddr.sin_addr.s_addr = inet_addr(argv[1]); if (connect(sockFd, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0) { perror("connect error"); exit(1); } /* main loop */ while ((fgets(ioBuf, BUF_SIZE, stdin)) != NULL) { if (buildReq(ioBuf, strlen(ioBuf), msgBuf, &msgLen) == SUCCESS) { /* send request to the server */ if (write(sockFd, msgBuf, msgLen) < 0) { perror("write error"); exit(1); } /* wait for response from the server */ msgLen = read(sockFd, msgBuf, BUF_SIZE); if (msgLen <= 0) break; if (parseRes(msgBuf, msgLen, ioBuf) == SUCCESS) { fputs(ioBuf, stdout); } else { printf("!!%s: parseRes failed\n", __FUNCTION__); DUMP_BUF(msgBuf, msgLen); } } else { printf("!!%s: buildReq failed\n", __FUNCTION__); DUMP_BUF(ioBuf, strlen(ioBuf)); } } close(sockFd); exit(0); }