<commit_before>/*
 *  libcucul      Canvas for ultrafast compositing of Unicode letters
 *  Copyright (c) 2002-2006 <NAME> <<EMAIL>>
 *                All Rights Reserved
 *
 *  $Id$
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the Do What The Fuck You Want To
 *  Public License, Version 2, as published by Sam Hocevar. See
 *  http://sam.zoy.org/wtfpl/COPYING for more details.
 */

/*
 *  This file contains various import functions.
 */

#include "config.h"
#include "common.h"

#if !defined(__KERNEL__)
#   include <stdio.h>
#   include <stdlib.h>
#   include <string.h>
#endif

#include "cucul.h"
#include "cucul_internals.h"

static cucul_canvas_t *import_caca(void const *, unsigned int);
static cucul_canvas_t *import_text(void const *, unsigned int);
static cucul_canvas_t *import_ansi(void const *, unsigned int);

/** \brief Import a buffer into a canvas
 *
 *  This function imports a memory area into an internal libcucul canvas.
 *
 *  Valid values for \c format are:
 *
 *  \li \c "": attempt to autodetect the file format.
 *
 *  \li \c "ansi": import ANSI files.
 *
 *  \li \c "caca": import native libcaca files.
 *
 *  \param buffer A \e libcucul buffer containing the data to be loaded
 *         into a canvas.
 *  \param size The length of the memory area.
 *  \param format A string describing the input format.
 *  \return A libcucul canvas, or NULL in case of error.
 */
cucul_canvas_t * cucul_import_canvas(cucul_buffer_t *b, char const *format)
{
    char const *buf = (char const*)b->data;

    if(b->size == 0 || b->data == NULL)
        return NULL;

    if(!strcasecmp("caca", format))
        return import_caca(b->data, b->size);
    if(!strcasecmp("text", format))
        return import_text(b->data, b->size);
    if(!strcasecmp("ansi", format))
        return import_ansi(b->data, b->size);

    /* Autodetection */
    if(!strcasecmp("", format))
    {
        unsigned int i=0;
        /* if 4 first letters are CACA */
        if(b->size >= 4 &&
            buf[0] == 'C' && buf[1] == 'A' && buf[2] == 'C' && buf[3] != 'A')
            return import_caca(b->data, b->size);

        /* If we find ESC[ argv, we guess it's an ANSI file */
        while(i < b->size - 1)
        {
            if((buf[i] == 0x1b) && (buf[i+1] == '['))
                return import_ansi(b->data, b->size);
            i++;
        }

        /* Otherwise, import it as text */
        return import_text(b->data, b->size);
    }
    return NULL;<commit_msg> * Fixed documentation for import<commit_after>/*
 *  libcucul      Canvas for ultrafast compositing of Unicode letters
 *  Copyright (c) 2002-2006 <NAME> <<EMAIL>>
 *                All Rights Reserved
 *
 *  $Id$
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the Do What The Fuck You Want To
 *  Public License, Version 2, as published by Sam Hocevar. See
 *  http://sam.zoy.org/wtfpl/COPYING for more details.
 */

/*
 *  This file contains various import functions.
 */

#include "config.h"
#include "common.h"

#if !defined(__KERNEL__)
#   include <stdio.h>
#   include <stdlib.h>
#   include <string.h>
#endif

#include "cucul.h"
#include "cucul_internals.h"

static cucul_canvas_t *import_caca(void const *, unsigned int);
static cucul_canvas_t *import_text(void const *, unsigned int);
static cucul_canvas_t *import_ansi(void const *, unsigned int);

/** \brief Import a buffer into a canvas
 *
 *  This function imports a memory area into an internal libcucul canvas.
 *
 *  Valid values for \c format are:
 *
 *  \li \c "": attempt to autodetect the file format.
 *
 *  \li \c "ansi": import ANSI files.
 *
 *  \li \c "caca": import native libcaca files.
 *
 *  \param buffer A \e libcucul buffer containing the data to be loaded
 *         into a canvas.
 *  \param format A string describing the input format.
 *  \return A libcucul canvas, or NULL in case of error.
 */
cucul_canvas_t * cucul_import_canvas(cucul_buffer_t *buffer, char const *format)
{
    char const *buf = (char const*)buffer->data;

    if(buffer->size == 0 || buffer->data == NULL)
        return NULL;

    if(!strcasecmp("caca", format))
        return import_caca(buffer->data, buffer->size);
    if(!strcasecmp("text", format))
        return import_text(buffer->data, buffer->size);
    if(!strcasecmp("ansi", format))
        return import_ansi(buffer->data, buffer->size);

    /* Autodetection */
    if(!strcasecmp("", format))
    {
        unsigned int i=0;
        /* if 4 first letters are CACA */
        if(buffer->size >= 4 &&
            buf[0] == 'C' && buf[1] == 'A' && buf[2] == 'C' && buf[3] != 'A')
            return import_caca(buffer->data, buffer->size);

        /* If we find ESC[ argv, we guess it's an ANSI file */
        while(i < buffer->size - 1)
        {
            if((buf[i] == 0x1b) && (buf[i+1] == '['))
                return import_ansi(buffer->data, buffer->size);
            i++;
        }

        /* Otherwise, import it as text */
        return import_text(buffer->data, buffer->size);
    }
    return NULL;
{'fraction_non_alphanumeric': 0.1371517631015001, 'fraction_numerical': 0.008182349503214495, 'mean_word_length': 2.6180408738548273, 'pattern_counts': {'":': 6, '<': 17, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6046247', 'n_tokens_mistral': 1739, 'n_tokens_neox': 1664, 'n_words': 537}