<commit_before>/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* gpio.c - Core IO library functions

   Modifications:
   Copyright (C) 1999 <NAME> <<EMAIL>>

   The GPIO Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The GPIO Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the GPIO Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
 */

#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "../include/gphoto2-port.h"
#include "library.h"

#ifdef ENABLE_NLS
#  include <libintl.h>
#  undef _
#  define _(String) dgettext (PACKAGE, String)
#  ifdef gettext_noop
#      define N_(String) gettext_noop (String)
#  else
#      define N_(String) (String)
#  endif
#else
#  define _(String) (String)
#  define N_(String) (String)
#endif

gp_port_info     device_list[256];
int              device_count;

int initialized = 0;

/* Toggle to turn on/off debugging */
int              glob_debug_level=0;

void gp_port_debug_printf (int target_debug_level, int debug_level, char *format, ...)
{
        va_list arg;
        if ((debug_level > 0)&&(debug_level >= target_debug_level)) {

                fprintf(stderr, "gp_port: ");
                va_start(arg, format);
                vfprintf(stderr, format, arg);
                va_end(arg);
                fprintf(stderr, "\n");
        }
}

#define GP_ERR_RES(num,str) \
    if (result == num) \
    return (N_(str));

char *gp_port_result_as_string (int result) {

    /* gp_port error range is -1 .. -99 */

    if ((result < -99) || (result > 0))
        return (N_("Unknown error"));

    GP_ERR_RES( 0,  "No error");
    GP_ERR_RES(-1,  "Unspecified error");
    GP_ERR_RES(-2,  "There was a timeout reading from or writing to the port");
    GP_ERR_RES(-3,  "The serial port is not supported");
    GP_ERR_RES(-4,  "The USB port is not supported");
    GP_ERR_RES(-5,  "The parallel port is not supported");
    GP_ERR_RES(-6,  "The network port is not supported");
    GP_ERR_RES(-7,  "The IEEE1394 port is not supported");
    GP_ERR_RES(-8,  "Unknown port");
    GP_ERR_RES(-9,  "Out of memory");
    GP_ERR_RES(-10, "Error loading a required library");
    GP_ERR_RES(-11, "Error initializing the port");
    GP_ERR_RES(-12, "Error opening the port");
    GP_ERR_RES(-13, "Timeout reading from or writing to the port");
    GP_ERR_RES(-14, "Error reading from the port");
    GP_ERR_RES(-15, "Error writing to the port");
    GP_ERR_RES(-16, "Error closing the port");
    GP_ERR_RES(-17, "Error updating the port settings");
    GP_ERR_RES(-18, "Error with the port");
    GP_ERR_RES(-19, "Error setting the serial port speed");
    GP_ERR_RES(-20, "Error sending a break to the serial port");
    GP_ERR_RES(-21, "Error flushing the serial line");
    GP_ERR_RES(-22, "Error clearing a halt condition on the USB port");
    GP_ERR_RES(-23, "Could not find the requested device on the USB port");
    GP_ERR_RES(-24, "Could not lock the device");

    return (N_("Unknown error"));
}

#undef GP_ERR_RES

/*
   Required library functions
   ----------------------------------------------------------------
 */

int gp_port_init(int debug)
{
        gp_port_debug_printf(GP_DEBUG_LOW, glob_debug_level, "Initializing...");
        /* Enumerate all the available devices */
        device_count = 0;
        glob_debug_level = debug;
	initialized = 1;
        return (gp_port_library_list(device_list, &device_count));
}

int gp_port_count_get(void)
{
        gp_port_debug_printf(GP_DEBUG_LOW, glob_debug_level, "Device count: %i", device_count);
	if (!initialized)
		gp_port_init (glob_debug_level);

        return device_count;
}

int gp_port_info_get(int device_number, gp_port_info *info)
{
        gp_port_debug_printf(GP_DEBUG_LOW, glob_debug_level, "Getting device info...");

        memcpy(info, &device_list[device_number], sizeof(device_list[device_number]));

        return GP_OK;
}

int
gp_port_new (gp_port **dev, gp_port_type type)
{
        gp_port_settings settings;
        char buf[1024];

        gp_port_debug_printf(GP_DEBUG_LOW, glob_debug_level, "Creating new device... ");

        switch(type) {
        case GP_PORT_SERIAL:
#ifndef GP_PORT_SUPPORTED_SERIAL
            return GP_ERROR_IO_SUPPORTED_SERIAL;
#endif
            break;
        case GP_PORT_USB:
#ifndef GP_PORT_SUPPORTED_USB
            return GP_ERROR_IO_SUPPORTED_USB;
#endif
            break;
        case GP_PORT_PARALLEL:
#ifndef GP_PORT_SUPPORTED_PARALLEL
            return GP_ERROR_IO_SUPPORTED_PARALLEL;
#endif
            break;
        case GP_PORT_NETWORK:
#ifndef GP_PORT_SUPPORTED_NETWORK
            return GP_ERROR_IO_SUPPORTED_NETWORK;
#endif
            break;
        case GP_PORT_IEEE1394:
#ifndef GP_PORT_SUPPORTED_IEEE1394
            return GP_ERROR_IO_SUPPORTED_IEEE1394;
#endif<commit_msg>One additional check if libgphoto2_port has been initialized<commit_after>/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* gpio.c - Core IO library functions

   Modifications:
   Copyright (C) 1999 <NAME> <<EMAIL>>

   The GPIO Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The GPIO Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the GPIO Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
 */

#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "../include/gphoto2-port.h"
#include "library.h"

#ifdef ENABLE_NLS
#  include <libintl.h>
#  undef _
#  define _(String) dgettext (PACKAGE, String)
#  ifdef gettext_noop
#      define N_(String) gettext_noop (String)
#  else
#      define N_(String) (String)
#  endif
#else
#  define _(String) (String)
#  define N_(String) (String)
#endif

gp_port_info     device_list[256];
int              device_count;

int initialized = 0;

/* Toggle to turn on/off debugging */
int              glob_debug_level=0;

void gp_port_debug_printf (int target_debug_level, int debug_level, char *format, ...)
{
        va_list arg;
        if ((debug_level > 0)&&(debug_level >= target_debug_level)) {

                fprintf(stderr, "gp_port: ");
                va_start(arg, format);
                vfprintf(stderr, format, arg);
                va_end(arg);
                fprintf(stderr, "\n");
        }
}

#define GP_ERR_RES(num,str) \
    if (result == num) \
    return (N_(str));

char *gp_port_result_as_string (int result) {

    /* gp_port error range is -1 .. -99 */

    if ((result < -99) || (result > 0))
        return (N_("Unknown error"));

    GP_ERR_RES( 0,  "No error");
    GP_ERR_RES(-1,  "Unspecified error");
    GP_ERR_RES(-2,  "There was a timeout reading from or writing to the port");
    GP_ERR_RES(-3,  "The serial port is not supported");
    GP_ERR_RES(-4,  "The USB port is not supported");
    GP_ERR_RES(-5,  "The parallel port is not supported");
    GP_ERR_RES(-6,  "The network port is not supported");
    GP_ERR_RES(-7,  "The IEEE1394 port is not supported");
    GP_ERR_RES(-8,  "Unknown port");
    GP_ERR_RES(-9,  "Out of memory");
    GP_ERR_RES(-10, "Error loading a required library");
    GP_ERR_RES(-11, "Error initializing the port");
    GP_ERR_RES(-12, "Error opening the port");
    GP_ERR_RES(-13, "Timeout reading from or writing to the port");
    GP_ERR_RES(-14, "Error reading from the port");
    GP_ERR_RES(-15, "Error writing to the port");
    GP_ERR_RES(-16, "Error closing the port");
    GP_ERR_RES(-17, "Error updating the port settings");
    GP_ERR_RES(-18, "Error with the port");
    GP_ERR_RES(-19, "Error setting the serial port speed");
    GP_ERR_RES(-20, "Error sending a break to the serial port");
    GP_ERR_RES(-21, "Error flushing the serial line");
    GP_ERR_RES(-22, "Error clearing a halt condition on the USB port");
    GP_ERR_RES(-23, "Could not find the requested device on the USB port");
    GP_ERR_RES(-24, "Could not lock the device");

    return (N_("Unknown error"));
}

#undef GP_ERR_RES

/*
   Required library functions
   ----------------------------------------------------------------
 */

int gp_port_init(int debug)
{
        gp_port_debug_printf(GP_DEBUG_LOW, glob_debug_level, "Initializing...");
        /* Enumerate all the available devices */
        device_count = 0;
        glob_debug_level = debug;
	initialized = 1;
        return (gp_port_library_list(device_list, &device_count));
}

int gp_port_count_get(void)
{
        gp_port_debug_printf(GP_DEBUG_LOW, glob_debug_level, "Device count: %i", device_count);
	if (!initialized)
		gp_port_init (glob_debug_level);

        return device_count;
}

int gp_port_info_get(int device_number, gp_port_info *info)
{
        gp_port_debug_printf(GP_DEBUG_LOW, glob_debug_level, "Getting device info...");

        memcpy(info, &device_list[device_number], sizeof(device_list[device_number]));

        return GP_OK;
}

int
gp_port_new (gp_port **dev, gp_port_type type)
{
        gp_port_settings settings;
        char buf[1024];

	if (!initialized)
		gp_port_init (glob_debug_level);

        gp_port_debug_printf(GP_DEBUG_LOW, glob_debug_level, "Creating new device... ");

        switch(type) {
        case GP_PORT_SERIAL:
#ifndef GP_PORT_SUPPORTED_SERIAL
            return GP_ERROR_IO_SUPPORTED_SERIAL;
#endif
            break;
        case GP_PORT_USB:
#ifndef GP_PORT_SUPPORTED_USB
            return GP_ERROR_IO_SUPPORTED_USB;
#endif
            break;
        case GP_PORT_PARALLEL:
#ifndef GP_PORT_SUPPORTED_PARALLEL
            return GP_ERROR_IO_SUPPORTED_PARALLEL;
#endif
            break;
        case GP_PORT_NETWORK:
#ifndef GP_PORT_SUPPORTED_NETWORK
            return GP_ERROR_IO_SUPPORTED_NETWORK;
#endif
            break;
        case GP_PORT_IEEE1394:
#ifndef GP_PORT_SUPPORTED_IEEE1394
            return GP_ERROR_IO_SUPPORTED_IEEE1394;
#endif
{'fraction_non_alphanumeric': 0.11323851203501094, 'fraction_numerical': 0.017596644784828593, 'mean_word_length': 2.9916302765647744, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 25, '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': '1776063', 'n_tokens_mistral': 3671, 'n_tokens_neox': 3481, 'n_words': 1054}