A lightweight in-memory database library written in C that provides table creation, data manipulation, and formatted ASCII table output capabilities.
- Table Management: Create and destroy tables with custom schemas
- Multiple Data Types: Support for INT, FLOAT, CHAR, and STRING types
- Memory Safe: Robust memory management with protection against buffer overflows
- Formatted Output: Beautiful ASCII table formatting for data visualization
- Comprehensive Testing: Extensive test suite covering various use cases
INT- Integer valuesFLOAT- Floating-point numbersCHAR- Single charactersSTRING- String values (dynamically allocated)
#include "db.h"
#include "ascii_db/ascii_table.h"
// Create a table
table_t *users = create_table("users");
add_column(users, "id", INT);
add_column(users, "name", STRING);
add_column(users, "age", INT);
// Add data
val_t row1[3] = {{.i = 1}, {.string = "Alice"}, {.i = 25}};
val_t row2[3] = {{.i = 2}, {.string = "Bob"}, {.i = 30}};
add_row(users, row1);
add_row(users, row2);
// Display table
print(users);
// Cleanup
destroy_table(users);table_t* create_table(const char* name)- Create new tablevoid add_column(table_t* table, const char* name, data_type_t type)- Add column to tablevoid add_row(table_t* table, val_t* values)- Insert row of datavoid print(table_t* table)- Display formatted tablevoid destroy_table(table_t* table)- Free table memory
typedef union {
int i;
float f;
char c;
char* string;
} val_t;# Build all test executables
make build_all
# Run specific test
make run_test_4
# Run complete test suite
make test_all
# Clean build artifacts
make cleanThe library includes comprehensive test coverage:
- Basic User Table - Simple user data management
- Product Inventory - Mixed data types with business data
- Empty Table - Edge case with no rows
- Single Row Table - Minimal data set
- Mixed Data Types - All supported types in one table
- Student Records - Academic data structure
- Maximum Columns - Stress test for column limits
- Employee Database - Complex business data
- Temperature Readings - Scientific data formatting
- Book Collection - Library management example
This library implements robust memory management:
- Dynamic Allocation: Strings are properly allocated and freed
- Buffer Overflow Protection: All string operations use exact-sized buffers
- Leak Prevention: Comprehensive cleanup in
destroy_table() - Sanitizer Ready: Compatible with AddressSanitizer for debugging
+-------+-------------+-------+
| uid | name | age |
+-------+-------------+-------+
| 123 | Malinga | 23 |
| 124 | Nagaraj | 21 |
| 125 | Shree Nath | 21 |
+-------+-------------+-------+
MIT License
Copyright (c) 2025 [MARK]
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- Ensure all tests pass:
make test_all - Run with AddressSanitizer for memory checking
- Follow existing code style and patterns
- Add tests for new functionality