2 * Copyright 2017, Andrew Lindesay <apl@lindesay.co.nz>
3 * Distributed under the terms of the MIT License.
15 BJsonEvent::BJsonEvent(json_event_type eventType
, const char* content
)
17 fEventType(eventType
),
24 BJsonEvent::BJsonEvent(const char* content
)
26 fEventType(B_JSON_STRING
),
33 BJsonEvent::BJsonEvent(double content
) {
34 fEventType
= B_JSON_NUMBER
;
37 int actualLength
= snprintf(0, 0, "%f", content
) + 1;
38 char* buffer
= (char*) malloc(sizeof(char) * actualLength
);
41 fprintf(stderr
, "memory exhaustion\n");
42 // given the risk, this is the only sensible thing to do here.
46 sprintf(buffer
, "%f", content
);
47 fOwnedContent
= buffer
;
51 BJsonEvent::BJsonEvent(int64 content
) {
52 fEventType
= B_JSON_NUMBER
;
56 static const char* zeroValue
= "0";
57 static const char* oneValue
= "1";
68 int actualLength
= snprintf(0, 0, "%" B_PRId64
, content
) + 1;
69 char* buffer
= (char*) malloc(sizeof(char) * actualLength
);
72 fprintf(stderr
, "memory exhaustion\n");
73 // given the risk, this is the only sensible thing to do
78 sprintf(buffer
, "%" B_PRId64
, content
);
79 fOwnedContent
= buffer
;
86 BJsonEvent::BJsonEvent(json_event_type eventType
)
88 fEventType(eventType
),
95 BJsonEvent::~BJsonEvent()
97 if (NULL
!= fOwnedContent
)
103 BJsonEvent::EventType() const
110 BJsonEvent::Content() const
112 if (NULL
!= fOwnedContent
)
113 return fOwnedContent
;
119 BJsonEvent::ContentDouble() const
121 return strtod(Content(), NULL
);
126 BJsonEvent::ContentInteger() const
128 return strtoll(Content(), NULL
, 10);