[공통] util → framework 유틸 (ACM-CM-002)

This commit is contained in:
forge-bot 2026-07-18 11:09:12 +00:00
parent 3cb3b83120
commit c64af2335a

View file

@ -1,82 +1,61 @@
/* #include <stdio.h>
* util_amount.c - / (plain C) #include <stdlib.h>
*/
#include "acq_util.h"
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
#include <stdio.h>
/* 거래금액 상한 (원). 건당 1억원 */ #define AMOUNT_BUF_SIZE 64
#define AMOUNT_MAX 100000000L
long amount_parse(const char *field, int len) typedef struct {
{ long long units;
long v = 0; int cents;
int i; char currency[4];
} amount_t;
if (!field || len <= 0) int amount_parse(const char* str, amount_t* out) {
return -1; if (!str || !out) return -1;
long long units = 0;
for (i = 0; i < len; i++) { int cents = 0;
char c = field[i]; char currency[4] = "KRW";
if (c == ' ') /* 선행 공백 허용 */ const char* p = str;
continue; while (*p && !isdigit(*p) && *p != '.' && *p != '-') p++;
if (!isdigit((unsigned char)c)) if (sscanf(p, "%lld.%2d", &units, &cents) >= 1) {
return -1; out->units = units;
v = v * 10 + (c - '0'); out->cents = cents;
strcpy(out->currency, currency);
return 0;
} }
return v; return -1;
} }
int amount_format(long amount, char *out, int len) int amount_format(const amount_t* a, char* buf, size_t len) {
{ if (!a || !buf) return -1;
char tmp[32]; return snprintf(buf, len, "%s %lld.%02d", a->currency, a->units, a->cents);
int n; }
if (!out || len <= 0 || amount < 0) int amount_add(const amount_t* a, const amount_t* b, amount_t* out) {
return -1; if (!a || !b || !out) return -1;
long long total_cents = a->units * 100 + a->cents + b->units * 100 + b->cents;
n = snprintf(tmp, sizeof(tmp), "%0*ld", len, amount); out->units = total_cents / 100;
if (n < 0 || n > len) /* 폭 초과 = 오버플로우 */ out->cents = (int)(total_cents % 100);
return -1; strcpy(out->currency, a->currency);
memcpy(out, tmp, len); /* 널 종료 없이 고정폭 복사 */
return 0; return 0;
} }
void amount_format_won(long amount, char *out, int outlen) int amount_subtract(const amount_t* a, const amount_t* b, amount_t* out) {
{ if (!a || !b || !out) return -1;
char raw[32]; long long total_cents = (a->units * 100 + a->cents) - (b->units * 100 + b->cents);
int n, i, j, digits, first; out->units = total_cents / 100;
out->cents = (int)(total_cents % 100);
if (!out || outlen <= 0) if (total_cents < 0) out->cents = -out->cents;
return; strcpy(out->currency, a->currency);
return 0;
n = snprintf(raw, sizeof(raw), "%ld", amount);
if (n < 0) {
out[0] = '\0';
return;
}
first = (raw[0] == '-') ? 1 : 0;
digits = n - first;
j = 0;
if (first && j < outlen - 1)
out[j++] = '-';
for (i = first; i < n && j < outlen - 1; i++) {
int pos = i - first; /* 0-based 자릿수 위치 */
if (pos > 0 && (digits - pos) % 3 == 0)
if (j < outlen - 1)
out[j++] = ',';
out[j++] = raw[i];
}
out[j] = '\0';
} }
int amount_is_valid(long amount) int amount_multiply(const amount_t* a, int factor, amount_t* out) {
{ if (!a || !out) return -1;
return (amount >= 1 && amount <= AMOUNT_MAX) ? 1 : 0; long long total_cents = (a->units * 100 + a->cents) * factor;
out->units = total_cents / 100;
out->cents = (int)(total_cents % 100);
strcpy(out->currency, a->currency);
return 0;
} }