summaryrefslogtreecommitdiff
path: root/src/upb_string.c
blob: 54df4f1bcdfe33ef2d0e3327f228fb4fa27a7584 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
 * upb - a minimalist implementation of protocol buffers.
 *
 * Copyright (c) 2009 Joshua Haberman.  See LICENSE for details.
 */

#include <stdio.h>
#include "upb_string.h"

struct upb_string *upb_strreadfile(const char *filename) {
  FILE *f = fopen(filename, "rb");
  if(!f) return false;
  if(fseek(f, 0, SEEK_END) != 0) goto error;
  long size = ftell(f);
  if(size < 0) goto error;
  if(fseek(f, 0, SEEK_SET) != 0) goto error;
  struct upb_string *s = upb_string_new();
  upb_string_resize(s, size);
  if(fread(s->ptr, size, 1, f) != 1) goto error;
  fclose(f);
  return s;

error:
  fclose(f);
  return NULL;
}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback