api_c.go 23 KB

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