btrfs: Attempt to fix GCC2 build.
[haiku.git] / src / kits / shared / JsonEvent.cpp
blob87687216a3657bb9189c35307bf0e8ab0e7e2586
1 /*
2 * Copyright 2017, Andrew Lindesay <apl@lindesay.co.nz>
3 * Distributed under the terms of the MIT License.
4 */
7 #include "JsonEvent.h"
9 #include <stdlib.h>
10 #include <stdio.h>
12 #include <String.h>
15 BJsonEvent::BJsonEvent(json_event_type eventType, const char* content)
17 fEventType(eventType),
18 fContent(content),
19 fOwnedContent(NULL)
24 BJsonEvent::BJsonEvent(const char* content)
26 fEventType(B_JSON_STRING),
27 fContent(content),
28 fOwnedContent(NULL)
33 BJsonEvent::BJsonEvent(double content) {
34 fEventType = B_JSON_NUMBER;
35 fContent = NULL;
37 int actualLength = snprintf(0, 0, "%f", content) + 1;
38 char* buffer = (char*) malloc(sizeof(char) * actualLength);
40 if (buffer == NULL) {
41 fprintf(stderr, "memory exhaustion\n");
42 // given the risk, this is the only sensible thing to do here.
43 exit(EXIT_FAILURE);
46 sprintf(buffer, "%f", content);
47 fOwnedContent = buffer;
51 BJsonEvent::BJsonEvent(int64 content) {
52 fEventType = B_JSON_NUMBER;
53 fContent = NULL;
54 fOwnedContent = NULL;
56 static const char* zeroValue = "0";
57 static const char* oneValue = "1";
59 switch (content) {
60 case 0:
61 fContent = zeroValue;
62 break;
63 case 1:
64 fContent = oneValue;
65 break;
66 default:
68 int actualLength = snprintf(0, 0, "%" B_PRId64, content) + 1;
69 char* buffer = (char*) malloc(sizeof(char) * actualLength);
71 if (buffer == NULL) {
72 fprintf(stderr, "memory exhaustion\n");
73 // given the risk, this is the only sensible thing to do
74 // here.
75 exit(EXIT_FAILURE);
78 sprintf(buffer, "%" B_PRId64, content);
79 fOwnedContent = buffer;
80 break;
86 BJsonEvent::BJsonEvent(json_event_type eventType)
88 fEventType(eventType),
89 fContent(NULL),
90 fOwnedContent(NULL)
95 BJsonEvent::~BJsonEvent()
97 if (NULL != fOwnedContent)
98 free(fOwnedContent);
102 json_event_type
103 BJsonEvent::EventType() const
105 return fEventType;
109 const char*
110 BJsonEvent::Content() const
112 if (NULL != fOwnedContent)
113 return fOwnedContent;
114 return fContent;
118 double
119 BJsonEvent::ContentDouble() const
121 return strtod(Content(), NULL);
125 int64
126 BJsonEvent::ContentInteger() const
128 return strtoll(Content(), NULL, 10);