types.c 2.47 KB
Newer Older
Moritz Lipp's avatar
Moritz Lipp committed
1 2
/* See LICENSE file for license and copyright information */

3
#include <stdlib.h>
4
#include <girara/datastructures.h>
5
#include <glib.h>
6

Moritz Lipp's avatar
Moritz Lipp committed
7
#include "types.h"
Moritz Lipp's avatar
Moritz Lipp committed
8
#include "links.h"
9
#include "internal.h"
10
#include "checked-integer-arithmetic.h"
Moritz Lipp's avatar
Moritz Lipp committed
11

12 13 14 15 16 17 18
zathura_index_element_t*
zathura_index_element_new(const char* title)
{
  if (title == NULL) {
    return NULL;
  }

19 20 21 22
  zathura_index_element_t* res = g_try_malloc0(sizeof(zathura_index_element_t));
  if (res == NULL) {
    return NULL;
  }
23 24 25 26 27 28 29 30 31 32 33 34 35 36

  res->title = g_strdup(title);

  return res;
}

void
zathura_index_element_free(zathura_index_element_t* index)
{
  if (index == NULL) {
    return;
  }

  g_free(index->title);
Moritz Lipp's avatar
Moritz Lipp committed
37
  zathura_link_free(index->link);
38 39 40 41 42 43
  g_free(index);
}

zathura_image_buffer_t*
zathura_image_buffer_create(unsigned int width, unsigned int height)
{
44 45 46 47
  g_return_val_if_fail(width != 0, NULL);
  g_return_val_if_fail(height != 0, NULL);

  unsigned int size = 0;
Sebastian Ramacher's avatar
Sebastian Ramacher committed
48 49
  if (checked_umul(width, height, &size) == true ||
      checked_umul(size, 3, &size) == true) {
50 51 52
    return NULL;
  }

53 54 55 56 57 58
  zathura_image_buffer_t* image_buffer = malloc(sizeof(zathura_image_buffer_t));

  if (image_buffer == NULL) {
    return NULL;
  }

59
  image_buffer->data = calloc(size, sizeof(unsigned char));
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

  if (image_buffer->data == NULL) {
    free(image_buffer);
    return NULL;
  }

  image_buffer->width     = width;
  image_buffer->height    = height;
  image_buffer->rowstride = width * 3;

  return image_buffer;
}

void
zathura_image_buffer_free(zathura_image_buffer_t* image_buffer)
{
  if (image_buffer == NULL) {
    return;
  }

  free(image_buffer->data);
  free(image_buffer);
}

84
girara_list_t*
85
zathura_document_information_entry_list_new(void)
86 87
{
  girara_list_t* list = girara_list_new2((girara_free_function_t)
Moritz Lipp's avatar
Moritz Lipp committed
88
                                         zathura_document_information_entry_free);
89 90 91 92 93 94

  return list;
}

zathura_document_information_entry_t*
zathura_document_information_entry_new(zathura_document_information_type_t type,
Moritz Lipp's avatar
Moritz Lipp committed
95
                                       const char* value)
96 97 98 99 100 101
{
  if (value == NULL) {
    return NULL;
  }

  zathura_document_information_entry_t* entry =
102 103 104 105
    g_try_malloc0(sizeof(zathura_document_information_entry_t));
  if (entry == NULL) {
    return NULL;
  }
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

  entry->type  = type;
  entry->value = g_strdup(value);

  return entry;
}

void
zathura_document_information_entry_free(zathura_document_information_entry_t* entry)
{
  if (entry == NULL) {
    return;
  }

  if (entry->value != NULL) {
    g_free(entry->value);
  }

  g_free(entry);
}