Objectively
Ultra-lightweight object oriented framework for GNU C.
Loading...
Searching...
No Matches
NumberFormatter.c
Go to the documentation of this file.
1/*
2 * Objectively: Ultra-lightweight object oriented framework for GNU C.
3 * Copyright (C) 2014 Jay Dolan <jay@jaydolan.com>
4 *
5 * This software is provided 'as-is', without any express or implied
6 * warranty. In no event will the authors be held liable for any damages
7 * arising from the use of this software.
8 *
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
12 *
13 * 1. The origin of this software must not be misrepresented; you must not
14 * claim that you wrote the original software. If you use this software
15 * in a product, an acknowledgment in the product documentation would be
16 * appreciated but is not required.
17 *
18 * 2. Altered source versions must be plainly marked as such, and must not be
19 * misrepresented as being the original software.
20 *
21 * 3. This notice may not be removed or altered from any source distribution.
22 */
23
24#include <assert.h>
25#include <stdio.h>
26#include <stdlib.h>
27
28#include "NumberFormatter.h"
29
30#define _Class _NumberFormatter
31
32#pragma mark - NumberFormatter
33
38static NumberFormatter *initWithFormat(NumberFormatter *self, const char *fmt) {
39
40 self = (NumberFormatter *) super(Object, self, init);
41 if (self) {
42 self->fmt = fmt ?: NUMBERFORMAT_DECIMAL;
43 }
44
45 return self;
46}
47
52static Number *numberFromString(const NumberFormatter *self, const String *string) {
53
54 if (string) {
55 double value;
56
57 const int res = sscanf(string->chars, self->fmt, &value);
58 if (res == 1) {
59 return $(alloc(Number), initWithValue, value);
60 }
61 }
62
63 return NULL;
64}
65
70static String *stringFromNumber(const NumberFormatter *self, const Number *number) {
71
72 return $(alloc(String), initWithFormat, self->fmt, number->value);
73}
74
75#pragma mark - Class lifecycle
76
80static void initialize(Class *clazz) {
81
82 ((NumberFormatterInterface *) clazz->interface)->numberFromString = numberFromString;
83 ((NumberFormatterInterface *) clazz->interface)->initWithFormat = initWithFormat;
84 ((NumberFormatterInterface *) clazz->interface)->stringFromNumber = stringFromNumber;
85}
86
92 static Class *clazz;
93 static Once once;
94
95 do_once(&once, {
96 clazz = _initialize(&(const ClassDef) {
97 .name = "NumberFormatter",
98 .superclass = _Object(),
99 .instanceSize = sizeof(NumberFormatter),
100 .interfaceOffset = offsetof(NumberFormatter, interface),
101 .interfaceSize = sizeof(NumberFormatterInterface),
103 });
104 });
105
106 return clazz;
107}
108
109#undef _Class
static Array * init(Array *self)
Definition Array.c:420
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:86
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:176
#define super(type, obj, method,...)
static Number * initWithValue(Number *self, const double value)
Definition Number.c:129
static Number * numberFromString(const NumberFormatter *self, const String *string)
Class * _NumberFormatter(void)
static String * stringFromNumber(const NumberFormatter *self, const Number *number)
static void initialize(Class *clazz)
static NumberFormatter * initWithFormat(NumberFormatter *self, const char *fmt)
Number formatting and parsing.
#define NUMBERFORMAT_DECIMAL
Decimal format.
Class * _Object(void)
Definition Object.c:136
static String * string(void)
Definition String.c:905
long Once
The Once type.
Definition Once.h:37
#define do_once(once, block)
Executes the given block at most one time.
Definition Once.h:43
ClassDefs are passed to _initialize via an archetype to initialize a Class.
Definition Class.h:41
The runtime representation of a Class.
Definition Class.h:95
ident interface
The interface of the Class.
Definition Class.h:105
Number formatting and parsing.
String * stringFromNumber(const NumberFormatter *self, const Number *number)
Yields a String representation of the specified Number instance.
const char * fmt
The format string.
A wrapper for placing numeric primitives into collections, etc.
Definition Number.h:41
double value
The backing value.
Definition Number.h:57
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46
UTF-8 strings.
Definition String.h:69
char * chars
The backing null-terminated UTF-8 encoded character array.
Definition String.h:85