Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
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
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
#define alloc(type)
Allocate and initialize and instance of type.
Definition: Class.h:159
#define super(type, obj, method,...)
static void initialize(Class *clazz)
Number formatting and parsing.
#define NUMBERFORMAT_DECIMAL
Decimal format.
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
Condition * init(Condition *self)
Initializes this Condition.
Definition: Condition.c:67
DateFormatter * initWithFormat(DateFormatter *self, const char *fmt)
Initializes a DateFormatter with the specified format string.
Definition: DateFormatter.c:76
MutableString * string(void)
Returns a new MutableString.
Number formatting and parsing.
String * stringFromNumber(const NumberFormatter *self, const Number *number)
Yields a String representation of the specified Number instance.
Number * numberFromString(const NumberFormatter *self, const String *string)
Parses a Number from the specified String.
Class * _NumberFormatter(void)
The NumberFormatter archetype.
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
Number * initWithValue(Number *self, double value)
Initializes this Number with the specified value.
Definition: Number.c:137
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46
Class * _Object(void)
The Object archetype.
Definition: Object.c:136
Immutable UTF-8 strings.
Definition: String.h:69
char * chars
The backing null-terminated UTF-8 encoded character array.
Definition: String.h:85