Skip to content

Instantly share code, notes, and snippets.

@mcnaveen
Last active January 5, 2025 07:58
Show Gist options
  • Save mcnaveen/f6561c0f16569eef25d773a31ab9194f to your computer and use it in GitHub Desktop.
Save mcnaveen/f6561c0f16569eef25d773a31ab9194f to your computer and use it in GitHub Desktop.
A lightweight C library for fetching the current year from getfullyear.com. Because sometimes, you need to get the full year properly, with all the bells and whistles.

get-full-year πŸ—“οΈ

A lightweight C library for fetching the current year from getfullyear.com. Because sometimes, you need to get the full year properly, with all the bells and whistles.

πŸš€ Features

  • Simple and intuitive API
  • Minimal dependencies (uses libcurl for HTTP requests)
  • Modular design
  • Enterprise mode support
  • Full control over memory management

πŸ“¦ Installation

Prerequisites

  • A C compiler (GCC, Clang, etc.)
  • libcurl installed on your system

Build Instructions

  1. Clone the repository:
git clone https://github.com/yourusername/get-full-year-c.git
cd get-full-year-c
  1. Compile the library:
gcc -o getfullyear main.c getfullyear.c -lcurl
  1. Include the getfullyear.h header in your C project.

Example

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

int main() {
    // Fetch the year data in standard mode
    YearResponseDTO *yearData = getFullYear(false);

    if (yearData) {
        printf("Year: %d\n", yearData->year);
        printf("Sponsored By: %s\n", yearData->sponsored_by);
        printf("Metadata: %s\n", yearData->metadata);

        // Free memory after use
        freeYearResponseDTO(yearData);
    } else {
        printf("Failed to fetch year data\n");
    }

    return 0;
}

Output

πŸš€ Initiating year acquisition process in STANDARD mode...
πŸ“‘ Establishing connection to temporal data service at https://getfullyear.com/api/year
βœ… HTTP response received. Parsing...
✨ Sponsored by our magnificent partner: Mock Sponsor
βœ… Year acquisition completed successfully
Year: 2025
Sponsored By: Mock Sponsor
Metadata: {"year":2025,"sponsored_by":"Mock Sponsor"}
#include "getfullyear.h"
#include <stdio.h>
int main() {
YearResponseDTO *yearData = getFullYear(false);
if (yearData) {
printf("Year: %d\n", yearData->year);
printf("Sponsored By: %s\n", yearData->sponsored_by);
printf("Metadata: %s\n", yearData->metadata);
freeYearResponseDTO(yearData);
} else {
printf("Failed to fetch year data\n");
}
return 0;
}
#include "getfullyear.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
// Error buffer for libcurl
static char errorBuffer[CURL_ERROR_SIZE];
// Helper struct to store HTTP response data
typedef struct {
char *memory;
size_t size;
} MemoryBlock;
// Write callback for libcurl
static size_t writeCallback(void *contents, size_t size, size_t nmemb, void *userp) {
size_t realSize = size * nmemb;
MemoryBlock *mem = (MemoryBlock *)userp;
char *ptr = realloc(mem->memory, mem->size + realSize + 1);
if (!ptr) {
fprintf(stderr, "Not enough memory (realloc failed)\n");
return 0;
}
mem->memory = ptr;
memcpy(&(mem->memory[mem->size]), contents, realSize);
mem->size += realSize;
mem->memory[mem->size] = '\0';
return realSize;
}
// Function to fetch the current year
YearResponseDTO *getFullYear(bool isEnterprise) {
CURL *curl;
CURLcode res;
MemoryBlock chunk = { .memory = NULL, .size = 0 };
const char *temporalDataEndpoint = "https://getfullyear.com/api/year";
printf("πŸš€ Initiating year acquisition process %s...\n",
isEnterprise ? "in ENTERPRISE mode" : "in STANDARD mode");
curl = curl_easy_init();
if (!curl) {
fprintf(stderr, "❌ Failed to initialize cURL\n");
return NULL;
}
chunk.memory = malloc(1); // Initial memory allocation
if (!chunk.memory) {
fprintf(stderr, "❌ Not enough memory (malloc failed)\n");
curl_easy_cleanup(curl);
return NULL;
}
curl_easy_setopt(curl, CURLOPT_URL, temporalDataEndpoint);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
fprintf(stderr, "❌ cURL error: %s\n", errorBuffer);
free(chunk.memory);
curl_easy_cleanup(curl);
return NULL;
}
printf("βœ… HTTP response received. Parsing...\n");
// Parse the JSON response (mock parsing for brevity)
YearResponseDTO *dto = (YearResponseDTO *)malloc(sizeof(YearResponseDTO));
if (!dto) {
fprintf(stderr, "❌ Not enough memory (malloc failed)\n");
free(chunk.memory);
curl_easy_cleanup(curl);
return NULL;
}
// Mock parsing - replace with actual JSON parsing logic (e.g., using cJSON or Jansson)
dto->year = 2025; // Hardcoded for demonstration
dto->sponsored_by = strdup("Mock Sponsor");
dto->metadata = strdup(chunk.memory);
if (!isEnterprise) {
printf("✨ Sponsored by our magnificent partner: %s\n", dto->sponsored_by);
}
printf("βœ… Year acquisition completed successfully\n");
free(chunk.memory);
curl_easy_cleanup(curl);
return dto;
}
// Free the YearResponseDTO memory
void freeYearResponseDTO(YearResponseDTO *dto) {
if (dto) {
free(dto->sponsored_by);
free(dto->metadata);
free(dto);
}
}
#ifndef GETFULLYEAR_H
#define GETFULLYEAR_H
#include <stdbool.h>
// Define a struct for the Year Response DTO
typedef struct {
int year;
char *sponsored_by;
char *metadata; // JSON metadata as a string
} YearResponseDTO;
// Define a function prototype for fetching the year
YearResponseDTO *getFullYear(bool isEnterprise);
// Define a function prototype for cleaning up the YearResponseDTO
void freeYearResponseDTO(YearResponseDTO *dto);
#endif // GETFULLYEAR_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment