Skip to content

Quick Start Guide

Pablo R. edited this page Sep 29, 2025 · 2 revisions

Quick Start Guide

Installation

Prerequisites

  • C99 compatible compiler (gcc, clang, MSVC)
  • GNU Make (Linux/macOS) or compatible build system
  • Standard C library

Building from Source

  1. Clone the repository:

    git clone https://github.com/murapadev/strap.git
    cd strap
  2. Build the library:

    make

    This creates libstrap.a in the project directory.

  3. Install system-wide (optional):

    sudo make install

    This installs:

    • Header: /usr/local/include/strap.h
    • Library: /usr/local/lib/libstrap.a

Integration into Your Project

Method 1: System Installation

If you installed STRAP system-wide:

#include <strap.h>

Compile with:

gcc -o myprogram myprogram.c -lstrap

Method 2: Local Include

If using STRAP locally in your project:

#include "path/to/strap.h"

Compile with:

gcc -o myprogram myprogram.c -L/path/to/strap -lstrap

Your First STRAP Program

Create a file called hello_strap.c:

#include <stdio.h>
#include <stdlib.h>
#include "strap.h"

int main(void) {
    printf("Enter your name: ");

    // Safe line reading
    char *name = afgets(stdin);
    if (!name) {
        fprintf(stderr, "Error reading input\n");
        return 1;
    }

    // Trim whitespace
    char *trimmed_name = strtrim(name);

    // Create greeting
    char *greeting = strjoin_va(" ", "Hello,", trimmed_name, "!", NULL);

    printf("%s\n", greeting);

    // Cleanup
    free(name);
    free(trimmed_name);
    free(greeting);

    return 0;
}

Compile and run:

gcc -I. -L. -o hello_strap hello_strap.c -lstrap
./hello_strap

Error Handling

STRAP provides unified error reporting for all operations. When a function fails, it sets a thread-local error code that you can query:

#include <stdio.h>
#include "strap.h"

int main(void) {
    // This will fail and set an error
    char *result = strtrim(NULL);
    if (!result) {
        // Get the error code and message
        strap_error_t err = strap_last_error();
        fprintf(stderr, "STRAP error: %s\n", strap_error_string(err));
        return 1;
    }

    // Clear error state if needed
    strap_clear_error();

    free(result);
    return 0;
}

Common Use Cases

1. Configuration File Reading

#include <stdio.h>
#include "strap.h"

void read_config(const char *filename) {
    FILE *fp = fopen(filename, "r");
    if (!fp) return;

    char *line;
    while ((line = afgets(fp)) != NULL) {
        char *trimmed = strtrim(line);
        if (trimmed[0] != '#' && trimmed[0] != '\0') {
            printf("Config line: %s\n", trimmed);
        }
        free(line);
        free(trimmed);
    }

    fclose(fp);
}

2. Path Construction

#include "strap.h"

char *build_path(const char *base, const char *file) {
    return strjoin_va("/", base, file, NULL);
}

int main(void) {
    char *config_path = build_path("/etc/myapp", "config.ini");
    printf("Config at: %s\n", config_path);
    free(config_path);
    return 0;
}

3. Time Measurements

#include <sys/time.h>
#include "strap.h"

void benchmark_operation(void) {
    struct timeval start, end, diff;

    gettimeofday(&start, NULL);

    // Your operation here
    for (int i = 0; i < 1000000; i++) {
        // Some work
    }

    gettimeofday(&end, NULL);
    diff = timeval_sub(end, start);

    printf("Operation took %.6f seconds\n", timeval_to_seconds(diff));
}

Testing Your Installation

Run the included tests to verify everything works:

cd tests
gcc -I.. -L.. -o test_strap test_strap.c -lstrap
./test_strap

You should see:

strtrim tests passed
strtrim_inplace tests passed
strjoin tests passed
strjoin_va tests passed
timeval tests passed
All tests passed!

Running Benchmarks

STRAP includes micro-benchmarks to test performance:

make bench
./benchmarks/strap_bench

This will run performance tests for various string and time operations.

Next Steps

Troubleshooting

Common Issues

"strap.h: No such file or directory"

  • Make sure you're including the correct path
  • Verify the header file exists in your include path

"undefined reference to 'afgets'"

  • Ensure you're linking with -lstrap
  • Check that libstrap.a is in your library path

Build fails on Windows

  • Use MinGW or MSYS2 for Windows builds
  • The CI includes Windows build instructions

Getting Help