(Simple String Format) is an syntax of format and a library for parse this.

Related tags

Miscellaneous ssfmt
Overview

SSFMT

(Simple String Format) is an syntax of format and a library for parse this.

SSFMT != {fmt}

SSFMT is NOT an API/library for parse {fmt} syntax !

Syntax

The syntax of SSFMT is very simple, its use braces { } for "declare" a format, and an identifier inside the braces block for identifie the format.

Example: {name} (is a format with "name" like indentifier)

You can escape braces with backslash \ for do not consider the followed braces like an format but like a braces.

Example: \{name\} (Here, "name" will not be interpreted like as format)

An identifier have a limit of 255 charaters. If an identifer length greater than this limit, only the first 255 chararacters are compared.

A identifier can be represented with any of ascii table characters, but its better to use only alphabet [a-z]+upper/lower, digits [0-9]+ and underscore _.

Download

git clone https://github.com/ownesis/ssfmt.git

Compile the library

cd ssfmt/

mkdir build && cd build/

cmake .. && make

Install library in system

as root

make install

This copies:

  • Header file ssfmt.h inside /usr/local/include.
  • Shared object library libssfmt.so inside /usr/local/lib.
  • Static object library libssfmt.a inside /usr/local/lib.

as root

ldconfig /usr/local/lib

This adding /usr/local/lib to dynamic library path.

Library Documentation

The dictionnary

To change a format with its value, you must specify a "dictionnary" with identifier/value correspondence.

The "dictionnary" is a structure with two char * fields (for the identifier and its value).

struct SSFMTDict {
    char *identifier;
    char *value;
};

The context

Now, you need to specify the "dictionnary" array in the context structure: ssfmt_ctx_t (struct SSFMT_ctx).

struct SSFMT_ctx {
	struct SSFMTDict *array;
	size_t array_len;
	_Bool case_sensitibe;
};

This context structure take 3 fields:

  • array: (struct SSFMTDict *), This pointer take the array address of SSFMTDict structure.
  • array_len: (size_t), This object take the array length.
  • case_sensitive: (_Bool), If set to 1 the identifer will become case sensitive; If set to 0 the identifer will become not case sensitive.

The library propose a macro function for init a context:

#define SSFMT_INIT(a, l, c) \
    { .array = (a), .array_len = (l), .case_sensitve = (c) }

Example of use:

ssfm_ctx_t ctx = SSFMT_INIT(the_dict_array, 5, 0);

The parser function

For parsing and get a string with the identifier replaced with its value, you must to call a function for do this.

char *ssfmt_parser(const ssfmt_ctx_t *ctx, const char *format, char *buf, size_t buf_len);

This function take 4 arguments:

  • ctx: (const ssfmt_ctx_t *), is an input argument, it's the address of the initializedstruct SSFMT_ctx.
  • format: (const char *), is an input argument, is a null terminated string with formats to replace.
  • buf: (char *buf), is an output argument, it takes a buffer which will store the result of the formatted character string format.
  • buf_len: (size_t): is an input argument, it's the size of the buffer buf.

The return value is the address of buf or NULL if an error occured.

Note

if a format is used with a unknown identifier (a identifer not in dictionnary array), it's just been ignored and will be removed of the result string.

If the length result formatted string is greater than buf_size, the buf will have the result of the formats that have been formatted and a finished with a null byte '\0'.

Example usage

code

#include <stdio.h>
#include <ssfmt.h>

#define ARRAY_SIZE 4
#define BUF_SIZE  1024 

#define FORMAT "{lastname} {firstname}:\n - Planet: {planet} ({dimension})"

int main(void) {
	char buf[BUF_SIZE] = {0};
	struct SSFMTDict array[ARRAY_SIZE] = {
		[0] = {
			.identifier = "firstname",
			.value = "Rick"	
		},

		[1] = {
			.identifier = "lastname",
			.value = "Sanchez"
		},

		[2] = {
			.identifier = "dimension",
			.value = "C-137"	
		},

		[3] = {
			.identifier = "planet",
			.value = "Earth"	
		}
	};

	ssfmt_ctx_t ctx = SSFMT_INIT(array, ARRAY_SIZE, 1);

	ssfmt_parser(&ctx, FORMAT, buf, BUF_SIZE);

	puts(buf);

	return 0;
}

compiling

gcc -o example example_code.c -I/usr/local/include/ -L/usr/local/lib -lssfmt

You might also like...
A run-time C++ library for working with units of measurement and conversions between them and with string representations of units and measurements

Units What's new Some of the CMake target names have changed in the latest release, please update builds appropriately Documentation A library that pr

Blog post on using a custom Bash builtin to parse INI config files

Writing a Bash Builtin in C to Parse INI Configs Why Not Just Parse INI Configs With Bash? Shell languages such as Bash excel at certain tasks, such a

A BOF to parse the imports of a provided PE-file, optionally extracting symbols on a per-dll basis.

PE Import Enumerator BOF What is this? This is a BOF to enumerate DLL files to-be-loaded by a given PE file. Depending on the number of arguments, thi

dump&parse luac5.3 binary chunk

Lua Lab1 LuacFormatter Author: Yintao, Xu Email: [email protected] In this lab, we aim at parsing lua5.3's dumped file(a.k.a lua byte code). Our goal

The OpenEXR project provides the specification and reference implementation of the EXR file format, the professional-grade image storage format of the motion picture industry.
The OpenEXR project provides the specification and reference implementation of the EXR file format, the professional-grade image storage format of the motion picture industry.

OpenEXR OpenEXR provides the specification and reference implementation of the EXR file format, the professional-grade image storage format of the mot

Source code from 068A, our 64k demo presented at Syntax 2017

068A source code dump This is a dump of the source code for the engine, graphics tool and player for 068A, our 64k demo released at Syntax 2017. It's

Keepy is a pseudo-language / HTML preprocessor that translates special syntax into HTML

Keepy is a pseudo-language / HTML preprocessor that translates special syntax into HTML with the peculiarity of being a "compiled pseudo-language". So after using Keepy you can edit the final output HTML file.

A single-header, new syntax for C & C++

sea A new syntax for C & C++, in one header file sea is a new syntax for C & C++. It can be used by adding the following line of code to a .c, or .cc/

Owner
null
header-only UTF-8 string functions based on STL-string

utf8_xxx header-only UTF-8 string functions based on STL-string size_t utf8_len(const std::string& _Str) std::string utf8_sub(const std::string& _Str,

Voidmatrix 2 Dec 27, 2021
A simple tool that aims to efficiently and quickly parse the outputs of web scraping tools like gau

massurl is a simple tool that aims to parse the outputs of tools like gau, and extract the parameters for each URL, remove duplicates and do it all very quickly. Because web scraping tools' outputs can get very large very quickly, it is nice to have a tool that parses them and and outputs something clean and easy to read.

Fr1nge 13 Jul 24, 2022
A simple processor emulator written in c++ that can parse and execute x32 code. x32 is binary code made by me for this processor.

A SIMPLE PROCESSOR EMULATOR AND CODE EXECUTOR The Repository This is a fairly new project and is still heavy in development. If you find and bugs feel

Luka Golob 4 Jan 20, 2022
A simple wrapper for 'pacman' with a syntax similar to 'apt' to help people transitioning to Arch and Arch based distributions like Manjaro.

aptpac aptpac is a program which helps with the transition to Arch Linux and Arch based distros like Manjaro. It simplifies using pacman as it works l

Itai Nelken 17 Sep 26, 2022
Arobase is a simple programming language with a c-like syntax.

Arobase Arobase is a simple programming language with a c-like syntax. Requirements gcc gas (gnu as) ld A 64 bits linux distribution (generated assemb

Nj├Ârd 17 Nov 28, 2022
A Simple Language (PHP-Like Syntax)

SL A Simple Language (PHP-Like Syntax) Usage: SL.exe <filename> A 64-bit windows executable is provided (SL.exe) alongside some demo SL code (main.sl)

null 3 Nov 15, 2021
A collection of code snippets and examples showing syntax and capabilities of VEX language inside SideFX Houdini

VEX tutorial A collection of code snippets and examples showing syntax and capabilities of VEX language inside SideFX Houdini by Juraj Tomori How to u

Juraj Tomori 618 Nov 23, 2022
Visual Studio extension for assembly syntax highlighting and code completion in assembly files and the disassembly window

Asm-Dude Assembly syntax highlighting and code assistance for assembly source files and the disassembly window for Visual Studio 2015, 2017 and 2019.

Henk-Jan Lebbink 4k Nov 16, 2022
BokutachiHook - Hook for Lunatic Rave 2 to parse score data and send it to an HTTP server, made specifically for Bokutachi IR.

BokutachiHook Hook for Lunatic Rave 2 to parse score data and send it to an HTTP server, made specifically for Bokutachi IR (https://bokutachi.xyz). T

null 7 Sep 12, 2022