C Code Generation

Generate portable ANSI C code with cross-platform compatibility and comprehensive helper libraries.

Overview

Circuitry generates C code that:

  • Follows ANSI C99/C11 standards
  • Works on Windows, Linux, and macOS
  • Includes memory management
  • Provides HTTP support via libcurl or WinINet
  • Handles JSON with custom parser

Generated Code Structure

#include <stdio.h>
#include <stdlib.h>
#include "circuitry_helpers.h"
#include "circuitry_ui.h"

WorkflowData* workflow_name(WorkflowData* input) {
    WorkflowData* data = input ? clone_workflow_data(input) : NULL;
    
    // HTTP Request
    HttpResponse* response = http_request("https://api.example.com", "GET", NULL, NULL);
    if (response) {
        data = parse_json_to_workflow(response->body);
        free_http_response(response);
    }
    
    // User confirmation
    ConfirmResult* confirm = confirm_action("Continue?", "Confirmation");
    if (confirm->confirmed) {
        // Process confirmed branch
        data = process_data(data);
    }
    free_confirm_result(confirm);
    
    return data;
}

Helper Libraries

Installation

Download the helper libraries from Code Generation dialog:

  • circuitry_helpers.h - Core function declarations
  • circuitry_helpers.c - Core implementations
  • circuitry_ui.h - UI function declarations
  • circuitry_ui.c - UI implementations
  • Makefile - Build configuration

Building

# Using Make
make

# Manual compilation (Linux/Mac with curl)
gcc -o workflow workflow.c circuitry_helpers.c circuitry_ui.c -lcurl

# Windows (with WinINet)
cl workflow.c circuitry_helpers.c circuitry_ui.c wininet.lib

Core Functions

HTTP Requests

// Simple GET request
HttpResponse* response = http_request(
    "https://api.example.com",
    "GET",
    NULL,  // body
    NULL   // headers
);

// POST with body and headers
const char* body = "{\"key\": \"value\"}";
const char* headers = "Content-Type: application/json\nAuthorization: Bearer token";
HttpResponse* response = http_request(url, "POST", body, headers);

// Clean up
free_http_response(response);

JSON Handling

// Parse JSON string
char* value = parse_json_field("{\"name\": \"John\"}", "name");
printf("Name: %s\n", value);
free(value);

// Create JSON
const char* pairs[] = {"name", "John", "age", "30"};
char* json = create_json(pairs, 2);
printf("JSON: %s\n", json);
free(json);

Template Variables

char* template = "Hello {{name}}, your ID is {{id}}";
char* data = "{\"name\": \"Alice\", \"id\": \"12345\"}";
char* result = replace_template_variables(template, data);
printf("%s\n", result);  // "Hello Alice, your ID is 12345"
free(result);

UI Functions

Confirmations

ConfirmResult* result = confirm_action("Process order?", "Order Confirmation");
if (result->confirmed) {
    printf("User confirmed at %s\n", result->timestamp);
}
free_confirm_result(result);

Messages

MessageResult* msg = show_message("Operation complete!", "success", "Success");
if (msg->acknowledged) {
    printf("Message shown: %s\n", msg->message);
}
free_message_result(msg);

Form Input

FormField fields[] = {
    {"email", "Email Address", "email", true, NULL},
    {"age", "Age", "number", false, "18"}
};

FormResult* form = get_form_input(fields, 2, "User Registration");
if (form->submitted) {
    for (int i = 0; i < form->field_count; i++) {
        printf("%s: %s\n", form->field_names[i], form->field_values[i]);
    }
}
free_form_result(form);

Memory Management

Best Practices

// Always free allocated memory
WorkflowData* data = create_workflow_data();
// ... use data
free_workflow_data(data);

// Check for NULL before using
HttpResponse* response = http_request(url, "GET", NULL, NULL);
if (response != NULL) {
    // Use response
    free_http_response(response);
}

// Use clone for data preservation
WorkflowData* original = get_data();
WorkflowData* copy = clone_workflow_data(original);
// Modify copy without affecting original
free_workflow_data(copy);

Error Handling

// Check return values
HttpResponse* response = http_request(url, "GET", NULL, NULL);
if (response == NULL) {
    log_message("ERROR", "HTTP request failed");
    return NULL;
}

if (response->status_code != 200) {
    log_message("ERROR", "Unexpected status code");
    free_http_response(response);
    return NULL;
}

// Process successful response
WorkflowData* data = parse_response(response);
free_http_response(response);
return data;

Cross-Platform Compilation

Linux

# Install dependencies
sudo apt-get install libcurl4-openssl-dev

# Compile
gcc -o workflow workflow.c circuitry_helpers.c circuitry_ui.c -lcurl -lm

macOS

# Install dependencies (curl is usually pre-installed)
# If needed: brew install curl

# Compile
gcc -o workflow workflow.c circuitry_helpers.c circuitry_ui.c -lcurl

Windows

REM Using Visual Studio Developer Command Prompt
cl /Fe:workflow.exe workflow.c circuitry_helpers.c circuitry_ui.c wininet.lib

REM Using MinGW
gcc -o workflow.exe workflow.c circuitry_helpers.c circuitry_ui.c -lwininet

Integration Examples

Command-Line Tool

int main(int argc, char* argv[]) {
    WorkflowData* input = NULL;
    
    // Parse command-line arguments
    if (argc > 1) {
        input = parse_json_to_workflow(argv[1]);
    }
    
    // Run workflow
    WorkflowData* result = workflow_name(input);
    
    // Output result
    if (result) {
        char* json = workflow_data_to_json(result);
        printf("%s\n", json);
        free(json);
        free_workflow_data(result);
    }
    
    if (input) {
        free_workflow_data(input);
    }
    
    return 0;
}

Embedded System

// For resource-constrained environments
#define MAX_DATA_SIZE 1024
#define MINIMAL_MEMORY

typedef struct {
    char buffer[MAX_DATA_SIZE];
    size_t size;
} CompactData;

CompactData* run_workflow(const char* input) {
    static CompactData result;
    
    // Minimal processing
    result.size = snprintf(result.buffer, MAX_DATA_SIZE, 
                          "{\"status\": \"processed\"}");
    
    return &result;
}

Testing

Unit Tests

#include <assert.h>

void test_json_parsing() {
    char* json = "{\"name\": \"test\", \"value\": 42}";
    
    char* name = parse_json_field(json, "name");
    assert(strcmp(name, "test") == 0);
    free(name);
    
    char* value = parse_json_field(json, "value");
    assert(strcmp(value, "42") == 0);
    free(value);
}

void test_template_replacement() {
    char* template = "Hello {{name}}";
    char* data = "{\"name\": \"World\"}";
    
    char* result = replace_template_variables(template, data);
    assert(strcmp(result, "Hello World") == 0);
    free(result);
}

int main() {
    test_json_parsing();
    test_template_replacement();
    printf("All tests passed!\n");
    return 0;
}

Best Practices

  1. Memory Management: Always free allocated memory
  2. Error Checking: Check all return values
  3. Buffer Sizes: Use safe string functions (snprintf vs sprintf)
  4. Logging: Use log_message for debugging
  5. Portability: Test on target platforms