First hacking session with GNU PDF library

4 Comments

In this short session we’ll get running a minimal piece of C code which uses types and functions of the types module, in the base layer of the GNU PDF library. For more information about the GNU PDF library architecture, please go here.

First of all, create a directory to store these little hacking things:

  1. mkdir gnupdfhack
  2. cd gnupdfhack/

Then, create a C test file, and open it with your favorite editor:

  1. touch test.c
  2. emacs test.c

This is a very first approximation to a trivial test unit, which simply:

  • checks the types declared in the library specification, and
  • tries to call some functionalities of the library implementation

The test.c file goes like this:

  1. #include <stdio.h>
  2. #include “../trunk/src/pdf.h”
  3.  
  4. int main ()
  5. {
  6.   printf (“GNU PDF hack test\n”);
  7.  
  8.   pdf_size_t size = 128;
  9.   pdf_error_t *error = NULL;
  10.   pdf_buffer_t *buf = pdf_buffer_new(size, &error);
  11.  
  12.   if (buf == NULL)
  13.     {
  14.       printf(“PDF buffer creation failed\n”);
  15.       /* do some more **error analysis here … */
  16.  
  17.       return 1;
  18.     }
  19.   else
  20.     {
  21.       pdf_buffer_destroy(buf);
  22.       printf(“PDF buffer created and destroyed successfully\n”);
  23.     }
  24.  
  25.   return 0;
  26. }

The type check tries to use the declaration of GNU PDF boolean types, while the library calls essentially allocate space for a buffer, and then they destroy it.

To get it working:

  1. gcc -Wall /usr/local/lib/libgnupdf.so test.c -o test
  2. ./text

This works under the assumption that the GNU PDF library is installed under /usr/local/lib. Please go to this previous post here for a guide on how to install the GNU PDF library in your system. If everything goes well, you’ll get this output:

  1. GNU PDF hack test
  2. —————–
  3. I have the types!
  4. PDF buffer created and destroyed successfully

On further sessions we’ll get more from the actual implementation to make some improvements required on the actual types module. Please feel free to read the base layer interfaces, and happy hacking! Hope this helps!

This entry is filed under C library test, Free Software, GNU PDF, Programming. And tagged with , , . You can follow any responses to this entry through RSS 2.0. You can leave a response, or trackback from your own site.

4 Responses to “First hacking session with GNU PDF library”


  1. Albert

    Post updated, thanks for the comments!

  2. Aleksander

    Some comments to the previous post from gg:

    pdf_error_t **error;
    pdf_buffer_t *buf = pdf_buffer_new(size, error);

    You should be passing the address of an existing (pdf_error_t *) (which should also be initialized to NULL) to pdf_buffer_new(); so something like this:
    pdf_error_t *error = NULL;
    pdf_buffer_t *buf = pdf_buffer_new(size, &error);

    Then:
    if(buf == PDF_FALSE)
    buf is a pointer, so you need to compare with NULL, not with PDF_FALSE.

  3. Albert

    Thanks for the remarks :)

    The if (PDF_TRUE) thing is just to perform a trivial data type checking about including the basic types module.

  4. gg

    the interface changed, so I made some updates to test.c:
    – cut here —————————————————–
    #include
    #include “../trunk/src/pdf.h”
    int main()
    {
    printf(“GNU PDF hack test\n”);

    pdf_size_t size = 128;
    pdf_error_t **error;
    pdf_buffer_t *buf = pdf_buffer_new(size, error);
    if(buf == PDF_FALSE)
    {
    printf(“PDF buffer creation failed\n”);
    /* do some more **error analysis here … */

    return 1;
    }
    else
    {
    pdf_buffer_destroy(buf);
    printf(“PDF buffer created and destroyed successfully\n”);
    }
    return 0;
    }
    – cut here —————————————————–
    at my system it was necessary to set LD_LIBRARY_PATH like this:
    $ LD_LIBRARY_PATH=/usr/local/lib/ ./test

    One question remains: Why do you test with “if(PDF_TRUE)”?