bibtex, a single-header BibTeX file parser written in C.

repo

The bibliographic arts are incredibly important for organizing research and linking together texts. The bibliography database (.bib) format used by BibTeX is a simple, text-based system for storing bibliographies.

Most bibliography database parsers are incredibly heavyweight and this makes them clumsy to include in new and larger projects.

bibtex.h aims to be a dead-simple, performant bib file parser. Internally it uses a small memory arena to store string table values, but allocates no memory. Users wishing to add more functionality on top can easily and performantly do so using this base layer.

Example showing how to use bibtex.h to read a bibliography database and iterate over each entry print out its fields:

#include 

#define BIBTEX_INCLUDE_IMPLEMENTATION
#include "bibtex.h"

int
main(int argc, char** argv)
{
  Bibtex_Entry *entry;
  Bibtex_Reader reader;
  Bibtex_Result *result;

  result = bibtex_reader_load_file(&reader, argv[1]);
  if (result->error)
  {
    fprintf(stderr, "error: %s\n", result->message);
    return(1);
  }

  while (bibtex_reader_is_valid(&reader))
  {
    result = bibtex_reader_next(&reader, &entry);
    if (result->error)
    {
      fprintf(stderr, "error: %s\n", result->message);
      return(1);
    }

    if (entry != NULL)
    {
      printf("ENTRY %.*s %.*s\n",
	     next_entry->type.length,
	     next_entry->type.data,
	     next_entry->citation_key.length,
	     next_entry->citation_key.data);

      for (uint32_t i = 0; i < next_entry->num_fields; ++i)
      {
	printf("\t%.*s = ", next_entry->fields[i].key.length, next_entry->fields[i].key.data);
	switch (next_entry->fields[i].type)
	{
	case BFT_STRING:
	  printf("{%.*s}\n", next_entry->fields[i].value.length, next_entry->fields[i].value.data);
	  break;
	case BFT_INTEGER:
	  printf("%.*s\n", next_entry->fields[i].value.length, next_entry->fields[i].value.data);
	  break;
	default:
	  printf("\n");
	  break;
	}
      }
    }
  }

  bibtex_reader_destroy(&reader);

  return(0);
}

incoming(1): software tools