1 /**
2  * License: MIT
3  */
4 module jansson_d.error;
5 
6 
7 package:
8 
9 private static import core.stdc.stdio;
10 private static import core.stdc.string;
11 private static import jansson_d.jansson;
12 private static import jansson_d.jansson_private;
13 
14 pure nothrow @trusted @nogc @live
15 void jsonp_error_init(scope jansson_d.jansson.json_error_t* error, scope const char* source)
16 
17 	do
18 	{
19 		if (error != null) {
20 			error.text[0] = '\0';
21 			error.line = -1;
22 			error.column = -1;
23 			error.position = 0;
24 
25 			if (source != null) {
26 				jsonp_error_set_source(error, source);
27 			} else {
28 				error.source[0] = '\0';
29 			}
30 		}
31 	}
32 
33 pure nothrow @trusted @nogc @live
34 void jsonp_error_set_source(scope jansson_d.jansson.json_error_t* error, scope const char* source)
35 
36 	do
37 	{
38 		if ((error == null) || (source == null)) {
39 			return;
40 		}
41 
42 		size_t length_ = core.stdc..string.strlen(source);
43 
44 		if (length_ < jansson_d.jansson.JSON_ERROR_SOURCE_LENGTH) {
45 			core.stdc..string.strncpy(&(error.source[0]), source, length_ + 1);
46 		} else {
47 			size_t extra = length_ - jansson_d.jansson.JSON_ERROR_SOURCE_LENGTH + 4;
48 			error.source[0] = '.';
49 			error.source[1] = '.';
50 			error.source[2] = '.';
51 			core.stdc..string.strncpy(&(error.source[0]) + 3, source + extra, length_ - extra + 1);
52 		}
53 	}
54 
55 nothrow @nogc @live
56 void jsonp_error_set(F ...)(scope jansson_d.jansson.json_error_t* error, int line, int column, size_t position, jansson_d.jansson.json_error_code_t code, scope const char* msg, F f)
57 
58 	in
59 	{
60 		assert(error != null);
61 		assert(msg != null);
62 	}
63 
64 	do
65 	{
66 		static if (f.length != 0) {
67 			jsonp_error_vset(error, line, column, position, code, msg, f[0 .. $]);
68 		} else {
69 			jsonp_error_vset(error, line, column, position, code, msg);
70 		}
71 	}
72 
73 nothrow @nogc @live
74 void jsonp_error_vset(F ...)(scope jansson_d.jansson.json_error_t* error, int line, int column, size_t position, jansson_d.jansson.json_error_code_t code, scope const char* msg, F f)
75 
76 	do
77 	{
78 		if (error == null) {
79 			return;
80 		}
81 
82 		if (error.text[0] != '\0') {
83 			/* error already set */
84 			return;
85 		}
86 
87 		error.line = line;
88 		error.column = column;
89 		error.position = cast(int)(position);
90 
91 		static if (f.length != 0) {
92 			jansson_d.jansson_private.snprintf(&(error.text[0]), error.text.length - 1, msg, f[0 .. $]);
93 		} else {
94 			jansson_d.jansson_private.snprintf(&(error.text[0]), error.text.length - 1, msg);
95 		}
96 
97 		error.text[error.text.length - 2] = '\0';
98 		error.text[error.text.length - 1] = cast(char)(code);
99 	}