api_c.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. package goyaml
  2. import (
  3. "io"
  4. "os"
  5. )
  6. func yaml_insert_token(parser *yaml_parser_t, pos int, token *yaml_token_t) {
  7. //fmt.Println("yaml_insert_token", "pos:", pos, "typ:", token.typ, "head:", parser.tokens_head, "len:", len(parser.tokens))
  8. // Check if we can move the queue at the beginning of the buffer.
  9. if parser.tokens_head > 0 && len(parser.tokens) == cap(parser.tokens) {
  10. if parser.tokens_head != len(parser.tokens) {
  11. copy(parser.tokens, parser.tokens[parser.tokens_head:])
  12. }
  13. parser.tokens = parser.tokens[:len(parser.tokens)-parser.tokens_head]
  14. parser.tokens_head = 0
  15. }
  16. parser.tokens = append(parser.tokens, *token)
  17. if pos < 0 {
  18. return
  19. }
  20. copy(parser.tokens[parser.tokens_head+pos+1:], parser.tokens[parser.tokens_head+pos:])
  21. parser.tokens[parser.tokens_head+pos] = *token
  22. }
  23. // Create a new parser object.
  24. func yaml_parser_initialize(parser *yaml_parser_t) bool {
  25. *parser = yaml_parser_t{
  26. raw_buffer: make([]byte, 0, input_raw_buffer_size),
  27. buffer: make([]byte, 0, input_buffer_size),
  28. //tokens: make([]yaml_token_t, 0, initial_queue_size),
  29. //indents: make([]int, 0, initial_stack_size),
  30. //simple_keys: make([]yaml_simple_key_t, 0, initial_stack_size),
  31. //states: make([]yaml_parser_state_t, 0, initial_stack_size),
  32. //marks: make([]yaml_mark_t, 0, initial_stack_size),
  33. //tag_directives: make([]yaml_tag_directive_t, 0, initial_stack_size),
  34. }
  35. return true
  36. }
  37. // Destroy a parser object.
  38. func yaml_parser_delete(parser *yaml_parser_t) {
  39. *parser = yaml_parser_t{}
  40. }
  41. // String read handler.
  42. func yaml_string_read_handler(parser *yaml_parser_t, buffer []byte) (n int, err error) {
  43. if parser.input_pos == len(parser.input) {
  44. return 0, io.EOF
  45. }
  46. n = copy(buffer, parser.input[parser.input_pos:])
  47. parser.input_pos += n
  48. return n, nil
  49. }
  50. // File read handler.
  51. func yaml_file_read_handler(parser *yaml_parser_t, buffer []byte) (n int, err error) {
  52. return parser.input_file.Read(buffer)
  53. }
  54. // Set a string input.
  55. func yaml_parser_set_input_string(parser *yaml_parser_t, input []byte) {
  56. if parser.read_handler != nil {
  57. panic("must set the input source only once")
  58. }
  59. parser.read_handler = yaml_string_read_handler
  60. parser.input = input
  61. parser.input_pos = 0
  62. }
  63. // Set a file input.
  64. func yaml_parser_set_input_file(parser *yaml_parser_t, file *os.File) {
  65. if parser.read_handler != nil {
  66. panic("must set the input source only once")
  67. }
  68. parser.read_handler = yaml_file_read_handler
  69. parser.input_file = file
  70. }
  71. // Set the source encoding.
  72. func yaml_parser_set_encoding(parser *yaml_parser_t, encoding yaml_encoding_t) {
  73. if parser.encoding != yaml_ANY_ENCODING {
  74. panic("must set the encoding only once")
  75. }
  76. parser.encoding = encoding
  77. }
  78. // Create a new emitter object.
  79. func yaml_emitter_initialize(emitter *yaml_emitter_t) bool {
  80. // [Go] These should be initialized lazily instead.
  81. *emitter = yaml_emitter_t{
  82. buffer: make([]byte, output_buffer_size),
  83. raw_buffer: make([]byte, 0, output_raw_buffer_size),
  84. states: make([]yaml_emitter_state_t, 0, initial_stack_size),
  85. events: make([]yaml_event_t, 0, initial_queue_size),
  86. indents: make([]int, 0, initial_stack_size),
  87. tag_directives: make([]yaml_tag_directive_t, 0, initial_stack_size),
  88. }
  89. return true
  90. }
  91. // Destroy an emitter object.
  92. func yaml_emitter_delete(emitter *yaml_emitter_t) {
  93. *emitter = yaml_emitter_t{}
  94. }
  95. // String write handler.
  96. func yaml_string_write_handler(emitter *yaml_emitter_t, buffer []byte) error {
  97. *emitter.output_buffer = append(*emitter.output_buffer, buffer...)
  98. return nil
  99. }
  100. // File write handler.
  101. func yaml_file_write_handler(emitter *yaml_emitter_t, buffer []byte) error {
  102. _, err := emitter.output_file.Write(buffer)
  103. return err
  104. }
  105. // Set a string output.
  106. func yaml_emitter_set_output_string(emitter *yaml_emitter_t, output_buffer *[]byte) {
  107. if emitter.write_handler != nil {
  108. panic("must set the output target only once")
  109. }
  110. emitter.write_handler = yaml_string_write_handler
  111. emitter.output_buffer = output_buffer
  112. }
  113. // Set a file output.
  114. func yaml_emitter_set_output_file(emitter *yaml_emitter_t, file io.Writer) {
  115. if emitter.write_handler != nil {
  116. panic("must set the output target only once")
  117. }
  118. emitter.write_handler = yaml_file_write_handler
  119. emitter.output_file = file
  120. }
  121. // Set the output encoding.
  122. func yaml_emitter_set_encoding(emitter *yaml_emitter_t, encoding yaml_encoding_t) {
  123. if emitter.encoding != yaml_ANY_ENCODING {
  124. panic("must set the output encoding only once")
  125. }
  126. emitter.encoding = encoding
  127. }
  128. // Set the canonical output style.
  129. func yaml_emitter_set_canonical(emitter *yaml_emitter_t, canonical bool) {
  130. emitter.canonical = canonical
  131. }
  132. //// Set the indentation increment.
  133. func yaml_emitter_set_indent(emitter *yaml_emitter_t, indent int) {
  134. if indent < 2 || indent > 9 {
  135. indent = 2
  136. }
  137. emitter.best_indent = indent
  138. }
  139. // Set the preferred line width.
  140. func yaml_emitter_set_width(emitter *yaml_emitter_t, width int) {
  141. if width < 0 {
  142. width = -1
  143. }
  144. emitter.best_width = width
  145. }
  146. // Set if unescaped non-ASCII characters are allowed.
  147. func yaml_emitter_set_unicode(emitter *yaml_emitter_t, unicode bool) {
  148. emitter.unicode = unicode
  149. }
  150. // Set the preferred line break character.
  151. func yaml_emitter_set_break(emitter *yaml_emitter_t, line_break yaml_break_t) {
  152. emitter.line_break = line_break
  153. }
  154. ///*
  155. // * Destroy a token object.
  156. // */
  157. //
  158. //YAML_DECLARE(void)
  159. //yaml_token_delete(yaml_token_t *token)
  160. //{
  161. // assert(token); // Non-NULL token object expected.
  162. //
  163. // switch (token.type)
  164. // {
  165. // case YAML_TAG_DIRECTIVE_TOKEN:
  166. // yaml_free(token.data.tag_directive.handle);
  167. // yaml_free(token.data.tag_directive.prefix);
  168. // break;
  169. //
  170. // case YAML_ALIAS_TOKEN:
  171. // yaml_free(token.data.alias.value);
  172. // break;
  173. //
  174. // case YAML_ANCHOR_TOKEN:
  175. // yaml_free(token.data.anchor.value);
  176. // break;
  177. //
  178. // case YAML_TAG_TOKEN:
  179. // yaml_free(token.data.tag.handle);
  180. // yaml_free(token.data.tag.suffix);
  181. // break;
  182. //
  183. // case YAML_SCALAR_TOKEN:
  184. // yaml_free(token.data.scalar.value);
  185. // break;
  186. //
  187. // default:
  188. // break;
  189. // }
  190. //
  191. // memset(token, 0, sizeof(yaml_token_t));
  192. //}
  193. //
  194. ///*
  195. // * Check if a string is a valid UTF-8 sequence.
  196. // *
  197. // * Check 'reader.c' for more details on UTF-8 encoding.
  198. // */
  199. //
  200. //static int
  201. //yaml_check_utf8(yaml_char_t *start, size_t length)
  202. //{
  203. // yaml_char_t *end = start+length;
  204. // yaml_char_t *pointer = start;
  205. //
  206. // while (pointer < end) {
  207. // unsigned char octet;
  208. // unsigned int width;
  209. // unsigned int value;
  210. // size_t k;
  211. //
  212. // octet = pointer[0];
  213. // width = (octet & 0x80) == 0x00 ? 1 :
  214. // (octet & 0xE0) == 0xC0 ? 2 :
  215. // (octet & 0xF0) == 0xE0 ? 3 :
  216. // (octet & 0xF8) == 0xF0 ? 4 : 0;
  217. // value = (octet & 0x80) == 0x00 ? octet & 0x7F :
  218. // (octet & 0xE0) == 0xC0 ? octet & 0x1F :
  219. // (octet & 0xF0) == 0xE0 ? octet & 0x0F :
  220. // (octet & 0xF8) == 0xF0 ? octet & 0x07 : 0;
  221. // if (!width) return 0;
  222. // if (pointer+width > end) return 0;
  223. // for (k = 1; k < width; k ++) {
  224. // octet = pointer[k];
  225. // if ((octet & 0xC0) != 0x80) return 0;
  226. // value = (value << 6) + (octet & 0x3F);
  227. // }
  228. // if (!((width == 1) ||
  229. // (width == 2 && value >= 0x80) ||
  230. // (width == 3 && value >= 0x800) ||
  231. // (width == 4 && value >= 0x10000))) return 0;
  232. //
  233. // pointer += width;
  234. // }
  235. //
  236. // return 1;
  237. //}
  238. //
  239. // Create STREAM-START.
  240. func yaml_stream_start_event_initialize(event *yaml_event_t, encoding yaml_encoding_t) bool {
  241. *event = yaml_event_t{
  242. typ: yaml_STREAM_START_EVENT,
  243. }
  244. event.stream_start.encoding = encoding
  245. return true
  246. }
  247. // Create STREAM-END.
  248. func yaml_stream_end_event_initialize(event *yaml_event_t) bool {
  249. *event = yaml_event_t{
  250. typ: yaml_STREAM_END_EVENT,
  251. }
  252. return true
  253. }
  254. // Create DOCUMENT-START.
  255. func yaml_document_start_event_initialize(event *yaml_event_t, version_directive *yaml_version_directive_t,
  256. tag_directives []yaml_tag_directive_t, implicit bool) bool {
  257. // [Go] It doesn't sound necessary to perform these copies
  258. // given that with garbage collection ownership is handled.
  259. var version_directive_copy *yaml_version_directive_t
  260. var tag_directives_copy []yaml_tag_directive_t
  261. if version_directive != nil {
  262. copy := *version_directive
  263. version_directive_copy = &copy
  264. }
  265. if len(tag_directives) > 0 {
  266. tag_directives_copy = append([]yaml_tag_directive_t(nil), tag_directives...)
  267. }
  268. *event = yaml_event_t{
  269. typ: yaml_DOCUMENT_START_EVENT,
  270. }
  271. event.document_start.version_directive = version_directive_copy
  272. event.document_start.tag_directives = tag_directives_copy
  273. event.document_start.implicit = implicit
  274. return true
  275. }
  276. // Create DOCUMENT-END.
  277. func yaml_document_end_event_initialize(event *yaml_event_t, implicit bool) bool {
  278. *event = yaml_event_t{
  279. typ: yaml_DOCUMENT_END_EVENT,
  280. }
  281. event.document_end.implicit = implicit
  282. return true
  283. }
  284. ///*
  285. // * Create ALIAS.
  286. // */
  287. //
  288. //YAML_DECLARE(int)
  289. //yaml_alias_event_initialize(event *yaml_event_t, anchor *yaml_char_t)
  290. //{
  291. // mark yaml_mark_t = { 0, 0, 0 }
  292. // anchor_copy *yaml_char_t = NULL
  293. //
  294. // assert(event) // Non-NULL event object is expected.
  295. // assert(anchor) // Non-NULL anchor is expected.
  296. //
  297. // if (!yaml_check_utf8(anchor, strlen((char *)anchor))) return 0
  298. //
  299. // anchor_copy = yaml_strdup(anchor)
  300. // if (!anchor_copy)
  301. // return 0
  302. //
  303. // ALIAS_EVENT_INIT(*event, anchor_copy, mark, mark)
  304. //
  305. // return 1
  306. //}
  307. // Create SCALAR.
  308. func yaml_scalar_event_initialize(event *yaml_event_t, anchor, tag, value []byte,
  309. plain_implicit, quoted_implicit bool, style yaml_scalar_style_t) bool {
  310. var anchor_copy, tag_copy, value_copy []byte
  311. // [Go] These copies are probably not necessary in Go, where
  312. // ownership of data is more flexible due to garbage collection.
  313. if len(anchor) > 0 {
  314. //if !yaml_check_utf8(anchor) { return false }
  315. anchor_copy = append([]byte(nil), anchor...)
  316. }
  317. if len(tag) > 0 {
  318. //if !yaml_check_utf8(tag) { return false }
  319. tag_copy = append([]byte(nil), tag...)
  320. }
  321. //if !yaml_check_utf8(value) { return false }
  322. value_copy = append([]byte(nil), value...)
  323. *event = yaml_event_t{
  324. typ: yaml_SCALAR_EVENT,
  325. }
  326. event.scalar.anchor = anchor_copy
  327. event.scalar.tag = tag_copy
  328. event.scalar.value = value_copy
  329. event.scalar.plain_implicit = plain_implicit
  330. event.scalar.quoted_implicit = quoted_implicit
  331. event.scalar.style = style
  332. return true
  333. }
  334. // Create SEQUENCE-START.
  335. func yaml_sequence_start_event_initialize(event *yaml_event_t, anchor, tag []byte, implicit bool, style yaml_sequence_style_t) bool {
  336. // [Go] These copies are probably not necessary in Go, where
  337. // ownership of data is more flexible due to garbage collection.
  338. var anchor_copy, tag_copy []byte
  339. if len(anchor) > 0 {
  340. //if !yaml_check_utf8(anchor) { return false }
  341. anchor_copy = append([]byte(nil), anchor...)
  342. }
  343. if len(tag) > 0 {
  344. //if !yaml_check_utf8(tag) { return false }
  345. tag_copy = append([]byte(nil), tag...)
  346. }
  347. *event = yaml_event_t{
  348. typ: yaml_SEQUENCE_START_EVENT,
  349. }
  350. event.sequence_start.anchor = anchor_copy
  351. event.sequence_start.tag = tag_copy
  352. event.sequence_start.implicit = implicit
  353. event.sequence_start.style = style
  354. return true
  355. }
  356. // Create SEQUENCE-END.
  357. func yaml_sequence_end_event_initialize(event *yaml_event_t) bool {
  358. *event = yaml_event_t{
  359. typ: yaml_SEQUENCE_END_EVENT,
  360. }
  361. return true
  362. }
  363. // Create MAPPING-START.
  364. func yaml_mapping_start_event_initialize(event *yaml_event_t, anchor, tag []byte, implicit bool, style yaml_mapping_style_t) bool {
  365. // [Go] These copies are probably not necessary in Go, where
  366. // ownership of data is more flexible due to garbage collection.
  367. var anchor_copy, tag_copy []byte
  368. if len(anchor) > 0 {
  369. //if !yaml_check_utf8(anchor) { return false }
  370. anchor_copy = append([]byte(nil), anchor...)
  371. }
  372. if len(tag) > 0 {
  373. //if !yaml_check_utf8(tag) { return false }
  374. tag_copy = append([]byte(nil), tag...)
  375. }
  376. *event = yaml_event_t{
  377. typ: yaml_MAPPING_START_EVENT,
  378. }
  379. event.mapping_start.anchor = anchor_copy
  380. event.mapping_start.tag = tag_copy
  381. event.mapping_start.implicit = implicit
  382. event.mapping_start.style = style
  383. return true
  384. }
  385. // Create MAPPING-END.
  386. func yaml_mapping_end_event_initialize(event *yaml_event_t) bool {
  387. *event = yaml_event_t{
  388. typ: yaml_MAPPING_END_EVENT,
  389. }
  390. return true
  391. }
  392. // Destroy an event object.
  393. func yaml_event_delete(event *yaml_event_t) {
  394. *event = yaml_event_t{}
  395. }
  396. ///*
  397. // * Create a document object.
  398. // */
  399. //
  400. //YAML_DECLARE(int)
  401. //yaml_document_initialize(document *yaml_document_t,
  402. // version_directive *yaml_version_directive_t,
  403. // tag_directives_start *yaml_tag_directive_t,
  404. // tag_directives_end *yaml_tag_directive_t,
  405. // start_implicit int, end_implicit int)
  406. //{
  407. // struct {
  408. // error yaml_error_type_t
  409. // } context
  410. // struct {
  411. // start *yaml_node_t
  412. // end *yaml_node_t
  413. // top *yaml_node_t
  414. // } nodes = { NULL, NULL, NULL }
  415. // version_directive_copy *yaml_version_directive_t = NULL
  416. // struct {
  417. // start *yaml_tag_directive_t
  418. // end *yaml_tag_directive_t
  419. // top *yaml_tag_directive_t
  420. // } tag_directives_copy = { NULL, NULL, NULL }
  421. // value yaml_tag_directive_t = { NULL, NULL }
  422. // mark yaml_mark_t = { 0, 0, 0 }
  423. //
  424. // assert(document) // Non-NULL document object is expected.
  425. // assert((tag_directives_start && tag_directives_end) ||
  426. // (tag_directives_start == tag_directives_end))
  427. // // Valid tag directives are expected.
  428. //
  429. // if (!STACK_INIT(&context, nodes, INITIAL_STACK_SIZE)) goto error
  430. //
  431. // if (version_directive) {
  432. // version_directive_copy = yaml_malloc(sizeof(yaml_version_directive_t))
  433. // if (!version_directive_copy) goto error
  434. // version_directive_copy.major = version_directive.major
  435. // version_directive_copy.minor = version_directive.minor
  436. // }
  437. //
  438. // if (tag_directives_start != tag_directives_end) {
  439. // tag_directive *yaml_tag_directive_t
  440. // if (!STACK_INIT(&context, tag_directives_copy, INITIAL_STACK_SIZE))
  441. // goto error
  442. // for (tag_directive = tag_directives_start
  443. // tag_directive != tag_directives_end; tag_directive ++) {
  444. // assert(tag_directive.handle)
  445. // assert(tag_directive.prefix)
  446. // if (!yaml_check_utf8(tag_directive.handle,
  447. // strlen((char *)tag_directive.handle)))
  448. // goto error
  449. // if (!yaml_check_utf8(tag_directive.prefix,
  450. // strlen((char *)tag_directive.prefix)))
  451. // goto error
  452. // value.handle = yaml_strdup(tag_directive.handle)
  453. // value.prefix = yaml_strdup(tag_directive.prefix)
  454. // if (!value.handle || !value.prefix) goto error
  455. // if (!PUSH(&context, tag_directives_copy, value))
  456. // goto error
  457. // value.handle = NULL
  458. // value.prefix = NULL
  459. // }
  460. // }
  461. //
  462. // DOCUMENT_INIT(*document, nodes.start, nodes.end, version_directive_copy,
  463. // tag_directives_copy.start, tag_directives_copy.top,
  464. // start_implicit, end_implicit, mark, mark)
  465. //
  466. // return 1
  467. //
  468. //error:
  469. // STACK_DEL(&context, nodes)
  470. // yaml_free(version_directive_copy)
  471. // while (!STACK_EMPTY(&context, tag_directives_copy)) {
  472. // value yaml_tag_directive_t = POP(&context, tag_directives_copy)
  473. // yaml_free(value.handle)
  474. // yaml_free(value.prefix)
  475. // }
  476. // STACK_DEL(&context, tag_directives_copy)
  477. // yaml_free(value.handle)
  478. // yaml_free(value.prefix)
  479. //
  480. // return 0
  481. //}
  482. //
  483. ///*
  484. // * Destroy a document object.
  485. // */
  486. //
  487. //YAML_DECLARE(void)
  488. //yaml_document_delete(document *yaml_document_t)
  489. //{
  490. // struct {
  491. // error yaml_error_type_t
  492. // } context
  493. // tag_directive *yaml_tag_directive_t
  494. //
  495. // context.error = YAML_NO_ERROR // Eliminate a compliler warning.
  496. //
  497. // assert(document) // Non-NULL document object is expected.
  498. //
  499. // while (!STACK_EMPTY(&context, document.nodes)) {
  500. // node yaml_node_t = POP(&context, document.nodes)
  501. // yaml_free(node.tag)
  502. // switch (node.type) {
  503. // case YAML_SCALAR_NODE:
  504. // yaml_free(node.data.scalar.value)
  505. // break
  506. // case YAML_SEQUENCE_NODE:
  507. // STACK_DEL(&context, node.data.sequence.items)
  508. // break
  509. // case YAML_MAPPING_NODE:
  510. // STACK_DEL(&context, node.data.mapping.pairs)
  511. // break
  512. // default:
  513. // assert(0) // Should not happen.
  514. // }
  515. // }
  516. // STACK_DEL(&context, document.nodes)
  517. //
  518. // yaml_free(document.version_directive)
  519. // for (tag_directive = document.tag_directives.start
  520. // tag_directive != document.tag_directives.end
  521. // tag_directive++) {
  522. // yaml_free(tag_directive.handle)
  523. // yaml_free(tag_directive.prefix)
  524. // }
  525. // yaml_free(document.tag_directives.start)
  526. //
  527. // memset(document, 0, sizeof(yaml_document_t))
  528. //}
  529. //
  530. ///**
  531. // * Get a document node.
  532. // */
  533. //
  534. //YAML_DECLARE(yaml_node_t *)
  535. //yaml_document_get_node(document *yaml_document_t, index int)
  536. //{
  537. // assert(document) // Non-NULL document object is expected.
  538. //
  539. // if (index > 0 && document.nodes.start + index <= document.nodes.top) {
  540. // return document.nodes.start + index - 1
  541. // }
  542. // return NULL
  543. //}
  544. //
  545. ///**
  546. // * Get the root object.
  547. // */
  548. //
  549. //YAML_DECLARE(yaml_node_t *)
  550. //yaml_document_get_root_node(document *yaml_document_t)
  551. //{
  552. // assert(document) // Non-NULL document object is expected.
  553. //
  554. // if (document.nodes.top != document.nodes.start) {
  555. // return document.nodes.start
  556. // }
  557. // return NULL
  558. //}
  559. //
  560. ///*
  561. // * Add a scalar node to a document.
  562. // */
  563. //
  564. //YAML_DECLARE(int)
  565. //yaml_document_add_scalar(document *yaml_document_t,
  566. // tag *yaml_char_t, value *yaml_char_t, length int,
  567. // style yaml_scalar_style_t)
  568. //{
  569. // struct {
  570. // error yaml_error_type_t
  571. // } context
  572. // mark yaml_mark_t = { 0, 0, 0 }
  573. // tag_copy *yaml_char_t = NULL
  574. // value_copy *yaml_char_t = NULL
  575. // node yaml_node_t
  576. //
  577. // assert(document) // Non-NULL document object is expected.
  578. // assert(value) // Non-NULL value is expected.
  579. //
  580. // if (!tag) {
  581. // tag = (yaml_char_t *)YAML_DEFAULT_SCALAR_TAG
  582. // }
  583. //
  584. // if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error
  585. // tag_copy = yaml_strdup(tag)
  586. // if (!tag_copy) goto error
  587. //
  588. // if (length < 0) {
  589. // length = strlen((char *)value)
  590. // }
  591. //
  592. // if (!yaml_check_utf8(value, length)) goto error
  593. // value_copy = yaml_malloc(length+1)
  594. // if (!value_copy) goto error
  595. // memcpy(value_copy, value, length)
  596. // value_copy[length] = '\0'
  597. //
  598. // SCALAR_NODE_INIT(node, tag_copy, value_copy, length, style, mark, mark)
  599. // if (!PUSH(&context, document.nodes, node)) goto error
  600. //
  601. // return document.nodes.top - document.nodes.start
  602. //
  603. //error:
  604. // yaml_free(tag_copy)
  605. // yaml_free(value_copy)
  606. //
  607. // return 0
  608. //}
  609. //
  610. ///*
  611. // * Add a sequence node to a document.
  612. // */
  613. //
  614. //YAML_DECLARE(int)
  615. //yaml_document_add_sequence(document *yaml_document_t,
  616. // tag *yaml_char_t, style yaml_sequence_style_t)
  617. //{
  618. // struct {
  619. // error yaml_error_type_t
  620. // } context
  621. // mark yaml_mark_t = { 0, 0, 0 }
  622. // tag_copy *yaml_char_t = NULL
  623. // struct {
  624. // start *yaml_node_item_t
  625. // end *yaml_node_item_t
  626. // top *yaml_node_item_t
  627. // } items = { NULL, NULL, NULL }
  628. // node yaml_node_t
  629. //
  630. // assert(document) // Non-NULL document object is expected.
  631. //
  632. // if (!tag) {
  633. // tag = (yaml_char_t *)YAML_DEFAULT_SEQUENCE_TAG
  634. // }
  635. //
  636. // if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error
  637. // tag_copy = yaml_strdup(tag)
  638. // if (!tag_copy) goto error
  639. //
  640. // if (!STACK_INIT(&context, items, INITIAL_STACK_SIZE)) goto error
  641. //
  642. // SEQUENCE_NODE_INIT(node, tag_copy, items.start, items.end,
  643. // style, mark, mark)
  644. // if (!PUSH(&context, document.nodes, node)) goto error
  645. //
  646. // return document.nodes.top - document.nodes.start
  647. //
  648. //error:
  649. // STACK_DEL(&context, items)
  650. // yaml_free(tag_copy)
  651. //
  652. // return 0
  653. //}
  654. //
  655. ///*
  656. // * Add a mapping node to a document.
  657. // */
  658. //
  659. //YAML_DECLARE(int)
  660. //yaml_document_add_mapping(document *yaml_document_t,
  661. // tag *yaml_char_t, style yaml_mapping_style_t)
  662. //{
  663. // struct {
  664. // error yaml_error_type_t
  665. // } context
  666. // mark yaml_mark_t = { 0, 0, 0 }
  667. // tag_copy *yaml_char_t = NULL
  668. // struct {
  669. // start *yaml_node_pair_t
  670. // end *yaml_node_pair_t
  671. // top *yaml_node_pair_t
  672. // } pairs = { NULL, NULL, NULL }
  673. // node yaml_node_t
  674. //
  675. // assert(document) // Non-NULL document object is expected.
  676. //
  677. // if (!tag) {
  678. // tag = (yaml_char_t *)YAML_DEFAULT_MAPPING_TAG
  679. // }
  680. //
  681. // if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error
  682. // tag_copy = yaml_strdup(tag)
  683. // if (!tag_copy) goto error
  684. //
  685. // if (!STACK_INIT(&context, pairs, INITIAL_STACK_SIZE)) goto error
  686. //
  687. // MAPPING_NODE_INIT(node, tag_copy, pairs.start, pairs.end,
  688. // style, mark, mark)
  689. // if (!PUSH(&context, document.nodes, node)) goto error
  690. //
  691. // return document.nodes.top - document.nodes.start
  692. //
  693. //error:
  694. // STACK_DEL(&context, pairs)
  695. // yaml_free(tag_copy)
  696. //
  697. // return 0
  698. //}
  699. //
  700. ///*
  701. // * Append an item to a sequence node.
  702. // */
  703. //
  704. //YAML_DECLARE(int)
  705. //yaml_document_append_sequence_item(document *yaml_document_t,
  706. // sequence int, item int)
  707. //{
  708. // struct {
  709. // error yaml_error_type_t
  710. // } context
  711. //
  712. // assert(document) // Non-NULL document is required.
  713. // assert(sequence > 0
  714. // && document.nodes.start + sequence <= document.nodes.top)
  715. // // Valid sequence id is required.
  716. // assert(document.nodes.start[sequence-1].type == YAML_SEQUENCE_NODE)
  717. // // A sequence node is required.
  718. // assert(item > 0 && document.nodes.start + item <= document.nodes.top)
  719. // // Valid item id is required.
  720. //
  721. // if (!PUSH(&context,
  722. // document.nodes.start[sequence-1].data.sequence.items, item))
  723. // return 0
  724. //
  725. // return 1
  726. //}
  727. //
  728. ///*
  729. // * Append a pair of a key and a value to a mapping node.
  730. // */
  731. //
  732. //YAML_DECLARE(int)
  733. //yaml_document_append_mapping_pair(document *yaml_document_t,
  734. // mapping int, key int, value int)
  735. //{
  736. // struct {
  737. // error yaml_error_type_t
  738. // } context
  739. //
  740. // pair yaml_node_pair_t
  741. //
  742. // assert(document) // Non-NULL document is required.
  743. // assert(mapping > 0
  744. // && document.nodes.start + mapping <= document.nodes.top)
  745. // // Valid mapping id is required.
  746. // assert(document.nodes.start[mapping-1].type == YAML_MAPPING_NODE)
  747. // // A mapping node is required.
  748. // assert(key > 0 && document.nodes.start + key <= document.nodes.top)
  749. // // Valid key id is required.
  750. // assert(value > 0 && document.nodes.start + value <= document.nodes.top)
  751. // // Valid value id is required.
  752. //
  753. // pair.key = key
  754. // pair.value = value
  755. //
  756. // if (!PUSH(&context,
  757. // document.nodes.start[mapping-1].data.mapping.pairs, pair))
  758. // return 0
  759. //
  760. // return 1
  761. //}
  762. //
  763. //