summaryrefslogtreecommitdiff
path: root/src/lib/yaml_util.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/yaml_util.c')
-rw-r--r--src/lib/yaml_util.c127
1 files changed, 127 insertions, 0 deletions
diff --git a/src/lib/yaml_util.c b/src/lib/yaml_util.c
new file mode 100644
index 0000000..06df9a9
--- /dev/null
+++ b/src/lib/yaml_util.c
@@ -0,0 +1,127 @@
+/**
+ * @file yaml_util.c
+ * @brief Utility for yaml data, it extracts value from yaml
+
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ * This software is the confidential and proprietary information
+ * of Samsung Electronics, Inc. ("Confidential Information"). You
+ * shall not disclose such Confidential Information and shall use
+ * it only in accordance with the terms of the license agreement
+ * you entered into with Samsung.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "yaml_util.h"
+
+#define DEBUG
+
+int yaml_getContents(char *file_name, char *contentsKey, char **retVal)
+{
+ yaml_parser_t parser;
+ yaml_token_t token;
+ FILE *file;
+ int stateYAML = YAML_NO_TOKEN;
+ char *scalarTokenVal = NULL;
+ int scalarTokenLen = 0;
+ char keyTokenVal[256];
+ char retTmp[256];
+ int retTmpLen;
+ int blockSequenceCnt = 0;
+
+ file = fopen(file_name, "r");
+ if (file == NULL) {
+ DEBUG("Failed to open file");
+ return -1;
+ }
+ /* Initialize parser */
+ if (!yaml_parser_initialize(&parser)) {
+ DEBUG("Failed to initialize parser!!!");
+ fclose(file);
+ return -2;
+ }
+ /* Set input file */
+ yaml_parser_set_input_file(&parser, file);
+
+ /* BEGIN new code */
+ do {
+ yaml_parser_scan(&parser, &token);
+ switch (token.type) {
+
+ case YAML_KEY_TOKEN:
+ DEBUG("[YAML_KEY_TOKEN]");
+ stateYAML = YAML_KEY_TOKEN;
+ break;
+ case YAML_VALUE_TOKEN:
+ DEBUG("[YAML_VALUE_TOKEN]");
+ stateYAML = YAML_VALUE_TOKEN;
+ break;
+ case YAML_BLOCK_SEQUENCE_START_TOKEN:
+ DEBUG("[YAML_BLOCK_SEQUENCE_START_TOKEN]");
+ blockSequenceCnt = 0;
+ memset(retTmp, 0x00, 256);
+ retTmpLen = 0;
+ break;
+ case YAML_BLOCK_ENTRY_TOKEN:
+ DEBUG("[YAML_BLOCK_ENTRY_TOKEN]");
+ stateYAML = YAML_BLOCK_ENTRY_TOKEN;
+ blockSequenceCnt++;
+ break;
+ case YAML_BLOCK_END_TOKEN:
+ DEBUG("[YAML_BLOCK_END_TOKEN]");
+ if (!strncmp(keyTokenVal, contentsKey, strlen(contentsKey))) {
+ if (stateYAML == YAML_BLOCK_ENTRY_TOKEN) {
+ *retVal = (char *)malloc(retTmpLen + 1);
+ memset(*retVal, 0x00, retTmpLen + 1);
+ strncpy(*retVal, retTmp, retTmpLen);
+ stateYAML = YAML_BLOCK_END_TOKEN;
+ }
+ }
+ break;
+ case YAML_SCALAR_TOKEN:
+ DEBUG("[YAML_SCALAR_TOKEN]: %s", token.data.scalar.value);
+ scalarTokenVal = token.data.scalar.value;
+ scalarTokenLen = token.data.scalar.length;
+
+ if (stateYAML == YAML_KEY_TOKEN) {
+ memset(keyTokenVal, 0x00, 256);
+ strncpy(keyTokenVal, scalarTokenVal, scalarTokenLen);
+ stateYAML = 0;
+ } else if (stateYAML == YAML_VALUE_TOKEN) {
+ DEBUG("keyTokenVal: %s", keyTokenVal);
+ DEBUG("contentsKey: %s", contentsKey);
+ if (!strncmp(keyTokenVal, contentsKey, strlen(contentsKey))) {
+ *retVal = (char *)malloc(scalarTokenLen + 1);
+ memset(*retVal, 0x00, scalarTokenLen + 1);
+ strncpy(*retVal, scalarTokenVal, scalarTokenLen);
+ DEBUG("scalarTokenVal: %s", scalarTokenVal);
+ DEBUG("retVal: %s", *retVal);
+ }
+ stateYAML = 0;
+ } else if (stateYAML == YAML_BLOCK_ENTRY_TOKEN) {
+ if (blockSequenceCnt > 1) { /* from second block */
+ strncat(retTmp, ",", 1);
+ retTmpLen += 1;
+ }
+ strncat(retTmp, scalarTokenVal, scalarTokenLen);
+ retTmpLen += scalarTokenLen + 1;
+ DEBUG("retTmp(%d): %s", retTmpLen, retTmp);
+ } else {
+ DEBUG("Unrecognised!!!");
+ }
+ break;
+ default:
+ break;
+ }
+
+ if (token.type != YAML_STREAM_END_TOKEN)
+ yaml_token_delete(&token);
+
+ } while (token.type != YAML_STREAM_END_TOKEN);
+
+ yaml_token_delete(&token);
+ yaml_parser_delete(&parser);
+ fclose(file);
+ return 0;
+}