This lightweight library provides a simple, efficient way to parse, manipulate, and save CSV (and other delimited) files in C. It features automatic type detection, sorting capabilities, and flexible data handling.
0.2 BETA
- Automatic type detection: Strings, integers, floats, and NULL values
- Flexible parsing: Customizable delimiters and parsing options
- Sorting capabilities: Sort by column index or name, ascending or descending
- Memory efficient: Smart memory management with automatic cleanup
- Comprehensive logging: Configurable logging levels for debugging
- Header support: Automatic header detection and handling
- Cross-platform: Works on any platform with a C99 compiler
- Add
fileparser.handfileparser.cto your project - Include the header in your code:
#include "fileparser.h"- C99 compatible compiler
- Standard C libraries (stdio.h, stdlib.h, string.h)
#include "fileparser.h"
int main()
{
// create a parser with default settings
PARSER parser = create_parser();
// parse a CSV file
if (parse_file(&parser, "data.csv"))
{
printf("Failed to parse file\n");
return 1;
}
// print all data
print_all_data(&parser);
// sort by the second column (index 1)
PARSER_SORT_SETTINGS sort_settings = create_parser_sort_settings();
sort_settings.tag = COLUMN_INDEX;
sort_settings.value.column_index = 1;
sort_settings.direction = ASCENDING;
if (sort_data(&parser, sort_settings))
{
printf("Failed to sort data\n");
}
// save the sorted data
save_data(&parser, "sorted_data.csv");
// always clean up
free_parser(&parser);
return 0;
}#include "fileparser.h"
int main()
{
// create custom parser settings
PARSER_SETTINGS settings = create_parser_settings();
settings.splitter = ','; // use comma as delimiter
settings.ignore_first_line = 0; // don't ignore first line
settings.first_line_as_header = 1; // treat first line as header
// change default settings
change_default_settings(settings);
// create parser with custom settings
PARSER parser = create_parser();
// parse file
if (parse_file(&parser, "data.csv"))
{
PARSER_LOG_CRITICAL("Failed to parse file");
return 1;
}
// sort by column name
PARSER_SORT_SETTINGS sort_settings = create_parser_sort_settings();
sort_settings.tag = COLUMN_NAME;
sort_settings.value.column_name = "Age"; // column name to sort by
sort_settings.direction = DESCENDING;
sort_settings.case_sensitive = 0; // case insensitive comparison
if (sort_data(&parser, sort_settings))
{
PARSER_LOG_CRITICAL("Failed to sort data");
}
else
{
// save sorted data with pipe delimiter
parser.settings.splitter = '|';
save_data(&parser, "sorted_data.txt");
}
// always clean up
free_parser(&parser);
return 0;
}-
PARSER create_parser()
Creates and returns a new parser object with default settings. -
int parse_file(PARSER* parser, const char* filename)
Parses a file and stores the data in the parser object. (You can do your custom logic with it) -
void free_parser(PARSER* parser)
Frees all resources associated with the parser.
-
int sort_data(PARSER* parser, PARSER_SORT_SETTINGS settings)
Sorts the parsed data by the specified column. -
int save_data(PARSER* parser, const char* filename)
Saves the parsed data to a file.
-
int print_all_data(PARSER* parser)
Prints all parsed data to the console. -
int print_data(PARSER* parser, size_t how_much_to_print)
Prints a specified amount of parsed data to the console.
-
PARSER_SETTINGS create_parser_settings()
Creates a new settings object with default values. -
void change_default_settings(PARSER_SETTINGS settings)
Changes the default parser settings. -
PARSER_SORT_SETTINGS create_parser_sort_settings()
Creates a new sort settings object with default values. -
void change_default_sort_settings(PARSER_SORT_SETTINGS settings)
Changes the default sort settings.
The library provides configurable logging levels:
#define LOGLEVEL_CRITICAL 0 // only critical errors
#define LOGLEVEL_WARNING 1 // warnings and errors
#define LOGLEVEL_INFO 2 // informational messages (default)
#define LOGLEVEL_DEBUG 3 // debug information
#define LOGLEVEL_NONE 4 // no loggingTo change the logging level, modify the LOG_LEVEL definition in fileparser.h:
#define LOG_LEVEL LOGLEVEL_WARNING // change to the desired levelCustomize parsing behavior with PARSER_SETTINGS:
splitter: Character used to separate values (default: ';')ignore_first_line: Whether to ignore the first line (default: 0)ignore_errors: Whether to continue parsing on errors (default: 1)first_line_as_header: Whether to treat the first line as header (default: 1)
Customize sorting behavior with PARSER_SORT_SETTINGS:
tag: Specify whether to sort byCOLUMN_NAMEorCOLUMN_INDEXvalue: The column name or index to sort bydirection:ASCENDINGorDESCENDINGordercase_sensitive: Whether string comparisons should be case sensitive (default: 1)
Compile with your project:
gcc <your_app.c> fileparser.c -o your_appThe library automatically detects and handles these data types:
- STRING_TYPE: Text values (quoted or unquoted)
- INTEGER_TYPE: Whole numbers
- FLOAT_TYPE: Decimal numbers
- NULL_TYPE: Empty values or explicit NULL strings
NOTE: More to be added in the future.
-
Memory Management
- Always use
free_parser()to properly free parser resources
- Always use
-
File Format
- Supports any delimiter (CHAR) (configurable via
splittersetting) - Handles multi quoted values (like """hello""")
- Automatically trims whitespace and newlines
- Recognizes "NULL" (case-insensitive) as a null value
- Supports any delimiter (CHAR) (configurable via
-
Performance
- Efficient parsing with minimal memory overhead
- Sorting uses quicksort algorithm for performance
-
Error Handling
- Comprehensive error logging at multiple levels
- All functions return error codes for programmatic handling
-
Header Handling
- Headers are automatically converted to strings if needed
- Missing headers are given automatic names (__parser_column_%d__) where %d stands for column index
Contributions are welcome! Please submit pull requests or open issues on GitHub.