~fta/+junk/mediadb

« back to all changes in this revision

Viewing changes to fio.c

  • Committer: Fabien Tassin
  • Date: 2008-08-14 19:36:43 UTC
  • Revision ID: fta@sofaraway.org-20080814193643-pop12h3olx7azk41
* Initial revision, just the C code leading to identcd. The full PHP mediadb
  is not included, nor are all the data test files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* fio.c: File IO */
 
2
/* Copyright (c) 2000-2001 Fabien Tassin <fta@sofaraway.org> */
 
3
 
 
4
#ifdef MALLOC
 
5
# include <dmalloc.h>
 
6
#endif
 
7
#include <sys/time.h>
 
8
#include <sys/types.h>
 
9
#include <malloc.h>
 
10
#include <string.h>
 
11
#include <unistd.h>
 
12
#include <stdio.h>
 
13
#include "fio.h"
 
14
 
 
15
FIO *FIO_init(int fh) {
 
16
  FIO *fio;
 
17
 
 
18
  fio = (FIO *) malloc(sizeof(struct _FIO));
 
19
  fio->fh = fh;
 
20
  fio->size = FIO_BUFFER;
 
21
  fio->buffer = (char *) malloc(fio->size);
 
22
  fio->start = fio->end = fio->buffer;
 
23
  fio->status = FIO_OK;
 
24
  fio->debug = 0;
 
25
  return(fio);
 
26
}
 
27
 
 
28
void FIO_free(FIO *fio) {
 
29
  free(fio->buffer);
 
30
  free(fio);
 
31
}
 
32
 
 
33
/* read a line in the given filehandle. Remove trailing '\n' or '\r\n' */
 
34
char *FIO_readline(FIO *fio) {
 
35
  char *p, *q, *old;
 
36
  ssize_t sz;
 
37
 
 
38
  while (1) {
 
39
    if (fio->end > fio->start) {
 
40
      /* the buffer is not empty. Look for the first newline */
 
41
      p = memchr(fio->start, '\n', fio->end - fio->start);
 
42
      if (p != NULL) {
 
43
        /* found */
 
44
        int f = 0;
 
45
        if (p > fio->start && *(p - 1) == '\r')
 
46
          f++, p--;
 
47
        *p = '\0';
 
48
        old = fio->start;
 
49
        fio->start = p + (f ? 2 : 1);
 
50
        fio->status = FIO_OK;
 
51
        if (fio->debug > 0)
 
52
          fprintf(stderr, "<<< \"%s%s\\n\"\n", old, f ? "\\r" : "");
 
53
        return(old);
 
54
      }
 
55
      if (fio->start != fio->buffer) {
 
56
        /* move unused data at the beginning of the buffer */
 
57
        for (p = fio->buffer, q = fio->start; q < fio->end; )
 
58
          *p++ = *q++;
 
59
        fio->start = fio->buffer;
 
60
        fio->end = p;
 
61
      }
 
62
      p = fio->end;
 
63
    }
 
64
    else
 
65
      p = fio->buffer;
 
66
 
 
67
    fio->status = FIO_OK;
 
68
    /* read new data from the filehandle */
 
69
    sz = read(fio->fh, p, fio->size - (fio->end - fio->start));
 
70
    if (sz < 0) {
 
71
      perror("read() failed");
 
72
      fio->status = FIO_ERROR;
 
73
      return(NULL);
 
74
    }
 
75
    if (sz == 0)
 
76
      return(NULL); /* nothing to read */
 
77
 
 
78
    fio->start = fio->buffer;
 
79
    fio->end = &p[sz];
 
80
 
 
81
    /* Look for the first newline */
 
82
    p = memchr(fio->start, '\n', fio->end - fio->start);
 
83
    if (p != NULL) {
 
84
      /* found */
 
85
      int f = 0;
 
86
      if (p > fio->start && *(p - 1) == '\r')
 
87
        f++, p--;
 
88
      *p = '\0';
 
89
      old = fio->start;
 
90
      fio->start = p + (f ? 2 : 1);
 
91
      fio->status = FIO_OK;
 
92
      if (fio->debug > 0)
 
93
        fprintf(stderr, "<<< \"%s%s\\n\"\n", old, f ? "\\r" : "");
 
94
      return(old);
 
95
    }
 
96
    if ((fio->end - fio->start) >= fio->size) {
 
97
      /* not found but the buffer is full. This line is too long */
 
98
      fio->status = FIO_TOOLONG;
 
99
      fio->start = fio->end;
 
100
      return(NULL);
 
101
    }
 
102
    /* loop */
 
103
  }
 
104
}
 
105
 
 
106
/* write the buffer into the filehandle (no '\r\n' added) */
 
107
int FIO_write(FIO *fio, char* buf) {
 
108
  int len = strlen(buf);
 
109
  if (fio->debug)
 
110
    fprintf(stderr, ">>> '%s'\n", buf);
 
111
  return(write(fio->fh, buf, len) == len);
 
112
}
 
113
 
 
114
/* write the buffer into the filehandle ('\r\n' added) */
 
115
int FIO_writeline(FIO *fio, char* buf) {
 
116
  int r, old;
 
117
  char *s;
 
118
 
 
119
  s = (char *) malloc(strlen(buf) + 2);
 
120
  strcpy(s, buf);
 
121
  if (fio->debug)
 
122
    fprintf(stderr, ">>> \"%s\\r\\n\"\n", buf);
 
123
  old = fio->debug;
 
124
  fio->debug = 0;
 
125
  r = FIO_write(fio, strcat(s, "\r\n"));
 
126
  fio->debug = old;
 
127
  free(s);
 
128
  return(r);
 
129
}