rllib  1
rlfileload.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  rlfileload.cpp - description
3  -------------------
4  begin : Fri Jul 28 2006
5  copyright : (C) 2006 by R. Lehrig
6  email : lehrig@t-online.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This library is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as *
13  * published by the Free Software Foundation *
14  * *
15  ***************************************************************************/
16 #include "rlfileload.h"
17 #include <stdio.h>
18 #include <string.h>
19 
21 {
22  loaded = debug = 0;
23  file_lines.line = NULL;
24  file_lines.next = NULL;
25  current_line = NULL;
26 }
27 
29 {
30  unload();
31 }
32 
33 int rlFileLoad::load(const char *filename)
34 {
35  FILE *fin;
36  rlFileLines *fl;
37  char line[rl_PRINTF_LENGTH], *cptr;
38 
39  unload();
40  fin = fopen(filename,"r");
41  if(fin == NULL) return -1;
42 
43  fl = &file_lines;
44  while(fgets(line,sizeof(line)-1,fin) != NULL)
45  {
46  cptr = strchr(line,'\n');
47  if(cptr != NULL) *cptr = '\0';
48  cptr = strchr(line,0x0D);
49  if(cptr != NULL) *cptr = '\0';
50  if(debug) printf("rlFileLoad::load line=%s\n",line);
51  fl->next = new rlFileLines;
52  fl = fl->next;
53  fl->line = new char [strlen(line)+1];
54  strcpy(fl->line,line);
55  fl->next = NULL;
56  }
57 
58  fclose(fin);
59  loaded = 1;
60  return 1;
61 }
62 
64 {
65  rlFileLines *fl,*flold;
66 
67  if(loaded == 0) return;
68  fl = &file_lines;
69  fl = fl->next;
70  while(fl != NULL)
71  {
72  if(debug) printf("rlFileLoad::unload line=%s",fl->line);
73  delete [] fl->line;
74  flold = fl;
75  fl = fl->next;
76  delete flold;
77  }
78  loaded = 0;
79 }
80 
81 const char *rlFileLoad::firstLine()
82 {
83  if(loaded == 0) return NULL;
86  if(current_line == NULL) return NULL;
87  if(debug) printf("rlFileLoad::firstLine=%s",current_line->line);
88  return current_line->line;
89 }
90 
91 const char *rlFileLoad::nextLine()
92 {
93  if(loaded == 0) return NULL;
95  if(current_line == NULL) return NULL;
96  if(debug) printf("rlFileLoad::nextLine=%s",current_line->line);
97  return current_line->line;
98 }
99 
100 void rlFileLoad::setDebug(int state)
101 {
102  if(state == 0) debug = 0;
103  else debug = 1;
104 }
105 
107 {
108  rlstring = "";
109  if(loaded == 0) return -1;
110  const char *line = firstLine();
111  while(line != NULL)
112  {
113  rlstring += line;
114  rlstring += "\n";
115  line = nextLine();
116  }
117  return 0;
118 }
119 
virtual ~rlFileLoad()
Definition: rlfileload.cpp:28
void unload()
Definition: rlfileload.cpp:63
rlFileLines * current_line
Definition: rlfileload.h:47
char * line
Definition: rlfileload.h:24
rlFileLines file_lines
Definition: rlfileload.h:46
int text2rlstring(rlString &rlstring)
Definition: rlfileload.cpp:106
#define rl_PRINTF_LENGTH
Definition: rldefine.h:71
int load(const char *filename)
Definition: rlfileload.cpp:33
int loaded
Definition: rlfileload.h:44
const char * nextLine()
Definition: rlfileload.cpp:91
const char * firstLine()
Definition: rlfileload.cpp:81
void setDebug(int state)
Definition: rlfileload.cpp:100
struct _rlFileLines_ * next
Definition: rlfileload.h:25
struct _rlFileLines_ rlFileLines