Armide Documentation. Release Kyle Mayes

Similar documents
Jansson Documentation

libcbor Documentation

PRINCIPLES OF OPERATING SYSTEMS

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

Common Misunderstandings from Exam 1 Material

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

Approximately a Test II CPSC 206

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee.

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

String constants. /* Demo: string constant */ #include <stdio.h> int main() {

CSCI 171 Chapter Outlines

CS2141 Software Development using C/C++ C++ Basics

Motivation was to facilitate development of systems software, especially OS development.

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Motivation was to facilitate development of systems software, especially OS development.

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Chapter-8 DATA TYPES. Introduction. Variable:

This is CS50. Harvard University Fall Quiz 0 Answer Key

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

ECE264 Spring 2013 Final Exam, April 30, 2013

Contents. Chapter 1 Overview of the JavaScript C Engine...1. Chapter 2 JavaScript API Reference...23

MUST. MPI Runtime Error Detection Tool

Call DLL from Limnor Applications

Programming Fundamentals - A Modular Structured Approach using C++ By: Kenneth Leroy Busbee

Linked data structures. EECS 211 Winter 2019

Project 2: Shell with History1

FLICONV-API. Generated by Doxygen

CS201 Some Important Definitions

ECE264 Fall 2013 Exam 3, November 20, 2013

ECE 244 Programming Fundamentals Fall Lab Assignment #5: Binary Search Trees

Memory Corruption 101 From Primitives to Exploit

CSE 565 Computer Security Fall 2018

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

by Pearson Education, Inc. All Rights Reserved.

ECE 2400 Computer Systems Programming, Fall 2018 PA2: List and Vector Data Structures

CS61, Fall 2012 Section 2 Notes

Compiling and Running a C Program in Unix

MUST. MPI Runtime Error Detection Tool

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

Final Exam. 11 May 2018, 120 minutes, 26 questions, 100 points

This document contains the questions and solutions to the CS107 midterm given in Winter 2018 by instructor Chris Gregg. This was a 120-minute exam.

Data Structures in C. C Programming and Software Tools. N.C. State Department of Computer Science

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Model Viva Questions for Programming in C lab

MDF4 Lib. Product Information

Contents. A Review of C language. Visual C Visual C++ 6.0

ECE551 Midterm Version 1

COMP 2355 Introduction to Systems Programming

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

FDK API Manual. June ITS Company

C++ Programming Chapter 7 Pointers

CSE 333 Midterm Exam Sample Solution 5/10/13

JTSK Programming in C II C-Lab II. Lecture 3 & 4

dmrlib Documentation Release Wijnand Modderman-Lenstra

c-lambda: C FFI via raco ctool

Types, Variables, and Constants

Exercise Session 2 Systems Programming and Computer Architecture

Understanding Pointers

CS 220: Introduction to Parallel Computing. Input/Output. Lecture 7

C mini reference. 5 Binary numbers 12

BİL200 TUTORIAL-EXERCISES Objective:

A Quick Introduction to C Programming

Advanced Pointer & Data Storage

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Section 3: File I/O, JSON, Generics. Meghan Cowan

Fundamental of Programming (C)

CPSC 3740 Programming Languages University of Lethbridge. Data Types

CS349/SE382 A1 C Programming Tutorial

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Call The Project Dynamic-Memory

LESSON 4. The DATA TYPE char

ICU 58 SpoofChecker API Changes

CSE 220: Systems Programming

C: Arrays, and strings. Department of Computer Science College of Engineering Boise State University. September 11, /16

PES INSTITUTE OF TECHNOLOGY (BSC) I MCA, First IA Test, November 2015 Programming Using C (13MCA11) Solution Set Faculty: Jeny Jijo

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

CSE 333 Midterm Exam 5/10/13

Fundamentals of Programming

ECE 551D Spring 2018 Midterm Exam

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Lecture 03 Bits, Bytes and Data Types

Dynamic memory allocation

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Chapter 5. Section 5.4 The Common String Library Functions. CS 50 Hathairat Rattanasook

by Pearson Education, Inc. All Rights Reserved. 2

Memory Management. a C view. Dr Alun Moon KF5010. Computer Science. Dr Alun Moon (Computer Science) Memory Management KF / 24

A skeleton file has been provided for you to use in both checkpoints.

Programming the DMCC in C

ECE551 Midterm Version 2

TI2725-C, C programming lab, course

An overview of Java, Data types and variables

CS Programming In C

Dynamic Memory Allocation and Command-line Arguments

malloc(), calloc(), realloc(), and free()

Computers Programming Course 5. Iulian Năstac

Transcription:

Armide Documentation Release 0.3.1 Kyle Mayes December 19, 2014

Contents 1 Introduction 1 1.1 Features.................................................. 1 1.2 License.................................................. 1 2 Table of Contents 3 2.1 Getting Started.............................................. 3 2.2 Tutorial.................................................. 4 2.3 API Reference.............................................. 7 3 Indices and tables 17 i

ii

CHAPTER 1 Introduction Armide is a C99 library for parsing, manipulating, and writing TOML data. It also has the capability to write JSON data. 1.1 Features No dependencies on other libraries Full support for UTF-8 Well documented Well tested 1.2 License Armide is licensed under the MIT license. 1

2 Chapter 1. Introduction

CHAPTER 2 Table of Contents 2.1 Getting Started 2.1.1 Downloading the Source The source for Armide is available from GitHub. $ git clone git@github.com:kylemayes/armide.git 2.1.2 Compiling and Installing Armide currently can only be built with CMake. $ mkdir Build && cd Build $ cmake -DCMAKE_BUILD_TYPE=Release.. && make $ make install Testing To compile the tests, pass the -DARMIDE_BUILD_TESTS=ON argument to CMake. $ cmake -DCMAKE_BUILD_TYPE=Release -DARMIDE_BUILD_TESTS=ON.. && make $ make test # run the tests $ make valgrind-test # run the tests under valgrind Testing with toml-test toml-test is a language agnostic program that tests TOML parsers. If you have Go installed, you can run toml-test on Armide after having built the tests as described above. $ export GOPATH=$HOME/go $ go get github.com/burntsushi/toml-test # install toml-test $ $GOPATH/bin/toml-test./Tests/Interface # requires that you are in the build directory 3

2.1.3 Compiling Programs that Use Armide The inclusion of #include <Armide.h> includes everything you need to use Armide. Armide has no external dependencies, so you only need to link your program against libarmide. $ cc -o Test Test.c -larmide 2.2 Tutorial In this tutorial, we will be parsing and printing out the values of a TOML document and writing an altered version of the document. [project] name = "Armide" start = 2014-12-03T10:48:57Z languages = [ "C", "C++" ] The full source is available as Example.c in the source code distribution under the Example directory. It can be compiled with cc -o Example Example.c -lm -larmide. 2.2.1 Parsing First, we include Armide.h and other necessary headers. #include <Armide.h> #include <stdio.h> Then, we read and parse Example.toml with toml_load_file(). int main(int argc, char* argv[]) { toml_error_t error; toml_t* toml = toml_load_file("example.toml", &error); if (toml == NULL) { printf("%s\n", error.message); } return -1; If the toml_load_file() function fails, it returns NULL and error is filled with the index, line number, column number, and type of the error as well as a descriptive error message. If parsing is successful, the returned toml_t will always be a table representing the root of the TOML document. 2.2.2 Manipulating We can then search for the table project with toml_table_get(). 4 Chapter 2. Table of Contents

toml_t* project = toml_table_get(toml, "project"); if (!toml_is_table(project)) { toml_decref(toml); } return -1; If there isn t a table member under the name project, toml_table_get() will return NULL. If its argument is NULL or not a table, toml_is_table() returns false. If toml_is_table() returns false, we need to free toml and return. However, toml_t* points to a reference counted type and thus should not be destroyed directly. Instead, we call toml_decref(), which will decrement the reference count (atomically) and destroy the value if the reference count reaches 0. Now that we have the project table, we can search for the members we are interested in. Here, we look for the name member and print it out if it exists and is a string. toml_t* name = toml_table_get(project, "name"); if (toml_is_string(name)) { printf("name = \"%s\"\n", toml_string_get(name)); } Here, we look for the start member and print it out if it exists and is a datetime. toml_t* start = toml_table_get(project, "start"); if (toml_is_datetime(start)) { char buffer[toml_datetime_size]; toml_datetime_print(start, buffer, sizeof(buffer)); } printf("datetime = %s\n", buffer); Datetimes in Armide are represented with the toml_utc_t datatype. The toml_datetime_print() function prints out a datetime to a buffer. Lastly, we find the languages member. If it is an array as we expect, we print out the size of the array and then use the toml_array_foreach() macro to iterate through each member and its index and print both. toml_t* languages = toml_table_get(project, "languages"); if (toml_is_array(languages)) { printf("languages.size = %zu\n", toml_array_get_size(languages)); size_t index = 0; toml_t* value = NULL; toml_array_foreach(languages, index, value) { if (toml_is_string(value)) { printf(" languages[%zu] = %s\n", index, toml_string_get(value)); 2.2. Tutorial 5

} } } Now, let s suppose that we want to add a value to this document and print it out. We could use the toml_table_set() function to add a new member. Warning: This code example is incorrect and is used for explanatory purposes. toml_table_set(project, "author", toml_create_string("kyle Mayes")); However, this would introduce a memory leak. toml_create_string(), like all toml_create_* functions, creates a new TOML value with a reference count of 1. toml_table_set() increments the reference count of its TOML value argument before placing it in the table or array. This means that the string created above will never be destroyed because it will have a reference count of 2 but toml_decref() will be called on it only once when its parent is destroyed. The solution to passing newly created values to functions like toml_table_set() is to use the variant ending with _with. These functions do not increment the reference count but instead steal the value. toml_table_set_with(project, "author", toml_create_string("kyle Mayes")); This ensures that the newly created string will always be destroyed when its parent is destroyed. 2.2.3 Writing Now that we have added a value to our TOML document, let s print it out. char* out = toml_dump(toml, 0); printf("%s\n", out); free(out); The toml_dump() function returns a string representation of any TOML value, but tables are the only TOML values that make valid TOML documents. There is also toml_dump_json() which, as its name suggests, fills a similar role but generates JSON instead of TOML. The second argument to these functions are the flags with which you can customize output. Information on these can be found in the API reference. 2.2.4 Cleaning Up Before we are done we need to ensure that toml is destroyed. toml_decref(toml); } return 0; The other TOML values we used do not need their reference counts decremented because they were only borrowed from their parent value and will be destroyed when their parent is destroyed. 6 Chapter 2. Table of Contents

If you want a toml_t* to outlive its parent, either use toml_incref() to increment its reference count or use toml_copy() or toml_deep_copy() to create a copy. Just remember to call toml_decref() when you are done! 2.3 API Reference 2.3.1 Preface All external functions, types, and values are prefixed with toml_ or TOML_. Unless noted otherwise, a function returns either -1 or NULL on error depending on the return type of that function. 2.3.2 Value Representation toml_t This type is used to represent all TOML values. It always contains the type of the value and its reference count. The remaining contents are determined by the type of the value. This type should always be accessed through a pointer. It is a reference counted type and should not be destroyed directly. There are functions for querying its type, incrementing and decrementing its reference count, making copies, and accessing its type-specific contents. Type enum toml_type The type of a TOML value. TOML_ARRAY TOML_FLOAT TOML_TABLE TOML_STRING TOML_BOOLEAN TOML_INTEGER TOML_DATETIME TOML_NONE A value outside of the range of enum toml_type that represents an error or special value. enum toml_type toml_get_type(const toml_t* toml) Returns the type of the TOML value. toml must not be NULL. bool toml_is(const toml_t* toml, enum toml_type type) Returns true if toml is non-null and has the given type. bool toml_is_array(const toml_t* toml) bool toml_is_float(const toml_t* toml) bool toml_is_table(const toml_t* toml) bool toml_is_string(const toml_t* toml) bool toml_is_boolean(const toml_t* toml) bool toml_is_integer(const toml_t* toml) bool toml_is_datetime(const toml_t* toml) These functions return true is toml is non-null and has the specified type. 2.3. API Reference 7

Reference Counting toml_t* toml_incref(toml_t* toml) Atomically increments the reference count of toml and returns toml. void toml_decref(toml_t* toml) Atomically decrements the reference count of toml and destroys toml if the reference count has reached 0. When a TOML value is created, it has a reference count of one. Storing values in tables or arrays increments the reference count of that value unless you use one of the functions ending in _with. The TOML values you receive from functions such as toml_table_get() are borrowed from their parent table or array; you should not call toml_decref() on these values. To extend the lifetime of these borrowed values beyond the life of their parent table or array, either use toml_incref() to increase the value s reference count or create a copy with toml_copy() or toml_deep_copy(). 2.3.3 Array An array is a homogenous, ordered collection of TOML values. toml_t* toml_create_array(void) Returns an empty array or NULL on allocation failure. size_t toml_array_get_size(const toml_t* toml) Returns the size of the array or 0 if toml is not an array. size_t toml_array_get_type(const toml_t* toml) Returns the type of the array s elements or TOML_NONE if toml is not an array or is empty. toml_t* toml_array_get(const toml_t* toml, size_t index) Returns the value at index or NULL if toml is not a table or index is out of range. int toml_array_set(toml_t* toml, size_t index, toml_t* element) Increments the reference count of element before replacing the value at index with element. toml_decref() is called on the replaced value. Returns 0 on success or -1 if toml is not an array, index is out of range, or the type of element does not match the type of the other elements. int toml_array_set_with(toml_t* toml, size_t index, toml_t* element) The same as above but the reference count of element is not incremented. int toml_array_add(toml_t* toml, toml_t* element) Increments the reference count of element before appending element to the end of the array. Returns 0 on success or -1 if toml is not an array or if growing the array fails. int toml_array_add_with(toml_t* toml, toml_t* element) The same as above but the reference count of element is not incremented. int toml_array_insert(toml_t* toml, size_t index, toml_t* element) Increments the reference count of element before inserting element into the array at index. Returns 0 on success or -1 if toml is not an array, index is out of range, or if growing the array fails. int toml_array_insert_with(toml_t* toml, size_t index, toml_t* element) The same as above but the reference count of element is not incremented. int toml_array_remove(toml_t* toml, size_t index) Calls toml_decref() on the value at index before removing it from the array. Returns 0 on success or -1 if toml is not an array or index is out of range. 8 Chapter 2. Table of Contents

int toml_array_clear(toml_t* toml) Calls toml_decref() on all the array s values before clearing the array. Returns 0 on success or -1 if toml is not an array. toml_array_foreach(toml, INDEX, VALUE) A convenience macro for iterating over the indices and values of an array. size_t index = 0; toml_t* value = NULL; toml_array_foreach(array, index, value) { //... } 2.3.4 Float A float is a double. toml_t* toml_create_float(double value) Returns a float with the supplied value or NULL on allocation failure. double toml_float_get(const toml_t* toml) Returns the double value of the float or 0.0 if toml is not a float. int toml_float_set(toml_t* toml, double value) Sets the double value of the float to the supplied value. Returns 0 on success or -1 if toml is not a float. 2.3.5 Table A table is an associative array mapping strings to TOML values of any type. toml_t* toml_create_table(void) Returns an empty table or NULL on allocation failure. size_t toml_table_get_size(const toml_t* toml) Returns the size of the table or 0 if toml is not a table. toml_t* toml_table_get(const toml_t* toml, const char* key) Returns the value associated with key or NULL if toml is not a table or if there is no value associated with key. toml_t* toml_table_get_n(const toml_t* toml, const char* key, size_t size) The same as above except only the first size characters of the key are considered. int toml_table_set(toml_t* toml, const char* key, toml_t* element) Increments the reference count of element before inserting element into the table under key. If there was a value previously associated with key, toml_decref() is called on it before it is replaced. Returns 0 on success or -1 if toml is not a table or if growing the table fails. int toml_table_set_n(toml_t* toml, const char* key, size_t size, toml_t* element) The same as above except only the first size characters of the key are considered. int toml_table_set_with(toml_t* toml, const char* key, toml_t* element) The same as toml_table_set() but the reference count of element is not incremented. int toml_table_set_with_n(toml_t* toml, const char* key, size_t size, toml_t* element) The same as above except only the first size characters of the key are considered. 2.3. API Reference 9

int toml_table_remove(toml_t* toml, const char* key) Calls toml_decref() on the value associated with key before removing it from the table. Returns 0 on success or -1 if toml is not a table, if there is no value associated with key, or if shrinking the table fails. int toml_table_remove_n(toml_t* toml, const char* key, size_t size) The same as above except only the first size characters of the key are considered. Iteration Unordered iteration over table key-value pairs is provided with an opaque iterator. void* toml_table_iter(const toml_t* toml) Returns an opaque iterator pointing to the first key-value pair in the table or NULL if toml is not a table or is empty. void* toml_table_iter_next(const toml_t* toml, void* iter) Returns an opaque iterator pointing to the next key-value pair after the one iter points to in the table or NULL if toml is not a table or iter is pointing at the last value. const char* toml_table_iter_get_key(void* iter) Returns the key of the key-value pair iter is pointing to. toml_t* toml_table_iter_get_value(void* iter) Returns the value of the key-value pair iter is pointing to. int toml_table_iter_set_value(void* iter, toml_t* toml) Increments the reference count of toml before replacing the value of the key-value pair iter points to with toml. toml_decref() is called on the previous value. The iterator is not invalidated. Returns 0 on success. int toml_table_iter_set_value_with(void* iter, toml_t* toml) The same as above but the reference count of toml is not incremented. toml_table_foreach(toml, ITER, KEY, VALUE) A convenience macro for iterating over the key-value pairs of a table. void* iter = NULL; const char* key = NULL; toml_t* value = NULL; toml_table_foreach(table, iter, key, value) { //... } Conversion Ordered iteration over table key-value pairs is supported with conversion to a list of key-value pairs. toml_key_value_t const char* key const toml_t* value toml_key_value_t* toml_table_to_list(const toml_t* toml) Returns an unordered list of the table s key-value pairs or NULL if toml is not a table or if allocation of the list fails. The list must be deallocated with free. 10 Chapter 2. Table of Contents

toml_comparer_t A typedef of the comparer function used to sort key-value pair lists. typedef int (*toml_comparer_t)(const toml_key_value_t*, const toml_key_value_t*); An example would be this function which sorts key-value pairs lexicographically by the key. int key_value_strcmp(const toml_key_value_t* a, const toml_key_value_t* b) { return strcmp(a->key, b->key); } toml_key_value_t* toml_table_to_ordered_list(const toml_t* toml, toml_comparer_t cmp) The same as toml_table_to_list() except the list is sorted using cmp. 2.3.6 String A string is a UTF-8 encoded string. Warning: Strings can contain embedded null terminators (\u0000). toml_t* toml_create_string(const char* value) Returns a string with a copy of the supplied value and the size set to strlen(value) or NULL on allocation failure. toml_t* toml_create_string_n(const char* value, size_t size) The same as above except only the first size characters of the value are copied and the size is set to size. toml_t* toml_create_string_with(char* value) Returns a string that has taken ownership of value or NULL on allocation failure. toml_t* toml_create_string_with_n(char* value, size_t size) The same as above except the size is set to size. size_t toml_string_get_size(const toml_t* toml) Returns the size of the string or 0 if toml is not a string. const char* toml_string_get(const toml_t* toml) Returns the string or NULL if toml is not a string. int toml_string_set(toml_t* toml, const char* value) Sets the value of the string to a copy of the supplied value and the size. Returns 0 on success or -1 if toml is not a string or on allocation failure. int toml_string_set_n(toml_t* toml, const char* value, size_t size) The same as above except only the first size characters of the value are copied and the size is set to size. int toml_string_set_with(toml_t* toml, char* value) Sets the value of the string to value and takes ownership of value. Returns 0 on success. int toml_string_set_with_n(toml_t* toml, char* value, size_t size) The same as above except the size is set to size. 2.3.7 Boolean A boolean is a bool. 2.3. API Reference 11

toml_t* toml_create_boolean(bool value) Returns a boolean with the supplied value or NULL on allocation failure. bool toml_boolean_get(const toml_t* toml) Returns the bool value of the boolean or false if toml is not a boolean. int toml_boolean_set(toml_t* toml, bool value) Sets the bool value of the boolean to the supplied value. Returns 0 on success or -1 if toml is not a boolean. 2.3.8 Integer An integer is an int64_t. toml_t* toml_create_integer(int64_t value) Returns a integer with the supplied value or NULL on allocation failure. int64_t toml_boolean_get(const toml_t* toml) Returns the int64_t value of the integer or 0 if toml is not a integer. int toml_boolean_set(toml_t* toml, int64_t value) Sets the int64_t value of the integer to the supplied value. Returns 0 on success or -1 if toml is not a integer. 2.3.9 Datetime A datetime is a toml_utc_t. toml_utc_t Represents a UTC time point down to nanosecond precision. int32_t year 1 to 9,999 inclusive. int32_t month 1 to 12 inclusive. int32_t day 1 to 28, 29, 30, or 31 inclusive depending on the month and year int32_t hour 0 to 23 inclusive int32_t minute 0 to 59 inclusive int32_t second 0 to 59 inclusive int32_t nanosecond 0 to 999,999,999 inclusive TOML_INVALID_TIME Equivalent to ((time_t)-1). Represents an invalid value. time_t toml_utc_to_time(toml_utc_t utc) Returns a time_t equivalent to the supplied toml_utc_t or TOML_INVALID_TIME if utc is invalid or before the Unix epoch (1970-01-01T00:00:00Z). The nanosecond field of toml_utc_t is considered in that the second field is incremented if the nanosecond field is greater than or equal to 500,000,000. 12 Chapter 2. Table of Contents

toml_utc_t toml_utc_from_time(time_t time) Returns a toml_utc_t equivalent to the supplied time_t. The nanosecond field of the toml_utc_t is set to 0. If time is invalid (because time_t is signed and time is negative), the returned toml_utc_t will have its year field set to -1. bool toml_utc_equal(toml_utc_t a, toml_utc_t b) Returns whether a and b are equal. toml_t* toml_create_datetime(toml_utc_t value) Returns a datetime with the supplied value or NULL if value is invalid or on allocation failure. toml_utc_t toml_datetime_get(const toml_t* toml) Returns the toml_utc_t value of the datetime or a toml_utc_t with its year field set to -1 if toml is not a datetime. TOML_DATETIME_SIZE The maximum buffer size needed to print a datetime (including a null terminator). int toml_datetime_print(const toml_t* toml, char* buffer, size_t size) Prints the datetime s value to a buffer in RFC 3339 format. Returns the amount of characters written if the buffer was large enough (this is guaranteed if it is at least as large as TOML_DATETIME_SIZE) or how many characters would have been written if the buffer is not large enough. int toml_datetime_set(toml_t* toml, toml_utc_t value) Sets the toml_utc_t value of the datetime to the supplied value. Returns 0 on success or -1 if toml is not a datetime or value is invalid. 2.3.10 Copying Due to the use of reference counting, you do not need to make copies of TOML values when placing them in arrays and tables. However, you may want to make a copy of a value when you want to modify it but need to keep the original value around too. toml_t* toml_copy(const toml_t* toml) Returns a shallow copy of the supplied TOML value or NULL on allocation failure. For arrays and tables, this means that only the top-level array or table is copied; the elements of the copied array or table are references to the elements in the original array or table. This means that modifying the copied array or table itself (adding, removing, clearing, etc.) will not modify the original but modifying the elements of the copied array or table will. toml_t* toml_deep_copy(const toml_t* toml) Returns a deep copy of the supplied TOML value or NULL on allocation failure. This only differs from toml_copy() for arrays and tables. For arrays and tables, the elements are copied with toml_deep_copy() recursively, resulting in a copy completely independent from the original. 2.3.11 Equality bool toml_equal(const toml_t* a, const toml_t* b) Returns whether the types of a and b are the same as well as the underlying values. Integers and floats are never equal even if their underlying values are equal. Arrays are considered equal if they are the same size and each element is equal to its corresponding element in the other array. 2.3. API Reference 13

Tables are considered equal if they are the same size and each key-value pair in one table is present in the other table and the values are equal. Strings are compared with strcmp, no Unicode string comparison functions are used. 2.3.12 Error Reporting enum toml_error_type The type of a TOML error. TOML_ERROR_NONE TOML_ERROR_FILE TOML_ERROR_INVALID TOML_ERROR_ENCODING TOML_ERROR_ALLOCATION toml_error_t No error reported. An error relating to opening or reading a file. An error relating to invalid TOML. An error relating to invalid UTF-8 encoding. An error relating to allocation failure. enum toml_error_type type The type of the error. size_t index The index in the source where the error occurred. TOML_ERROR_INVALID. Only applies to TOML_ERROR_ENCODING or int line The line in the source where the error occurred. Only applies to TOML_ERROR_INVALID. int column The column in the source where the error occurred. Only applies to TOML_ERROR_INVALID. char message[] The error message. 2.3.13 Parsing toml_t* toml_load(const char* source, toml_error_t* error) Parses the TOML document supplied as source. Populates error with appropriate information. Returns a table representing the root of the TOML document or NULL on error. toml_t* toml_load_n(const char* source, size_t size, toml_error_t* error) The same as above except only the first size characters of the source are used. toml_t* toml_load_with(char* source, toml_error_t* error) The same as toml_load() except the TOML parser takes ownership of the source. toml_t* toml_load_with_n(char* source, size_t size, toml_error_t* error) The same as above except only the first size characters of the source are used. Requires that the size of source be at least longer than size. toml_t* toml_load_file(const char* filepath, toml_error_t* error) Opens and reads the file at filepath and parses the contents as a TOML document. Populates error with appropriate information. Returns a table representing the root of the TOML document or NULL on error. 14 Chapter 2. Table of Contents

2.3.14 Writing The TOML and JSON writing functions take a flags parameter. TOML_COMPACT This flag determines whether the TOML or JSON will be reduced in size. TOML JSON No spaces in arrays No spaces between keys, =, and values No extra newlines between tables No spaces in arrays No spaces between : and values TOML_ASCII_ONLY This flag determines whether all non-ascii characters are escaped when possible. TOML JSON Keys are not strings in TOML so non-ascii characters will not be escaped in them Keys are strings in JSON so non-ascii characters will be escaped in them JSON does not support 4-byte escape sequences (\Uxxxxxxxx), so codepoints greater than 0xFFFF will be escaped as UTF-16 surrogate pairs TOML_PRECISION(VALUE) This flag determines the precision of floats when printed. The default value is 17. The value must be between 0 and 24 inclusive. TOML_INDENTATION(VALUE) This flag determines the number of spaces to be used as indentation. The default value is 2. The value must be between 0 and 16 inclusive. char* toml_dump(const toml_t* toml, uint64_t flags) Returns the TOML string representation of toml or NULL on allocation failure. The return value must be deallocated with free. char* toml_dump_json(const toml_t* toml, uint64_t flags) Returns the JSON string representation of toml or NULL on allocation failure. The return value must be deallocated with free. int toml_dump_file(const char* filepath, const toml_t* toml, uint64_t flags) Writes the TOML string representation of toml to the file at filepath, replacing it if it exists. Returns 0 on success or -1 on failure. int toml_dump_json_file(const char* filepath, const toml_t* toml, uint64_t flags) Writes the JSON string representation of toml to the file at filepath, replacing it if it exists. Returns 0 on success or -1 on failure. 2.3. API Reference 15

16 Chapter 2. Table of Contents

CHAPTER 3 Indices and tables genindex search 17

18 Chapter 3. Indices and tables

Index R RFC RFC 3339, 13 T toml_array_add (C function), 8 toml_array_add_with (C function), 8 toml_array_clear (C function), 8 toml_array_foreach (C macro), 9 toml_array_get (C function), 8 toml_array_get_size (C function), 8 toml_array_get_type (C function), 8 toml_array_insert (C function), 8 toml_array_insert_with (C function), 8 toml_array_remove (C function), 8 toml_array_set (C function), 8 toml_array_set_with (C function), 8 TOML_ASCII_ONLY (C macro), 15 toml_boolean_get (C function), 12 toml_boolean_set (C function), 12 TOML_COMPACT (C macro), 15 toml_comparer_t (C type), 10 toml_copy (C function), 13 toml_create_array (C function), 8 toml_create_boolean (C function), 11 toml_create_datetime (C function), 13 toml_create_float (C function), 9 toml_create_integer (C function), 12 toml_create_string (C function), 11 toml_create_string_n (C function), 11 toml_create_string_with (C function), 11 toml_create_string_with_n (C function), 11 toml_create_table (C function), 9 toml_datetime_get (C function), 13 toml_datetime_print (C function), 13 toml_datetime_set (C function), 13 TOML_DATETIME_SIZE (C macro), 13 toml_decref (C function), 8 toml_deep_copy (C function), 13 toml_dump (C function), 15 toml_dump_file (C function), 15 toml_dump_json (C function), 15 toml_dump_json_file (C function), 15 toml_equal (C function), 13 toml_error_t (C type), 14 toml_error_t.column (C member), 14 toml_error_t.index (C member), 14 toml_error_t.line (C member), 14 toml_error_t.type (C member), 14 toml_error_type (C type), 14 toml_float_get (C function), 9 toml_float_set (C function), 9 toml_get_type (C function), 7 toml_incref (C function), 8 TOML_INDENTATION (C macro), 15 TOML_INVALID_TIME (C macro), 12 toml_is (C function), 7 toml_is_array (C function), 7 toml_is_boolean (C function), 7 toml_is_datetime (C function), 7 toml_is_float (C function), 7 toml_is_integer (C function), 7 toml_is_string (C function), 7 toml_is_table (C function), 7 toml_key_value_t (C type), 10 toml_key_value_t.key (C member), 10 toml_key_value_t.value (C member), 10 toml_load (C function), 14 toml_load_file (C function), 14 toml_load_n (C function), 14 toml_load_with (C function), 14 toml_load_with_n (C function), 14 TOML_NONE (C macro), 7 TOML_PRECISION (C macro), 15 toml_string_get (C function), 11 toml_string_get_size (C function), 11 toml_string_set (C function), 11 toml_string_set_n (C function), 11 toml_string_set_with (C function), 11 toml_string_set_with_n (C function), 11 toml_t (C type), 7 19

toml_table_foreach (C macro), 10 toml_table_get (C function), 9 toml_table_get_n (C function), 9 toml_table_get_size (C function), 9 toml_table_iter (C function), 10 toml_table_iter_get_key (C function), 10 toml_table_iter_get_value (C function), 10 toml_table_iter_next (C function), 10 toml_table_iter_set_value (C function), 10 toml_table_iter_set_value_with (C function), 10 toml_table_remove (C function), 9 toml_table_remove_n (C function), 10 toml_table_set (C function), 9 toml_table_set_n (C function), 9 toml_table_set_with (C function), 9 toml_table_set_with_n (C function), 9 toml_table_to_list (C function), 10 toml_table_to_ordered_list (C function), 11 toml_type (C type), 7 toml_utc_equal (C function), 13 toml_utc_from_time (C function), 12 toml_utc_t (C type), 12 toml_utc_t.day (C member), 12 toml_utc_t.hour (C member), 12 toml_utc_t.minute (C member), 12 toml_utc_t.month (C member), 12 toml_utc_t.nanosecond (C member), 12 toml_utc_t.second (C member), 12 toml_utc_t.year (C member), 12 toml_utc_to_time (C function), 12 20 Index