summaryrefslogtreecommitdiff
path: root/readline.cpp
blob: f13852e785c0b508d13ca35f7e120a0e95d8303a (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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
 * readline.cpp: A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id$
 */

#include <stdlib.h>
#include <stdio.h>
#include "readline.h"

// --- cReadLineInfosatepg ---------------------------------------------------

cReadLineInfosatepg::cReadLineInfosatepg(void)
{
  buffer = NULL;
}

cReadLineInfosatepg::~cReadLineInfosatepg()
{
  free(buffer);
}

char *cReadLineInfosatepg::Read(FILE *f,size_t *size)
{
  free(buffer); buffer=NULL;
  if ((!size) || (!f)) return NULL;
  bool ext=false;
  *size=0;
  do
  {
    char *tempbuffer=NULL;
    size_t tempsize=0;

    ext=false;
    int n = getline(&tempbuffer, &tempsize, f);
    if (n > 0)
    {
      if (tempbuffer[n-1] == '\n')
      {
        n--;
        tempbuffer[n] = 0;
        if (n > 0)
        {
          if (tempbuffer[n-1] == '\r')
            n--;
          tempbuffer[n] = 0;
        }
      }
      if (n>0)
      {
        if (tempbuffer[n-1] == '\\')
        {
          n--;
          tempbuffer[n]=0;
          ext=true;
        }
      }

      buffer=(char*) realloc(buffer,*size+(n+1));
      snprintf(&buffer[*size],n+1,tempbuffer);
      free(tempbuffer);
      *size+=n;
    }
  }
  while (ext==true);

  return buffer;
}