Comunidad Gambas-es
Creando bibliotecas compartidas en C - Versión para impresión

+- Comunidad Gambas-es (https://gambas-es.org)
+-- Foro: Linux (https://gambas-es.org/forum-9.html)
+--- Foro: Programación en otros lenguajes (https://gambas-es.org/forum-12.html)
+---- Foro: C/C++ (https://gambas-es.org/forum-15.html)
+---- Tema: Creando bibliotecas compartidas en C (/thread-1789.html)



Creando bibliotecas compartidas en C - tincho - 12-09-2024

Hola.
Hace un momento hice una prueba, le pedi a ChatGPT que me escribiera una biblioteca compartida en C que a su vez usa la librería LibreDWG y el resultado fue:
Código:
#include <stdio.h>
#include <stdlib.h>
#include <libredwg.h>

const char* list_blocks(const char* filename) {
static char result[4096];
DWG_Data *dwg;
int i;

// Inicializa la biblioteca
dwg = dwg_read(filename);
if (!dwg) {
return "Error reading DWG file.";
}

// Recorre los bloques y crea una lista
result[0] = '\0';
for (i = 0; i < dwg->num_blocks; i++) {
strcat(result, dwg->blocks[i].name);
strcat(result, "\n");
}

// Limpia
dwg_free(dwg);

return result;
}

Por supuesto no funciono, por un lado en mi sistema no existe libredwg.h, pero se puede descargar el código fuente:
https://github.com/LibreDWG/libredwg
A ver si algún experto en C tiene ganas de experimentar un poco con esta herramienta.

Luego en el codigo fuente se puede ver un ejemlo que lista, en lugar de los "Blocks", los "Layers"

Código:
/*****************************************************************************/
/*  LibreDWG - free implementation of the DWG file format                    */
/*                                                                           */
/*  Copyright (C) 2018-2019 Free Software Foundation, Inc.                   */
/*                                                                           */
/*  This library is free software, licensed under the terms of the GNU       */
/*  General Public License as published by the Free Software Foundation,     */
/*  either version 3 of the License, or (at your option) any later version.  */
/*  You should have received a copy of the GNU General Public License        */
/*  along with this program.  If not, see <http://www.gnu.org/licenses/>.    */
/*****************************************************************************/

/*
 * dwglayers.c: print list of layers in a DWG
 *
 * written by Reini Urban
 */

#include "../src/config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <getopt.h>
#ifdef HAVE_VALGRIND_VALGRIND_H
#  include <valgrind/valgrind.h>
#endif

#include <dwg.h>
#include "common.h"
#include "bits.h"
#include "logging.h"

static int
usage (void)
{
  printf ("\nUsage: dwglayers [-f|--flags] [--on] <input_file.dwg>\n");
  return 1;
}
static int
opt_version (void)
{
  printf ("dwglayers %s\n", PACKAGE_VERSION);
  return 0;
}
static int
help (void)
{
  printf ("\nUsage: dwglayers [OPTION]... DWGFILE\n");
  printf ("Print list of layers.\n"
          "\n");
#ifdef HAVE_GETOPT_LONG
  printf ("  -x, --extnames            prints EXTNAMES (r13-r14 only)\n"
          "                i.e. space instead of _\n");
  printf ("  -f, --flags               prints also flags:\n"
          "                3 chars for: f for frozen, + or - for ON or OFF, l "
          "for locked\n");
  printf ("  -o, --on                  prints only ON layers\n");
  printf ("  -h, --help                display this help and exit\n");
  printf ("      --version             output version information and exit\n"
          "\n");
#else
  printf ("  -x            prints EXTNAMES (r13-r14 only)\n"
          "                i.e. space instead of _\n");
  printf ("  -f            prints also flags:\n"
          "                3 chars for: f for frozen, + or - for ON or OFF, l "
          "for locked\n");
  printf ("  -o            prints only ON layers\n");
  printf ("  -h            display this help and exit\n");
  printf ("  -i            output version information and exit\n"
          "\n");
#endif
  printf ("GNU LibreDWG online manual: "
          "<https://www.gnu.org/software/libredwg/>\n");
  return 0;
}

int
main (int argc, char *argv[])
{
  int error;
  long i = 1;
  int flags = 0, on = 0, extnames = 0;
  char *filename_in;
  Dwg_Data dwg;
  Dwg_Object *obj;
  Dwg_Object_LAYER *layer;
  Dwg_Object_LAYER_CONTROL *_ctrl;
  int c;
#ifdef HAVE_GETOPT_LONG
  int option_index = 0;
  static struct option long_options[] = { { "flags", 0, 0, 'f' },
                                          { "extnames", 0, 0, 'x' },
                                          { "on", 0, 0, 'o' },
                                          { "help", 0, 0, 0 },
                                          { "version", 0, 0, 0 },
                                          { NULL, 0, NULL, 0 } };
#endif

  while
#ifdef HAVE_GETOPT_LONG
      ((c = getopt_long (argc, argv, "xfoh", long_options, &option_index))
       != -1)
#else
      ((c = getopt (argc, argv, "xfohi")) != -1)
#endif
    {
      if (c == -1)
        break;
      switch (c)
        {
#ifdef HAVE_GETOPT_LONG
        case 0:
          if (!strcmp (long_options[option_index].name, "version"))
            return opt_version ();
          if (!strcmp (long_options[option_index].name, "help"))
            return help ();
          break;
#else
        case 'i':
          return opt_version ();
#endif
        case 'x':
          extnames = 1;
          break;
        case 'f':
          flags = 1;
          break;
        case 'o':
          on = 1;
          break;
        case 'h':
          return help ();
        case '?':
          fprintf (stderr, "%s: invalid option '-%c' ignored\n", argv[0],
                   optopt);
          break;
        default:
          return usage ();
        }
    }
  i = optind;
  if (i >= argc)
    return usage ();

  filename_in = argv[i];
  memset (&dwg, 0, sizeof (Dwg_Data));
  error = dwg_read_file (filename_in, &dwg);
  if (error >= DWG_ERR_CRITICAL)
    fprintf (stderr, "READ ERROR %s: 0x%x\n", filename_in, error);

  obj = dwg_get_first_object (&dwg, DWG_TYPE_LAYER_CONTROL);
  _ctrl = obj->tio.object->tio.LAYER_CONTROL;
  for (i = 0; i < _ctrl->num_entries; i++)
    {
      char *name;
      assert (_ctrl->entries);
      assert (_ctrl->entries[i]);
      obj = _ctrl->entries[i]->obj;
      if (!obj || obj->type != DWG_TYPE_LAYER) // can be DICTIONARY also
        continue;
      assert (_ctrl->entries[i]->obj->tio.object);
      layer = _ctrl->entries[i]->obj->tio.object->tio.LAYER;
      if (on && (!layer->on || layer->frozen))
        continue;
      if (flags)
        printf ("%s%s%s\t", layer->frozen ? "f" : " ", layer->on ? "+" : "-",
                layer->locked ? "l" : " ");
      if (extnames && dwg.header.from_version >= R_13 && dwg.header.from_version < R_2000)
        {
          if (!(name = dwg_find_table_extname (&dwg, obj)))
            name = layer->name;
        }
      else
        name = layer->name;
      // since r2007 unicode, converted to utf-8
      if (dwg.header.from_version >= R_2007)
        {
          char *utf8 = bit_convert_TU ((BITCODE_TU)name);
          printf ("%s\n", utf8);
          free (utf8);
        }
      else
        printf ("%s\n", name);
      fflush (stdout);
    }

  // forget about valgrind. really huge DWG's need endlessly here.
  if ((dwg.header.version && dwg.num_objects < 1000)
#if defined __SANITIZE_ADDRESS__ || __has_feature(address_sanitizer)
      || 1
#endif
#ifdef HAVE_VALGRIND_VALGRIND_H
      || (RUNNING_ON_VALGRIND)
#endif
  )
    dwg_free (&dwg);
  return error >= DWG_ERR_CRITICAL ? 1 : 0;
}