rllib
1
lib
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
20
rlFileLoad::rlFileLoad
()
21
{
22
loaded
=
debug
= 0;
23
file_lines
.
line
= NULL;
24
file_lines
.
next
= NULL;
25
current_line
= NULL;
26
}
27
28
rlFileLoad::~rlFileLoad
()
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
63
void
rlFileLoad::unload
()
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;
84
current_line
= &
file_lines
;
85
current_line
=
current_line
->
next
;
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;
94
if
(
current_line
!= NULL)
current_line
=
current_line
->
next
;
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
106
int
rlFileLoad::text2rlstring
(
rlString
&rlstring)
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
rlFileLoad::~rlFileLoad
virtual ~rlFileLoad()
Definition:
rlfileload.cpp:28
rlFileLoad::unload
void unload()
Definition:
rlfileload.cpp:63
rlFileLoad::current_line
rlFileLines * current_line
Definition:
rlfileload.h:47
_rlFileLines_::line
char * line
Definition:
rlfileload.h:24
rlFileLoad::file_lines
rlFileLines file_lines
Definition:
rlfileload.h:46
rlFileLoad::text2rlstring
int text2rlstring(rlString &rlstring)
Definition:
rlfileload.cpp:106
rl_PRINTF_LENGTH
#define rl_PRINTF_LENGTH
Definition:
rldefine.h:71
rlFileLoad::load
int load(const char *filename)
Definition:
rlfileload.cpp:33
rlfileload.h
rlFileLoad::loaded
int loaded
Definition:
rlfileload.h:44
_rlFileLines_
Definition:
rlfileload.h:22
rlFileLoad::nextLine
const char * nextLine()
Definition:
rlfileload.cpp:91
rlString
Definition:
rlstring.h:27
rlFileLoad::firstLine
const char * firstLine()
Definition:
rlfileload.cpp:81
rlFileLoad::setDebug
void setDebug(int state)
Definition:
rlfileload.cpp:100
rlFileLoad::rlFileLoad
rlFileLoad()
Definition:
rlfileload.cpp:20
rlFileLoad::debug
int debug
Definition:
rlfileload.h:45
_rlFileLines_::next
struct _rlFileLines_ * next
Definition:
rlfileload.h:25
rlFileLines
struct _rlFileLines_ rlFileLines
Generated by
1.8.14