api_c.go 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  1. package goyaml
  2. import (
  3. "io"
  4. "os"
  5. )
  6. ///*
  7. // * Extend a string.
  8. // */
  9. //
  10. //YAML_DECLARE(int)
  11. //yaml_string_extend(yaml_char_t **start,
  12. // yaml_char_t **pointer, yaml_char_t **end)
  13. //{
  14. // yaml_char_t *new_start = yaml_realloc(*start, (*end - *start)*2);
  15. //
  16. // if (!new_start) return 0;
  17. //
  18. // memset(new_start + (*end - *start), 0, *end - *start);
  19. //
  20. // *pointer = new_start + (*pointer - *start);
  21. // *end = new_start + (*end - *start)*2;
  22. // *start = new_start;
  23. //
  24. // return 1;
  25. //}
  26. //
  27. ///*
  28. // * Append a string B to a string A.
  29. // */
  30. //
  31. //YAML_DECLARE(int)
  32. //yaml_string_join(
  33. // yaml_char_t **a_start, yaml_char_t **a_pointer, yaml_char_t **a_end,
  34. // yaml_char_t **b_start, yaml_char_t **b_pointer, yaml_char_t **b_end)
  35. //{
  36. // if (*b_start == *b_pointer)
  37. // return 1;
  38. //
  39. // while (*a_end - *a_pointer <= *b_pointer - *b_start) {
  40. // if (!yaml_string_extend(a_start, a_pointer, a_end))
  41. // return 0;
  42. // }
  43. //
  44. // memcpy(*a_pointer, *b_start, *b_pointer - *b_start);
  45. // *a_pointer += *b_pointer - *b_start;
  46. //
  47. // return 1;
  48. //}
  49. //
  50. ///*
  51. // * Extend a stack.
  52. // */
  53. //
  54. //YAML_DECLARE(int)
  55. //yaml_stack_extend(void **start, void **top, void **end)
  56. //{
  57. // void *new_start = yaml_realloc(*start, ((char *)*end - (char *)*start)*2);
  58. //
  59. // if (!new_start) return 0;
  60. //
  61. // *top = (char *)new_start + ((char *)*top - (char *)*start);
  62. // *end = (char *)new_start + ((char *)*end - (char *)*start)*2;
  63. // *start = new_start;
  64. //
  65. // return 1;
  66. //}
  67. func yaml_insert_token(parser *yaml_parser_t, pos int, token *yaml_token_t) {
  68. // Check if we can move the queue at the beginning of the buffer.
  69. if parser.tokens_head > 0 && len(parser.tokens) == cap(parser.tokens) {
  70. if parser.tokens_head != len(parser.tokens) {
  71. copy(parser.tokens, parser.tokens[parser.tokens_head:])
  72. }
  73. parser.tokens = parser.tokens[:len(parser.tokens)-parser.tokens_head]
  74. parser.tokens_head = 0
  75. }
  76. parser.tokens = append(parser.tokens, yaml_token_t{})
  77. if pos < 0 {
  78. return
  79. }
  80. copy(parser.tokens[parser.tokens_head+pos+1:], parser.tokens[parser.tokens_head+pos:])
  81. parser.tokens[pos] = *token
  82. }
  83. // Create a new parser object.
  84. func yaml_parser_initialize(parser *yaml_parser_t) bool {
  85. // [Go] These should be initialized lazily, and probably start with smaller sizes.
  86. parser.raw_buffer = make([]byte, 0, input_raw_buffer_size)
  87. parser.buffer = make([]byte, 0, input_buffer_size)
  88. parser.tokens = make([]yaml_token_t, 0, initial_queue_size)
  89. parser.indents = make([]int, 0, initial_stack_size)
  90. parser.simple_keys = make([]yaml_simple_key_t, 0, initial_stack_size)
  91. parser.states = make([]yaml_parser_state_t, 0, initial_stack_size)
  92. parser.marks = make([]yaml_mark_t, 0, initial_stack_size)
  93. parser.tag_directives = make([]yaml_tag_directive_t, 0, initial_stack_size)
  94. return true
  95. }
  96. // String read handler.
  97. func yaml_string_read_handler(parser *yaml_parser_t, buffer []byte) (n int, err error) {
  98. if parser.input_pos == len(parser.input) {
  99. return 0, io.EOF
  100. }
  101. n = copy(buffer, parser.input[parser.input_pos:])
  102. parser.input_pos += n
  103. return n, nil
  104. }
  105. // File read handler.
  106. func yaml_file_read_handler(parser *yaml_parser_t, buffer []byte) (n int, err error) {
  107. return parser.input_file.Read(buffer)
  108. }
  109. // Set a string input.
  110. func yaml_parser_set_input_string(parser *yaml_parser_t, input []byte) {
  111. if parser.read_handler != nil {
  112. panic("must set the input source only once")
  113. }
  114. parser.read_handler = yaml_string_read_handler
  115. parser.input = input
  116. parser.input_pos = 0
  117. }
  118. // Set a file input.
  119. func yaml_parser_set_input_file(parser *yaml_parser_t, file *os.File) {
  120. if parser.read_handler != nil {
  121. panic("must set the input source only once")
  122. }
  123. parser.read_handler = yaml_file_read_handler
  124. parser.input_file = file
  125. }
  126. ///*
  127. // * Set the source encoding.
  128. // */
  129. //
  130. //YAML_DECLARE(void)
  131. //yaml_parser_set_encoding(yaml_parser_t *parser, yaml_encoding_t encoding)
  132. //{
  133. // assert(parser); // Non-NULL parser object expected.
  134. // assert(!parser.encoding); // Encoding is already set or detected.
  135. //
  136. // parser.encoding = encoding;
  137. //}
  138. //
  139. ///*
  140. // * Create a new emitter object.
  141. // */
  142. //
  143. //YAML_DECLARE(int)
  144. //yaml_emitter_initialize(yaml_emitter_t *emitter)
  145. //{
  146. // assert(emitter); // Non-NULL emitter object expected.
  147. //
  148. // memset(emitter, 0, sizeof(yaml_emitter_t));
  149. // if (!BUFFER_INIT(emitter, emitter.buffer, OUTPUT_BUFFER_SIZE))
  150. // goto error;
  151. // if (!BUFFER_INIT(emitter, emitter.raw_buffer, OUTPUT_RAW_BUFFER_SIZE))
  152. // goto error;
  153. // if (!STACK_INIT(emitter, emitter.states, INITIAL_STACK_SIZE))
  154. // goto error;
  155. // if (!QUEUE_INIT(emitter, emitter.events, INITIAL_QUEUE_SIZE))
  156. // goto error;
  157. // if (!STACK_INIT(emitter, emitter.indents, INITIAL_STACK_SIZE))
  158. // goto error;
  159. // if (!STACK_INIT(emitter, emitter.tag_directives, INITIAL_STACK_SIZE))
  160. // goto error;
  161. //
  162. // return 1;
  163. //
  164. //error:
  165. //
  166. // BUFFER_DEL(emitter, emitter.buffer);
  167. // BUFFER_DEL(emitter, emitter.raw_buffer);
  168. // STACK_DEL(emitter, emitter.states);
  169. // QUEUE_DEL(emitter, emitter.events);
  170. // STACK_DEL(emitter, emitter.indents);
  171. // STACK_DEL(emitter, emitter.tag_directives);
  172. //
  173. // return 0;
  174. //}
  175. //
  176. ///*
  177. // * Destroy an emitter object.
  178. // */
  179. //
  180. //YAML_DECLARE(void)
  181. //yaml_emitter_delete(yaml_emitter_t *emitter)
  182. //{
  183. // assert(emitter); // Non-NULL emitter object expected.
  184. //
  185. // BUFFER_DEL(emitter, emitter.buffer);
  186. // BUFFER_DEL(emitter, emitter.raw_buffer);
  187. // STACK_DEL(emitter, emitter.states);
  188. // while (!QUEUE_EMPTY(emitter, emitter.events)) {
  189. // yaml_event_delete(&DEQUEUE(emitter, emitter.events));
  190. // }
  191. // QUEUE_DEL(emitter, emitter.events);
  192. // STACK_DEL(emitter, emitter.indents);
  193. // while (!STACK_EMPTY(empty, emitter.tag_directives)) {
  194. // yaml_tag_directive_t tag_directive = POP(emitter, emitter.tag_directives);
  195. // yaml_free(tag_directive.handle);
  196. // yaml_free(tag_directive.prefix);
  197. // }
  198. // STACK_DEL(emitter, emitter.tag_directives);
  199. // yaml_free(emitter.anchors);
  200. //
  201. // memset(emitter, 0, sizeof(yaml_emitter_t));
  202. //}
  203. //
  204. ///*
  205. // * String write handler.
  206. // */
  207. //
  208. //static int
  209. //yaml_string_write_handler(void *data, unsigned char *buffer, size_t size)
  210. //{
  211. // yaml_emitter_t *emitter = data;
  212. //
  213. // if (emitter.output.string.size + *emitter.output.string.size_written
  214. // < size) {
  215. // memcpy(emitter.output.string.buffer
  216. // + *emitter.output.string.size_written,
  217. // buffer,
  218. // emitter.output.string.size
  219. // - *emitter.output.string.size_written);
  220. // *emitter.output.string.size_written = emitter.output.string.size;
  221. // return 0;
  222. // }
  223. //
  224. // memcpy(emitter.output.string.buffer
  225. // + *emitter.output.string.size_written, buffer, size);
  226. // *emitter.output.string.size_written += size;
  227. // return 1;
  228. //}
  229. //
  230. ///*
  231. // * File write handler.
  232. // */
  233. //
  234. //static int
  235. //yaml_file_write_handler(void *data, unsigned char *buffer, size_t size)
  236. //{
  237. // yaml_emitter_t *emitter = data;
  238. //
  239. // return (fwrite(buffer, 1, size, emitter.output.file) == size);
  240. //}
  241. ///*
  242. // * Set a string output.
  243. // */
  244. //
  245. //YAML_DECLARE(void)
  246. //yaml_emitter_set_output_string(yaml_emitter_t *emitter,
  247. // unsigned char *output, size_t size, size_t *size_written)
  248. //{
  249. // assert(emitter); // Non-NULL emitter object expected.
  250. // assert(!emitter.write_handler); // You can set the output only once.
  251. // assert(output); // Non-NULL output string expected.
  252. //
  253. // emitter.write_handler = yaml_string_write_handler;
  254. // emitter.write_handler_data = emitter;
  255. //
  256. // emitter.output.string.buffer = output;
  257. // emitter.output.string.size = size;
  258. // emitter.output.string.size_written = size_written;
  259. // *size_written = 0;
  260. //}
  261. //
  262. ///*
  263. // * Set a file output.
  264. // */
  265. //
  266. //YAML_DECLARE(void)
  267. //yaml_emitter_set_output_file(yaml_emitter_t *emitter, FILE *file)
  268. //{
  269. // assert(emitter); // Non-NULL emitter object expected.
  270. // assert(!emitter.write_handler); // You can set the output only once.
  271. // assert(file); // Non-NULL file object expected.
  272. //
  273. // emitter.write_handler = yaml_file_write_handler;
  274. // emitter.write_handler_data = emitter;
  275. //
  276. // emitter.output.file = file;
  277. //}
  278. //
  279. ///*
  280. // * Set a generic output handler.
  281. // */
  282. //
  283. //YAML_DECLARE(void)
  284. //yaml_emitter_set_output(yaml_emitter_t *emitter,
  285. // yaml_write_handler_t *handler, void *data)
  286. //{
  287. // assert(emitter); // Non-NULL emitter object expected.
  288. // assert(!emitter.write_handler); // You can set the output only once.
  289. // assert(handler); // Non-NULL handler object expected.
  290. //
  291. // emitter.write_handler = handler;
  292. // emitter.write_handler_data = data;
  293. //}
  294. //
  295. ///*
  296. // * Set the output encoding.
  297. // */
  298. //
  299. //YAML_DECLARE(void)
  300. //yaml_emitter_set_encoding(yaml_emitter_t *emitter, yaml_encoding_t encoding)
  301. //{
  302. // assert(emitter); // Non-NULL emitter object expected.
  303. // assert(!emitter.encoding); // You can set encoding only once.
  304. //
  305. // emitter.encoding = encoding;
  306. //}
  307. //
  308. ///*
  309. // * Set the canonical output style.
  310. // */
  311. //
  312. //YAML_DECLARE(void)
  313. //yaml_emitter_set_canonical(yaml_emitter_t *emitter, int canonical)
  314. //{
  315. // assert(emitter); // Non-NULL emitter object expected.
  316. //
  317. // emitter.canonical = (canonical != 0);
  318. //}
  319. //
  320. ///*
  321. // * Set the indentation increment.
  322. // */
  323. //
  324. //YAML_DECLARE(void)
  325. //yaml_emitter_set_indent(yaml_emitter_t *emitter, int indent)
  326. //{
  327. // assert(emitter); // Non-NULL emitter object expected.
  328. //
  329. // emitter.best_indent = (1 < indent && indent < 10) ? indent : 2;
  330. //}
  331. //
  332. ///*
  333. // * Set the preferred line width.
  334. // */
  335. //
  336. //YAML_DECLARE(void)
  337. //yaml_emitter_set_width(yaml_emitter_t *emitter, int width)
  338. //{
  339. // assert(emitter); // Non-NULL emitter object expected.
  340. //
  341. // emitter.best_width = (width >= 0) ? width : -1;
  342. //}
  343. //
  344. ///*
  345. // * Set if unescaped non-ASCII characters are allowed.
  346. // */
  347. //
  348. //YAML_DECLARE(void)
  349. //yaml_emitter_set_unicode(yaml_emitter_t *emitter, int unicode)
  350. //{
  351. // assert(emitter); // Non-NULL emitter object expected.
  352. //
  353. // emitter.unicode = (unicode != 0);
  354. //}
  355. //
  356. ///*
  357. // * Set the preferred line break character.
  358. // */
  359. //
  360. //YAML_DECLARE(void)
  361. //yaml_emitter_set_break(yaml_emitter_t *emitter, yaml_break_t line_break)
  362. //{
  363. // assert(emitter); // Non-NULL emitter object expected.
  364. //
  365. // emitter.line_break = line_break;
  366. //}
  367. //
  368. ///*
  369. // * Destroy a token object.
  370. // */
  371. //
  372. //YAML_DECLARE(void)
  373. //yaml_token_delete(yaml_token_t *token)
  374. //{
  375. // assert(token); // Non-NULL token object expected.
  376. //
  377. // switch (token.type)
  378. // {
  379. // case YAML_TAG_DIRECTIVE_TOKEN:
  380. // yaml_free(token.data.tag_directive.handle);
  381. // yaml_free(token.data.tag_directive.prefix);
  382. // break;
  383. //
  384. // case YAML_ALIAS_TOKEN:
  385. // yaml_free(token.data.alias.value);
  386. // break;
  387. //
  388. // case YAML_ANCHOR_TOKEN:
  389. // yaml_free(token.data.anchor.value);
  390. // break;
  391. //
  392. // case YAML_TAG_TOKEN:
  393. // yaml_free(token.data.tag.handle);
  394. // yaml_free(token.data.tag.suffix);
  395. // break;
  396. //
  397. // case YAML_SCALAR_TOKEN:
  398. // yaml_free(token.data.scalar.value);
  399. // break;
  400. //
  401. // default:
  402. // break;
  403. // }
  404. //
  405. // memset(token, 0, sizeof(yaml_token_t));
  406. //}
  407. //
  408. ///*
  409. // * Check if a string is a valid UTF-8 sequence.
  410. // *
  411. // * Check 'reader.c' for more details on UTF-8 encoding.
  412. // */
  413. //
  414. //static int
  415. //yaml_check_utf8(yaml_char_t *start, size_t length)
  416. //{
  417. // yaml_char_t *end = start+length;
  418. // yaml_char_t *pointer = start;
  419. //
  420. // while (pointer < end) {
  421. // unsigned char octet;
  422. // unsigned int width;
  423. // unsigned int value;
  424. // size_t k;
  425. //
  426. // octet = pointer[0];
  427. // width = (octet & 0x80) == 0x00 ? 1 :
  428. // (octet & 0xE0) == 0xC0 ? 2 :
  429. // (octet & 0xF0) == 0xE0 ? 3 :
  430. // (octet & 0xF8) == 0xF0 ? 4 : 0;
  431. // value = (octet & 0x80) == 0x00 ? octet & 0x7F :
  432. // (octet & 0xE0) == 0xC0 ? octet & 0x1F :
  433. // (octet & 0xF0) == 0xE0 ? octet & 0x0F :
  434. // (octet & 0xF8) == 0xF0 ? octet & 0x07 : 0;
  435. // if (!width) return 0;
  436. // if (pointer+width > end) return 0;
  437. // for (k = 1; k < width; k ++) {
  438. // octet = pointer[k];
  439. // if ((octet & 0xC0) != 0x80) return 0;
  440. // value = (value << 6) + (octet & 0x3F);
  441. // }
  442. // if (!((width == 1) ||
  443. // (width == 2 && value >= 0x80) ||
  444. // (width == 3 && value >= 0x800) ||
  445. // (width == 4 && value >= 0x10000))) return 0;
  446. //
  447. // pointer += width;
  448. // }
  449. //
  450. // return 1;
  451. //}
  452. //
  453. ///*
  454. // * Create STREAM-START.
  455. // */
  456. //
  457. //YAML_DECLARE(int)
  458. //yaml_stream_start_event_initialize(yaml_event_t *event,
  459. // yaml_encoding_t encoding)
  460. //{
  461. // yaml_mark_t mark = { 0, 0, 0 };
  462. //
  463. // assert(event); // Non-NULL event object is expected.
  464. //
  465. // STREAM_START_EVENT_INIT(*event, encoding, mark, mark);
  466. //*event = yaml_event_t{
  467. // typ: yaml_STREAM_START_EVENT,
  468. // start_mark: mark,
  469. // end_mark: mark,
  470. //}
  471. //event.stream_start.encoding = encoding
  472. //
  473. // return 1;
  474. //}
  475. //
  476. ///*
  477. // * Create STREAM-END.
  478. // */
  479. //
  480. //YAML_DECLARE(int)
  481. //yaml_stream_end_event_initialize(yaml_event_t *event)
  482. //{
  483. // yaml_mark_t mark = { 0, 0, 0 };
  484. //
  485. // assert(event); // Non-NULL event object is expected.
  486. //
  487. // STREAM_END_EVENT_INIT(*event, mark, mark);
  488. //*event = yaml_event_t{
  489. // typ: yaml_STREAM_END_EVENT,
  490. // start_mark: mark,
  491. // end_mark: mark,
  492. //}
  493. //
  494. // return 1;
  495. //}
  496. //
  497. ///*
  498. // * Create DOCUMENT-START.
  499. // */
  500. //
  501. //YAML_DECLARE(int)
  502. //yaml_document_start_event_initialize(yaml_event_t *event,
  503. // yaml_version_directive_t *version_directive,
  504. // yaml_tag_directive_t *tag_directives_start,
  505. // yaml_tag_directive_t *tag_directives_end,
  506. // int implicit)
  507. //{
  508. // struct {
  509. // yaml_error_type_t error;
  510. // } context;
  511. // yaml_mark_t mark = { 0, 0, 0 };
  512. // yaml_version_directive_t *version_directive_copy = NULL;
  513. // struct {
  514. // yaml_tag_directive_t *start;
  515. // yaml_tag_directive_t *end;
  516. // yaml_tag_directive_t *top;
  517. // } tag_directives_copy = { NULL, NULL, NULL };
  518. // yaml_tag_directive_t value = { NULL, NULL };
  519. //
  520. // assert(event); // Non-NULL event object is expected.
  521. // assert((tag_directives_start && tag_directives_end) ||
  522. // (tag_directives_start == tag_directives_end));
  523. // // Valid tag directives are expected.
  524. //
  525. // if (version_directive) {
  526. // version_directive_copy = yaml_malloc(sizeof(yaml_version_directive_t));
  527. // if (!version_directive_copy) goto error;
  528. // version_directive_copy.major = version_directive.major;
  529. // version_directive_copy.minor = version_directive.minor;
  530. // }
  531. //
  532. // if (tag_directives_start != tag_directives_end) {
  533. // yaml_tag_directive_t *tag_directive;
  534. // if (!STACK_INIT(&context, tag_directives_copy, INITIAL_STACK_SIZE))
  535. // goto error;
  536. // for (tag_directive = tag_directives_start;
  537. // tag_directive != tag_directives_end; tag_directive ++) {
  538. // assert(tag_directive.handle);
  539. // assert(tag_directive.prefix);
  540. // if (!yaml_check_utf8(tag_directive.handle,
  541. // strlen((char *)tag_directive.handle)))
  542. // goto error;
  543. // if (!yaml_check_utf8(tag_directive.prefix,
  544. // strlen((char *)tag_directive.prefix)))
  545. // goto error;
  546. // value.handle = yaml_strdup(tag_directive.handle);
  547. // value.prefix = yaml_strdup(tag_directive.prefix);
  548. // if (!value.handle || !value.prefix) goto error;
  549. // if (!PUSH(&context, tag_directives_copy, value))
  550. // goto error;
  551. // value.handle = NULL;
  552. // value.prefix = NULL;
  553. // }
  554. // }
  555. //
  556. // DOCUMENT_START_EVENT_INIT(*event, version_directive_copy,
  557. // tag_directives_copy.start, tag_directives_copy.top,
  558. // implicit, mark, mark);
  559. //*event = yaml_event_t{
  560. // typ: yaml_DOCUMENT_START_EVENT,
  561. // start_mark: mark,
  562. // end_mark: mark,
  563. //}
  564. //event.document_start.version_directive = version_directive_copy
  565. //event.document_start.tag_directives = tag_directives
  566. //event.document_start.implicit = implicit
  567. //
  568. // return 1;
  569. //
  570. //error:
  571. // yaml_free(version_directive_copy);
  572. // while (!STACK_EMPTY(context, tag_directives_copy)) {
  573. // yaml_tag_directive_t value = POP(context, tag_directives_copy);
  574. // yaml_free(value.handle);
  575. // yaml_free(value.prefix);
  576. // }
  577. // STACK_DEL(context, tag_directives_copy);
  578. // yaml_free(value.handle);
  579. // yaml_free(value.prefix);
  580. //
  581. // return 0;
  582. //}
  583. //
  584. ///*
  585. // * Create DOCUMENT-END.
  586. // */
  587. //
  588. //YAML_DECLARE(int)
  589. //yaml_document_end_event_initialize(yaml_event_t *event, int implicit)
  590. //{
  591. // yaml_mark_t mark = { 0, 0, 0 };
  592. //
  593. // assert(event); // Non-NULL emitter object is expected.
  594. //
  595. // DOCUMENT_END_EVENT_INIT(*event, implicit, mark, mark);
  596. //
  597. // return 1;
  598. //}
  599. //
  600. ///*
  601. // * Create ALIAS.
  602. // */
  603. //
  604. //YAML_DECLARE(int)
  605. //yaml_alias_event_initialize(yaml_event_t *event, yaml_char_t *anchor)
  606. //{
  607. // yaml_mark_t mark = { 0, 0, 0 };
  608. // yaml_char_t *anchor_copy = NULL;
  609. //
  610. // assert(event); // Non-NULL event object is expected.
  611. // assert(anchor); // Non-NULL anchor is expected.
  612. //
  613. // if (!yaml_check_utf8(anchor, strlen((char *)anchor))) return 0;
  614. //
  615. // anchor_copy = yaml_strdup(anchor);
  616. // if (!anchor_copy)
  617. // return 0;
  618. //
  619. // ALIAS_EVENT_INIT(*event, anchor_copy, mark, mark);
  620. //
  621. // return 1;
  622. //}
  623. //
  624. ///*
  625. // * Create SCALAR.
  626. // */
  627. //
  628. //YAML_DECLARE(int)
  629. //yaml_scalar_event_initialize(yaml_event_t *event,
  630. // yaml_char_t *anchor, yaml_char_t *tag,
  631. // yaml_char_t *value, int length,
  632. // int plain_implicit, int quoted_implicit,
  633. // yaml_scalar_style_t style)
  634. //{
  635. // yaml_mark_t mark = { 0, 0, 0 };
  636. // yaml_char_t *anchor_copy = NULL;
  637. // yaml_char_t *tag_copy = NULL;
  638. // yaml_char_t *value_copy = NULL;
  639. //
  640. // assert(event); // Non-NULL event object is expected.
  641. // assert(value); // Non-NULL anchor is expected.
  642. //
  643. // if (anchor) {
  644. // if (!yaml_check_utf8(anchor, strlen((char *)anchor))) goto error;
  645. // anchor_copy = yaml_strdup(anchor);
  646. // if (!anchor_copy) goto error;
  647. // }
  648. //
  649. // if (tag) {
  650. // if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error;
  651. // tag_copy = yaml_strdup(tag);
  652. // if (!tag_copy) goto error;
  653. // }
  654. //
  655. // if (length < 0) {
  656. // length = strlen((char *)value);
  657. // }
  658. //
  659. // if (!yaml_check_utf8(value, length)) goto error;
  660. // value_copy = yaml_malloc(length+1);
  661. // if (!value_copy) goto error;
  662. // memcpy(value_copy, value, length);
  663. // value_copy[length] = '\0';
  664. //
  665. // SCALAR_EVENT_INIT(*event, anchor_copy, tag_copy, value_copy, length,
  666. // plain_implicit, quoted_implicit, style, mark, mark);
  667. //
  668. // return 1;
  669. //
  670. //error:
  671. // yaml_free(anchor_copy);
  672. // yaml_free(tag_copy);
  673. // yaml_free(value_copy);
  674. //
  675. // return 0;
  676. //}
  677. //
  678. ///*
  679. // * Create SEQUENCE-START.
  680. // */
  681. //
  682. //YAML_DECLARE(int)
  683. //yaml_sequence_start_event_initialize(yaml_event_t *event,
  684. // yaml_char_t *anchor, yaml_char_t *tag, int implicit,
  685. // yaml_sequence_style_t style)
  686. //{
  687. // yaml_mark_t mark = { 0, 0, 0 };
  688. // yaml_char_t *anchor_copy = NULL;
  689. // yaml_char_t *tag_copy = NULL;
  690. //
  691. // assert(event); // Non-NULL event object is expected.
  692. //
  693. // if (anchor) {
  694. // if (!yaml_check_utf8(anchor, strlen((char *)anchor))) goto error;
  695. // anchor_copy = yaml_strdup(anchor);
  696. // if (!anchor_copy) goto error;
  697. // }
  698. //
  699. // if (tag) {
  700. // if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error;
  701. // tag_copy = yaml_strdup(tag);
  702. // if (!tag_copy) goto error;
  703. // }
  704. //
  705. // SEQUENCE_START_EVENT_INIT(*event, anchor_copy, tag_copy,
  706. // implicit, style, mark, mark);
  707. //
  708. // return 1;
  709. //
  710. //error:
  711. // yaml_free(anchor_copy);
  712. // yaml_free(tag_copy);
  713. //
  714. // return 0;
  715. //}
  716. //
  717. ///*
  718. // * Create SEQUENCE-END.
  719. // */
  720. //
  721. //YAML_DECLARE(int)
  722. //yaml_sequence_end_event_initialize(yaml_event_t *event)
  723. //{
  724. // yaml_mark_t mark = { 0, 0, 0 };
  725. //
  726. // assert(event); // Non-NULL event object is expected.
  727. //
  728. // SEQUENCE_END_EVENT_INIT(*event, mark, mark);
  729. //
  730. // return 1;
  731. //}
  732. //
  733. ///*
  734. // * Create MAPPING-START.
  735. // */
  736. //
  737. //YAML_DECLARE(int)
  738. //yaml_mapping_start_event_initialize(yaml_event_t *event,
  739. // yaml_char_t *anchor, yaml_char_t *tag, int implicit,
  740. // yaml_mapping_style_t style)
  741. //{
  742. // yaml_mark_t mark = { 0, 0, 0 };
  743. // yaml_char_t *anchor_copy = NULL;
  744. // yaml_char_t *tag_copy = NULL;
  745. //
  746. // assert(event); // Non-NULL event object is expected.
  747. //
  748. // if (anchor) {
  749. // if (!yaml_check_utf8(anchor, strlen((char *)anchor))) goto error;
  750. // anchor_copy = yaml_strdup(anchor);
  751. // if (!anchor_copy) goto error;
  752. // }
  753. //
  754. // if (tag) {
  755. // if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error;
  756. // tag_copy = yaml_strdup(tag);
  757. // if (!tag_copy) goto error;
  758. // }
  759. //
  760. // MAPPING_START_EVENT_INIT(*event, anchor_copy, tag_copy,
  761. // implicit, style, mark, mark);
  762. //
  763. // return 1;
  764. //
  765. //error:
  766. // yaml_free(anchor_copy);
  767. // yaml_free(tag_copy);
  768. //
  769. // return 0;
  770. //}
  771. //
  772. ///*
  773. // * Create MAPPING-END.
  774. // */
  775. //
  776. //YAML_DECLARE(int)
  777. //yaml_mapping_end_event_initialize(yaml_event_t *event)
  778. //{
  779. // yaml_mark_t mark = { 0, 0, 0 };
  780. //
  781. // assert(event); // Non-NULL event object is expected.
  782. //
  783. // MAPPING_END_EVENT_INIT(*event, mark, mark);
  784. //
  785. // return 1;
  786. //}
  787. ///*
  788. // * Create a document object.
  789. // */
  790. //
  791. //YAML_DECLARE(int)
  792. //yaml_document_initialize(yaml_document_t *document,
  793. // yaml_version_directive_t *version_directive,
  794. // yaml_tag_directive_t *tag_directives_start,
  795. // yaml_tag_directive_t *tag_directives_end,
  796. // int start_implicit, int end_implicit)
  797. //{
  798. // struct {
  799. // yaml_error_type_t error;
  800. // } context;
  801. // struct {
  802. // yaml_node_t *start;
  803. // yaml_node_t *end;
  804. // yaml_node_t *top;
  805. // } nodes = { NULL, NULL, NULL };
  806. // yaml_version_directive_t *version_directive_copy = NULL;
  807. // struct {
  808. // yaml_tag_directive_t *start;
  809. // yaml_tag_directive_t *end;
  810. // yaml_tag_directive_t *top;
  811. // } tag_directives_copy = { NULL, NULL, NULL };
  812. // yaml_tag_directive_t value = { NULL, NULL };
  813. // yaml_mark_t mark = { 0, 0, 0 };
  814. //
  815. // assert(document); // Non-NULL document object is expected.
  816. // assert((tag_directives_start && tag_directives_end) ||
  817. // (tag_directives_start == tag_directives_end));
  818. // // Valid tag directives are expected.
  819. //
  820. // if (!STACK_INIT(&context, nodes, INITIAL_STACK_SIZE)) goto error;
  821. //
  822. // if (version_directive) {
  823. // version_directive_copy = yaml_malloc(sizeof(yaml_version_directive_t));
  824. // if (!version_directive_copy) goto error;
  825. // version_directive_copy.major = version_directive.major;
  826. // version_directive_copy.minor = version_directive.minor;
  827. // }
  828. //
  829. // if (tag_directives_start != tag_directives_end) {
  830. // yaml_tag_directive_t *tag_directive;
  831. // if (!STACK_INIT(&context, tag_directives_copy, INITIAL_STACK_SIZE))
  832. // goto error;
  833. // for (tag_directive = tag_directives_start;
  834. // tag_directive != tag_directives_end; tag_directive ++) {
  835. // assert(tag_directive.handle);
  836. // assert(tag_directive.prefix);
  837. // if (!yaml_check_utf8(tag_directive.handle,
  838. // strlen((char *)tag_directive.handle)))
  839. // goto error;
  840. // if (!yaml_check_utf8(tag_directive.prefix,
  841. // strlen((char *)tag_directive.prefix)))
  842. // goto error;
  843. // value.handle = yaml_strdup(tag_directive.handle);
  844. // value.prefix = yaml_strdup(tag_directive.prefix);
  845. // if (!value.handle || !value.prefix) goto error;
  846. // if (!PUSH(&context, tag_directives_copy, value))
  847. // goto error;
  848. // value.handle = NULL;
  849. // value.prefix = NULL;
  850. // }
  851. // }
  852. //
  853. // DOCUMENT_INIT(*document, nodes.start, nodes.end, version_directive_copy,
  854. // tag_directives_copy.start, tag_directives_copy.top,
  855. // start_implicit, end_implicit, mark, mark);
  856. //
  857. // return 1;
  858. //
  859. //error:
  860. // STACK_DEL(&context, nodes);
  861. // yaml_free(version_directive_copy);
  862. // while (!STACK_EMPTY(&context, tag_directives_copy)) {
  863. // yaml_tag_directive_t value = POP(&context, tag_directives_copy);
  864. // yaml_free(value.handle);
  865. // yaml_free(value.prefix);
  866. // }
  867. // STACK_DEL(&context, tag_directives_copy);
  868. // yaml_free(value.handle);
  869. // yaml_free(value.prefix);
  870. //
  871. // return 0;
  872. //}
  873. //
  874. ///*
  875. // * Destroy a document object.
  876. // */
  877. //
  878. //YAML_DECLARE(void)
  879. //yaml_document_delete(yaml_document_t *document)
  880. //{
  881. // struct {
  882. // yaml_error_type_t error;
  883. // } context;
  884. // yaml_tag_directive_t *tag_directive;
  885. //
  886. // context.error = YAML_NO_ERROR; // Eliminate a compliler warning.
  887. //
  888. // assert(document); // Non-NULL document object is expected.
  889. //
  890. // while (!STACK_EMPTY(&context, document.nodes)) {
  891. // yaml_node_t node = POP(&context, document.nodes);
  892. // yaml_free(node.tag);
  893. // switch (node.type) {
  894. // case YAML_SCALAR_NODE:
  895. // yaml_free(node.data.scalar.value);
  896. // break;
  897. // case YAML_SEQUENCE_NODE:
  898. // STACK_DEL(&context, node.data.sequence.items);
  899. // break;
  900. // case YAML_MAPPING_NODE:
  901. // STACK_DEL(&context, node.data.mapping.pairs);
  902. // break;
  903. // default:
  904. // assert(0); // Should not happen.
  905. // }
  906. // }
  907. // STACK_DEL(&context, document.nodes);
  908. //
  909. // yaml_free(document.version_directive);
  910. // for (tag_directive = document.tag_directives.start;
  911. // tag_directive != document.tag_directives.end;
  912. // tag_directive++) {
  913. // yaml_free(tag_directive.handle);
  914. // yaml_free(tag_directive.prefix);
  915. // }
  916. // yaml_free(document.tag_directives.start);
  917. //
  918. // memset(document, 0, sizeof(yaml_document_t));
  919. //}
  920. //
  921. ///**
  922. // * Get a document node.
  923. // */
  924. //
  925. //YAML_DECLARE(yaml_node_t *)
  926. //yaml_document_get_node(yaml_document_t *document, int index)
  927. //{
  928. // assert(document); // Non-NULL document object is expected.
  929. //
  930. // if (index > 0 && document.nodes.start + index <= document.nodes.top) {
  931. // return document.nodes.start + index - 1;
  932. // }
  933. // return NULL;
  934. //}
  935. //
  936. ///**
  937. // * Get the root object.
  938. // */
  939. //
  940. //YAML_DECLARE(yaml_node_t *)
  941. //yaml_document_get_root_node(yaml_document_t *document)
  942. //{
  943. // assert(document); // Non-NULL document object is expected.
  944. //
  945. // if (document.nodes.top != document.nodes.start) {
  946. // return document.nodes.start;
  947. // }
  948. // return NULL;
  949. //}
  950. //
  951. ///*
  952. // * Add a scalar node to a document.
  953. // */
  954. //
  955. //YAML_DECLARE(int)
  956. //yaml_document_add_scalar(yaml_document_t *document,
  957. // yaml_char_t *tag, yaml_char_t *value, int length,
  958. // yaml_scalar_style_t style)
  959. //{
  960. // struct {
  961. // yaml_error_type_t error;
  962. // } context;
  963. // yaml_mark_t mark = { 0, 0, 0 };
  964. // yaml_char_t *tag_copy = NULL;
  965. // yaml_char_t *value_copy = NULL;
  966. // yaml_node_t node;
  967. //
  968. // assert(document); // Non-NULL document object is expected.
  969. // assert(value); // Non-NULL value is expected.
  970. //
  971. // if (!tag) {
  972. // tag = (yaml_char_t *)YAML_DEFAULT_SCALAR_TAG;
  973. // }
  974. //
  975. // if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error;
  976. // tag_copy = yaml_strdup(tag);
  977. // if (!tag_copy) goto error;
  978. //
  979. // if (length < 0) {
  980. // length = strlen((char *)value);
  981. // }
  982. //
  983. // if (!yaml_check_utf8(value, length)) goto error;
  984. // value_copy = yaml_malloc(length+1);
  985. // if (!value_copy) goto error;
  986. // memcpy(value_copy, value, length);
  987. // value_copy[length] = '\0';
  988. //
  989. // SCALAR_NODE_INIT(node, tag_copy, value_copy, length, style, mark, mark);
  990. // if (!PUSH(&context, document.nodes, node)) goto error;
  991. //
  992. // return document.nodes.top - document.nodes.start;
  993. //
  994. //error:
  995. // yaml_free(tag_copy);
  996. // yaml_free(value_copy);
  997. //
  998. // return 0;
  999. //}
  1000. //
  1001. ///*
  1002. // * Add a sequence node to a document.
  1003. // */
  1004. //
  1005. //YAML_DECLARE(int)
  1006. //yaml_document_add_sequence(yaml_document_t *document,
  1007. // yaml_char_t *tag, yaml_sequence_style_t style)
  1008. //{
  1009. // struct {
  1010. // yaml_error_type_t error;
  1011. // } context;
  1012. // yaml_mark_t mark = { 0, 0, 0 };
  1013. // yaml_char_t *tag_copy = NULL;
  1014. // struct {
  1015. // yaml_node_item_t *start;
  1016. // yaml_node_item_t *end;
  1017. // yaml_node_item_t *top;
  1018. // } items = { NULL, NULL, NULL };
  1019. // yaml_node_t node;
  1020. //
  1021. // assert(document); // Non-NULL document object is expected.
  1022. //
  1023. // if (!tag) {
  1024. // tag = (yaml_char_t *)YAML_DEFAULT_SEQUENCE_TAG;
  1025. // }
  1026. //
  1027. // if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error;
  1028. // tag_copy = yaml_strdup(tag);
  1029. // if (!tag_copy) goto error;
  1030. //
  1031. // if (!STACK_INIT(&context, items, INITIAL_STACK_SIZE)) goto error;
  1032. //
  1033. // SEQUENCE_NODE_INIT(node, tag_copy, items.start, items.end,
  1034. // style, mark, mark);
  1035. // if (!PUSH(&context, document.nodes, node)) goto error;
  1036. //
  1037. // return document.nodes.top - document.nodes.start;
  1038. //
  1039. //error:
  1040. // STACK_DEL(&context, items);
  1041. // yaml_free(tag_copy);
  1042. //
  1043. // return 0;
  1044. //}
  1045. //
  1046. ///*
  1047. // * Add a mapping node to a document.
  1048. // */
  1049. //
  1050. //YAML_DECLARE(int)
  1051. //yaml_document_add_mapping(yaml_document_t *document,
  1052. // yaml_char_t *tag, yaml_mapping_style_t style)
  1053. //{
  1054. // struct {
  1055. // yaml_error_type_t error;
  1056. // } context;
  1057. // yaml_mark_t mark = { 0, 0, 0 };
  1058. // yaml_char_t *tag_copy = NULL;
  1059. // struct {
  1060. // yaml_node_pair_t *start;
  1061. // yaml_node_pair_t *end;
  1062. // yaml_node_pair_t *top;
  1063. // } pairs = { NULL, NULL, NULL };
  1064. // yaml_node_t node;
  1065. //
  1066. // assert(document); // Non-NULL document object is expected.
  1067. //
  1068. // if (!tag) {
  1069. // tag = (yaml_char_t *)YAML_DEFAULT_MAPPING_TAG;
  1070. // }
  1071. //
  1072. // if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error;
  1073. // tag_copy = yaml_strdup(tag);
  1074. // if (!tag_copy) goto error;
  1075. //
  1076. // if (!STACK_INIT(&context, pairs, INITIAL_STACK_SIZE)) goto error;
  1077. //
  1078. // MAPPING_NODE_INIT(node, tag_copy, pairs.start, pairs.end,
  1079. // style, mark, mark);
  1080. // if (!PUSH(&context, document.nodes, node)) goto error;
  1081. //
  1082. // return document.nodes.top - document.nodes.start;
  1083. //
  1084. //error:
  1085. // STACK_DEL(&context, pairs);
  1086. // yaml_free(tag_copy);
  1087. //
  1088. // return 0;
  1089. //}
  1090. //
  1091. ///*
  1092. // * Append an item to a sequence node.
  1093. // */
  1094. //
  1095. //YAML_DECLARE(int)
  1096. //yaml_document_append_sequence_item(yaml_document_t *document,
  1097. // int sequence, int item)
  1098. //{
  1099. // struct {
  1100. // yaml_error_type_t error;
  1101. // } context;
  1102. //
  1103. // assert(document); // Non-NULL document is required.
  1104. // assert(sequence > 0
  1105. // && document.nodes.start + sequence <= document.nodes.top);
  1106. // // Valid sequence id is required.
  1107. // assert(document.nodes.start[sequence-1].type == YAML_SEQUENCE_NODE);
  1108. // // A sequence node is required.
  1109. // assert(item > 0 && document.nodes.start + item <= document.nodes.top);
  1110. // // Valid item id is required.
  1111. //
  1112. // if (!PUSH(&context,
  1113. // document.nodes.start[sequence-1].data.sequence.items, item))
  1114. // return 0;
  1115. //
  1116. // return 1;
  1117. //}
  1118. //
  1119. ///*
  1120. // * Append a pair of a key and a value to a mapping node.
  1121. // */
  1122. //
  1123. //YAML_DECLARE(int)
  1124. //yaml_document_append_mapping_pair(yaml_document_t *document,
  1125. // int mapping, int key, int value)
  1126. //{
  1127. // struct {
  1128. // yaml_error_type_t error;
  1129. // } context;
  1130. //
  1131. // yaml_node_pair_t pair;
  1132. //
  1133. // assert(document); // Non-NULL document is required.
  1134. // assert(mapping > 0
  1135. // && document.nodes.start + mapping <= document.nodes.top);
  1136. // // Valid mapping id is required.
  1137. // assert(document.nodes.start[mapping-1].type == YAML_MAPPING_NODE);
  1138. // // A mapping node is required.
  1139. // assert(key > 0 && document.nodes.start + key <= document.nodes.top);
  1140. // // Valid key id is required.
  1141. // assert(value > 0 && document.nodes.start + value <= document.nodes.top);
  1142. // // Valid value id is required.
  1143. //
  1144. // pair.key = key;
  1145. // pair.value = value;
  1146. //
  1147. // if (!PUSH(&context,
  1148. // document.nodes.start[mapping-1].data.mapping.pairs, pair))
  1149. // return 0;
  1150. //
  1151. // return 1;
  1152. //}
  1153. //
  1154. //