summaryrefslogtreecommitdiff
path: root/readline.cpp
blob: 50d2b2bba8e11d6161b43a88ace0f13b432eb9c6 (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
71
72
/*
 * 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"

extern char *strcatrealloc(char *dest, const char *src);

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

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

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

char *cReadLineInfosatepg::Read(FILE *f,size_t *size)
{
  if (buffer) free(buffer);
   buffer=NULL;
  if ((!size) || (!f)) return NULL;
  bool ext=false;
  *size=0;
  char *tempbuffer=NULL;
  size_t tempsize=0;
  do
  {
    ext=false;
    int n = getline(&tempbuffer, &tempsize, f);
    if (n > 0)
    {
      if (tempbuffer[n-1] == '\n')
      {
        tempbuffer[--n] = 0;
        if (n > 0)
        {
          if (tempbuffer[n-1] == '\r')
             tempbuffer[--n] = 0;
        }
      }
      if (n>0)
      {
        if (tempbuffer[n-1] == '\\')
        {
          tempbuffer[--n]=0;
          ext=true;
        }
      }
      if (n>0) {
         buffer=strcatrealloc(buffer,tempbuffer);
         //tempbuffer[0]=0;
         *size+=n;
      } else {
         //tempbuffer[0]=0;
         ext=true;
      }
    }
  }
  while (ext==true);
  if (tempbuffer) free(tempbuffer);
  return buffer;
}