Release candidate
This commit is contained in:
parent
77d3dcf372
commit
174aebaf39
52
Makefile
Normal file
52
Makefile
Normal file
@ -0,0 +1,52 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# ******************************************************************************
|
||||
#
|
||||
# @file Makefile
|
||||
#
|
||||
# @brief Makefile for YAML Loader library
|
||||
#
|
||||
# @copyright Copyright (C) 2024 Jackrabbit Founders LLC. All rights reserved.
|
||||
#
|
||||
# @date Mar 2024
|
||||
# @author Barrett Edwards <code@jrlabs.io>
|
||||
#
|
||||
# ******************************************************************************
|
||||
|
||||
CC=gcc
|
||||
CFLAGS= -g3 -O0 -Wall -Wextra
|
||||
MACROS=
|
||||
INCLUDE_DIR=/usr/local/include
|
||||
LIB_DIR=/usr/local/lib
|
||||
INCLUDE_PATH=-I $(INCLUDE_DIR) -I /usr/include/glib-2.0 -I /usr/lib/`uname -m`-linux-gnu/glib-2.0/include/
|
||||
LIB_PATH=-L $(LIB_DIR)
|
||||
LIBS=-l yaml -l glib-2.0
|
||||
TARGET=yamlloader
|
||||
|
||||
all: testbench lib$(TARGET).a
|
||||
|
||||
testbench: testbench.c main.o
|
||||
$(CC) $^ $(CFLAGS) $(MACROS) $(INCLUDE_PATH) $(LIB_PATH) $(LIBS) -o $@
|
||||
|
||||
lib$(TARGET).a: main.o
|
||||
ar rcs $@ $^
|
||||
|
||||
main.o: main.c main.h
|
||||
$(CC) -c $< $(CFLAGS) $(MACROS) $(INCLUDE_PATH) -o $@
|
||||
|
||||
clean:
|
||||
rm -rf ./*.o ./*.a testbench
|
||||
|
||||
doc:
|
||||
doxygen
|
||||
|
||||
install: lib$(TARGET).a
|
||||
sudo cp lib$(TARGET).a $(LIB_DIR)/
|
||||
sudo cp main.h $(INCLUDE_DIR)/$(TARGET).h
|
||||
|
||||
.PHONY: all clean doc install
|
||||
|
||||
# Variables
|
||||
# $^ Will expand to be all the sensitivity list
|
||||
# $< Will expand to be the frist file in sensitivity list
|
||||
# $@ Will expand to be the target name (the left side of the ":" )
|
||||
# -c gcc will compile but not try and link
|
||||
67
config.yaml
Normal file
67
config.yaml
Normal file
@ -0,0 +1,67 @@
|
||||
version: 1
|
||||
vid: 1000
|
||||
sid: 0001
|
||||
svid: 2000
|
||||
ssid: 0002
|
||||
sn: 12345678
|
||||
ingress_port: 1
|
||||
total_ports: 3
|
||||
max_vcss: 2
|
||||
max_vppbs: 3
|
||||
hdm_decoders: 1
|
||||
ports:
|
||||
0:
|
||||
state: 4
|
||||
dv: 1
|
||||
dt: 1
|
||||
cv: 1
|
||||
max_link_width: 16
|
||||
neg_link_width: 16
|
||||
speeds: 0
|
||||
max_link_speed: 5
|
||||
cur_link_speed: 4
|
||||
ltssm: 4
|
||||
first_lane: 0
|
||||
lf: 0
|
||||
ld: 0
|
||||
1:
|
||||
state: 3
|
||||
dv: 1
|
||||
dt: 4
|
||||
cv: 3
|
||||
max_link_width: 16
|
||||
neg_link_width: 16
|
||||
speeds: 0
|
||||
max_link_speed: 5
|
||||
cur_link_speed: 5
|
||||
ltssm: 4
|
||||
first_lane: 0
|
||||
lf: 0
|
||||
ld: 16
|
||||
vcss:
|
||||
0:
|
||||
state: 1
|
||||
uspid: 0
|
||||
num_vppb: 2
|
||||
vppbs:
|
||||
0:
|
||||
bind_status: 2
|
||||
portid: 0
|
||||
ldid: 0
|
||||
1:
|
||||
bind_status: 2
|
||||
portid: 1
|
||||
ldid: 0
|
||||
1:
|
||||
state: 1
|
||||
uspid: 3
|
||||
num_vppb: 2
|
||||
vppbs:
|
||||
0:
|
||||
bind_status: 2
|
||||
portid: 3
|
||||
ldid: 0
|
||||
1:
|
||||
bind_status: 2
|
||||
portid: 4
|
||||
ldid: 0
|
||||
261
main.c
Normal file
261
main.c
Normal file
@ -0,0 +1,261 @@
|
||||
/* SPDX-License-Identifier: Apache-2.0 */
|
||||
/**
|
||||
* @file yamlloader.c
|
||||
*
|
||||
* @brief Code file to Load a .yaml file into a GHashTable
|
||||
*
|
||||
* @copyright Copyright (C) 2024 Jackrabbit Founders LLC. All rights reserved.
|
||||
*
|
||||
* @date Feb 2024
|
||||
* @author Barrett Edwards <code@jrlabs.io>
|
||||
*
|
||||
*/
|
||||
|
||||
/* INCLUDES ==================================================================*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <glib-2.0/glib.h>
|
||||
#include <yaml.h> // apt install libyaml-dev libyaml-doc
|
||||
|
||||
#include "yamlloader.h"
|
||||
|
||||
/* MACROS ====================================================================*/
|
||||
|
||||
#define MAX_STR 256
|
||||
|
||||
/* ENUMERATIONS ==============================================================*/
|
||||
|
||||
/* STRUCTS ===================================================================*/
|
||||
|
||||
struct yaml_parse_state {
|
||||
yaml_parser_t *parser;
|
||||
GHashTable *ht;
|
||||
|
||||
int escape;
|
||||
char *keyname;
|
||||
int loop;
|
||||
};
|
||||
|
||||
/* GLOBAL VARIABLES ==========================================================*/
|
||||
|
||||
/* PROTOTYPES ================================================================*/
|
||||
|
||||
int yl_parse(struct yaml_parse_state *y);
|
||||
|
||||
/* FUNCTIONS =================================================================*/
|
||||
|
||||
/**
|
||||
* Free allocated memory
|
||||
*
|
||||
* @param ht GHashTable* to walk and free memory from
|
||||
*/
|
||||
int yl_free(GHashTable *ht)
|
||||
{
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a .yaml file into a GHashTable
|
||||
*
|
||||
* @param filename char*
|
||||
* @return GHashTable*
|
||||
*/
|
||||
GHashTable *yl_load(char *filename)
|
||||
{
|
||||
int rv;
|
||||
FILE *fp;
|
||||
GHashTable *ht;
|
||||
yaml_parser_t parser;
|
||||
struct yaml_parse_state yps;
|
||||
|
||||
/* STEPS:
|
||||
* 1: Validate inputs
|
||||
* 2: Create GHashTable to store the result
|
||||
* 3: Initialize parser
|
||||
* 4: Open config file
|
||||
* 5: Set YAML parser input file
|
||||
* 6: Parse config File
|
||||
* 7: Cleanup
|
||||
*/
|
||||
|
||||
// STEP 1: Validate inputs
|
||||
if(filename == NULL) {
|
||||
goto end_ht;
|
||||
}
|
||||
|
||||
// STEP 2: Create strdict to store the result
|
||||
ht = g_hash_table_new(g_str_hash, g_str_equal);
|
||||
if (ht == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// STEP 3: Initialize parser
|
||||
rv = yaml_parser_initialize(&parser);
|
||||
if(rv == 0) {
|
||||
goto end_ht;
|
||||
}
|
||||
|
||||
// STEP 4: Open config file
|
||||
fp = fopen(filename, "r");
|
||||
if(fp == 0) {
|
||||
goto end_ht;
|
||||
}
|
||||
|
||||
// STEP 5: Set YAML parser input file
|
||||
yaml_parser_set_input_file(&parser, fp);
|
||||
|
||||
// STEP 6: Parse config File
|
||||
memset(&yps, 0, sizeof(struct yaml_parse_state));
|
||||
yps.ht = ht;
|
||||
yps.parser = &parser;
|
||||
yps.escape = YAML_STREAM_END_TOKEN;
|
||||
|
||||
rv = yl_parse(&yps);
|
||||
if(rv != 0) {
|
||||
goto end_parser;
|
||||
}
|
||||
|
||||
// STEP 7: Cleanup
|
||||
yaml_parser_delete(&parser);
|
||||
fclose(fp);
|
||||
|
||||
return ht;
|
||||
|
||||
end_parser:
|
||||
yaml_parser_delete(&parser);
|
||||
fclose(fp);
|
||||
end_ht:
|
||||
yl_free(ht);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse function called for each entry in the yaml file
|
||||
*/
|
||||
int yl_parse(struct yaml_parse_state *yps)
|
||||
{
|
||||
int rv;
|
||||
char *s;
|
||||
yl_obj_t *ylo;
|
||||
yaml_token_t t;
|
||||
GHashTable *newht;
|
||||
struct yaml_parse_state newyps;
|
||||
|
||||
do {
|
||||
yaml_parser_scan(yps->parser, &t);
|
||||
switch(t.type)
|
||||
{
|
||||
case YAML_STREAM_START_TOKEN: break;
|
||||
case YAML_STREAM_END_TOKEN: break;
|
||||
case YAML_KEY_TOKEN: yps->loop = 1; break;
|
||||
case YAML_VALUE_TOKEN: yps->loop = 2; break;
|
||||
case YAML_BLOCK_SEQUENCE_START_TOKEN: break;
|
||||
case YAML_BLOCK_ENTRY_TOKEN: break;
|
||||
case YAML_BLOCK_END_TOKEN: break;
|
||||
case YAML_BLOCK_MAPPING_START_TOKEN:
|
||||
if(yps->loop != 2) // Ignore the very first blk_map_start in the stream
|
||||
break;
|
||||
|
||||
// Create a new GHashTable object and add to the current hashtable
|
||||
ylo = calloc (1, sizeof( yl_obj_t ) );
|
||||
ylo->ht = g_hash_table_new(g_str_hash, g_str_equal);
|
||||
rv = g_hash_table_insert(yps->ht, yps->keyname, ylo);
|
||||
if (rv == FALSE) { // return of 0 is an error
|
||||
goto end;
|
||||
}
|
||||
|
||||
// Initialize a new yaml+_parse_state
|
||||
memset(&newyps, 0, sizeof(struct yaml_parse_state));
|
||||
newyps.ht = ylo->ht;
|
||||
newyps.parser = yps->parser;
|
||||
newyps.escape = YAML_BLOCK_END_TOKEN;
|
||||
|
||||
// Make recursive parse call with new parse state
|
||||
rv = yl_parse(&newyps);
|
||||
if (rv != 0) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
// After returning from the recursivecall to parse, clean up
|
||||
yps->loop = 0;
|
||||
yps->keyname = NULL;
|
||||
break;
|
||||
case YAML_SCALAR_TOKEN:
|
||||
// Duplicate the string
|
||||
s = strndup(t.data.scalar.value, MAX_STR);
|
||||
|
||||
// store duplicated string value yaml_parse_state as the keyname until we use it later
|
||||
if (yps->loop == 1) {
|
||||
yps->keyname = s;
|
||||
}
|
||||
else if (yps->loop == 2) {
|
||||
// Store the duplicated value string in a yl_obj struct and then store that yl_obj in the hash table
|
||||
ylo = calloc (1, sizeof( yl_obj_t ) );
|
||||
ylo->str = s;
|
||||
|
||||
// We now have the value, store the KV pair in hash table
|
||||
rv = g_hash_table_insert(yps->ht, yps->keyname, ylo);
|
||||
if (rv ==0) { // 0
|
||||
goto end;
|
||||
}
|
||||
yps->keyname = NULL;
|
||||
}
|
||||
yps->loop = 0;
|
||||
break;
|
||||
default: yps->loop = 0; break;
|
||||
}
|
||||
if(t.type != yps->escape)
|
||||
yaml_token_delete(&t);
|
||||
} while(t.type != yps->escape);
|
||||
yaml_token_delete(&t);
|
||||
return 0;
|
||||
|
||||
end:
|
||||
yaml_token_delete(&t);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Print the GHasHTable
|
||||
*/
|
||||
int yl_print(GHashTable *ht)
|
||||
{
|
||||
|
||||
if (ht == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
g_hash_table_foreach(ht, _yl_print_entry, 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Print an individual entry in the GHashTable
|
||||
*/
|
||||
GHFunc _yl_print_entry(gpointer key, gpointer value, gpointer user_data)
|
||||
{
|
||||
int i, indent;
|
||||
yl_obj_t *ylo;
|
||||
|
||||
indent = (int) user_data;
|
||||
ylo = (yl_obj_t*) value;
|
||||
|
||||
// Print the indent spaces
|
||||
for ( i = 0 ; i < indent ; i++)
|
||||
printf(" ");
|
||||
|
||||
if (ylo->str != NULL) {
|
||||
printf("%s:%s\n", (char*) key, ylo->str);
|
||||
}
|
||||
else {
|
||||
printf("%s:\n", (char*) key);
|
||||
g_hash_table_foreach(ylo->ht, _yl_print_entry, indent+2);
|
||||
}
|
||||
}
|
||||
|
||||
44
main.h
Normal file
44
main.h
Normal file
@ -0,0 +1,44 @@
|
||||
/* SPDX-License-Identifier: Apache-2.0 */
|
||||
/**
|
||||
* @file yamlloader.c
|
||||
*
|
||||
* @brief Code file to Load a .yaml file into a GHashTable
|
||||
*
|
||||
* @copyright Copyright (C) 2024 Jackrabbit Founders LLC. All rights reserved.
|
||||
*
|
||||
* @date Feb 2024
|
||||
* @author Barrett Edwards <code@jrlabs.io>
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef _YAMLLOADER_H
|
||||
#define _YAMLLOADER_H
|
||||
|
||||
/* INCLUDES ==================================================================*/
|
||||
|
||||
#include <linux/types.h>
|
||||
|
||||
#include <glib-2.0/glib.h>
|
||||
|
||||
/* MACROS ====================================================================*/
|
||||
|
||||
/* ENUMERATIONS ==============================================================*/
|
||||
|
||||
/* STRUCTS ===================================================================*/
|
||||
|
||||
typedef struct yl_obj
|
||||
{
|
||||
char *str;
|
||||
GHashTable *ht;
|
||||
} yl_obj_t;
|
||||
|
||||
/* GLOBAL VARIABLES ==========================================================*/
|
||||
|
||||
/* PROTOTYPES ================================================================*/
|
||||
|
||||
GHashTable *yl_load(char *filename);
|
||||
int yl_free(GHashTable *ht);
|
||||
int yl_print(GHashTable *ht);
|
||||
GHFunc _yl_print_entry(gpointer key, gpointer value, gpointer user_data);
|
||||
|
||||
#endif //ifndef _YAMLLOADER_H
|
||||
52
testbench.c
Normal file
52
testbench.c
Normal file
@ -0,0 +1,52 @@
|
||||
/* SPDX-License-Identifier: Apache-2.0 */
|
||||
/**
|
||||
* @file testbench.c
|
||||
*
|
||||
* @brief Testbench for yaml loader library
|
||||
*
|
||||
* @copyright Copyright (C) 2024 Jackrabbit Founders LLC. All rights reserved.
|
||||
*
|
||||
* @date Feb 2024
|
||||
* @author Barrett Edwards <code@jrlabs.io>
|
||||
*
|
||||
*/
|
||||
|
||||
/* INCLUDES ==================================================================*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <glib-2.0/glib.h>
|
||||
|
||||
#include "yamlloader.h"
|
||||
|
||||
/* MACROS ====================================================================*/
|
||||
|
||||
/* ENUMERATIONS ==============================================================*/
|
||||
|
||||
/* STRUCTS ===================================================================*/
|
||||
|
||||
/* GLOBAL VARIABLES ==========================================================*/
|
||||
|
||||
/* PROTOTYPES ================================================================*/
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
GHashTable *ht;
|
||||
|
||||
if (argc < 2 )
|
||||
{
|
||||
printf("Usage: testbench <filename.yaml>\n");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
ht = yl_load(argv[1]);
|
||||
|
||||
yl_print(ht);
|
||||
|
||||
yl_free(ht);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user