#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "alec.h"
static const double temperature_readings[] = {
22.1, 22.2, 22.1, 22.3, 22.2, 22.4, 22.3, 22.5,
22.4, 22.6, 22.5, 22.7, 22.6, 22.5, 22.4, 22.3
};
static const size_t NUM_READINGS = sizeof(temperature_readings) / sizeof(temperature_readings[0]);
int example_basic_encoding(void) {
printf("\n=== Basic Encoding Example ===\n");
AlecEncoder* enc = alec_encoder_new();
if (!enc) {
fprintf(stderr, "Failed to create encoder\n");
return 1;
}
uint8_t compressed[256];
size_t compressed_len;
size_t total_compressed = 0;
for (size_t i = 0; i < NUM_READINGS; i++) {
AlecResult res = alec_encode_value(
enc,
temperature_readings[i],
i * 1000,
"temp_sensor_1",
compressed,
sizeof(compressed),
&compressed_len
);
if (res != ALEC_OK) {
fprintf(stderr, "Encoding failed: %s\n", alec_result_to_string(res));
alec_encoder_free(enc);
return 1;
}
total_compressed += compressed_len;
printf(" Value %.1f -> %zu bytes\n", temperature_readings[i], compressed_len);
}
size_t original_size = NUM_READINGS * sizeof(double);
double ratio = (1.0 - (double)total_compressed / original_size) * 100;
printf("\nResults:\n");
printf(" Original size: %zu bytes (%zu values x %zu bytes)\n",
original_size, NUM_READINGS, sizeof(double));
printf(" Compressed size: %zu bytes\n", total_compressed);
printf(" Compression: %.1f%%\n", ratio);
printf(" Context version: %u\n", alec_encoder_context_version(enc));
alec_encoder_free(enc);
return 0;
}
int example_multi_encoding(void) {
printf("\n=== Multi-Value Encoding Example ===\n");
AlecEncoder* enc = alec_encoder_new();
if (!enc) {
fprintf(stderr, "Failed to create encoder\n");
return 1;
}
uint8_t compressed[512];
size_t compressed_len;
AlecResult res = alec_encode_multi(
enc,
temperature_readings,
NUM_READINGS,
0,
"temp_sensor_batch",
compressed,
sizeof(compressed),
&compressed_len
);
if (res != ALEC_OK) {
fprintf(stderr, "Multi-encoding failed: %s\n", alec_result_to_string(res));
alec_encoder_free(enc);
return 1;
}
size_t original_size = NUM_READINGS * sizeof(double);
double ratio = (1.0 - (double)compressed_len / original_size) * 100;
printf(" Original size: %zu bytes (%zu values)\n", original_size, NUM_READINGS);
printf(" Compressed size: %zu bytes\n", compressed_len);
printf(" Compression: %.1f%%\n", ratio);
alec_encoder_free(enc);
return 0;
}
int example_with_checksum(void) {
printf("\n=== Encoding with Checksum Example ===\n");
AlecEncoder* enc = alec_encoder_new_with_checksum();
if (!enc) {
fprintf(stderr, "Failed to create encoder\n");
return 1;
}
uint8_t compressed[256];
size_t compressed_len;
AlecResult res = alec_encode_value(
enc,
22.5,
1234567890,
"checksum_test",
compressed,
sizeof(compressed),
&compressed_len
);
if (res != ALEC_OK) {
fprintf(stderr, "Encoding with checksum failed: %s\n", alec_result_to_string(res));
alec_encoder_free(enc);
return 1;
}
printf(" Encoded with checksum: %zu bytes\n", compressed_len);
printf(" (Checksum adds ~4 bytes for integrity verification)\n");
alec_encoder_free(enc);
return 0;
}
int main(void) {
printf("ALEC C Bindings Example\n");
printf("Version: %s\n", alec_version());
printf("========================\n");
int result = 0;
result |= example_basic_encoding();
result |= example_multi_encoding();
result |= example_with_checksum();
if (result == 0) {
printf("\n=== All examples completed successfully ===\n");
}
return result;
}