#include <iostream>
#include <curl/curl.h>
#include <string>
#include <xlsxwriter.h>
// Callback function to write fetched data into a string
size_t write_callback(void *contents, size_t size, size_t nmemb, std::string *data) {
    size_t total_size = size * nmemb;
    data->append((char*)contents, total_size);
    return total_size;
}
int main() {
    // Initialize libcurl
    CURL *curl;
    CURLcode res;
    curl_global_init(CURL_GLOBAL_DEFAULT);
    curl = curl_easy_init();
    if (curl) {
        std::string data; // String to store fetched data
        // Set URL to fetch data from
        curl_easy_setopt(curl, CURLOPT_URL, "http://e...content-available-to-author-only...e.com/data.txt");
        // Set callback function to write fetched data into 'data' string
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
        // Perform the request
        res = curl_easy_perform(curl);
        // Check for errors
        if (res != CURLE_OK) {
            std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
        }
        // Clean up
        curl_easy_cleanup(curl);
        curl_global_cleanup();
        // Write fetched data into an Excel file using Libxlsxwriter
        lxw_workbook  *workbook  = workbook_new("output.xlsx");
        lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
        // Split the fetched data into rows and columns and write it to the Excel file
        size_t row = 0;
        size_t col = 0;
        for (char c : data) {
            if (c == '\n') {
                row++;
                col = 0;
            } else if (c == ',') {
                col++;
            } else {
                worksheet_write_string(worksheet, row, col, &c, NULL);
            }
        }
        // Close the workbook
        workbook_close(workbook);
    } else {
        std::cerr << "Failed to initialize libcurl!" << std::endl;
    }
    return 0;
}