rllib  1
Public Member Functions | Private Attributes | List of all members
rlFileLoad Class Reference

#include <rlfileload.h>

Collaboration diagram for rlFileLoad:
Collaboration graph
[legend]

Public Member Functions

 rlFileLoad ()
 
virtual ~rlFileLoad ()
 
int load (const char *filename)
 
void unload ()
 
const char * firstLine ()
 
const char * nextLine ()
 
void setDebug (int state)
 
int text2rlstring (rlString &rlstring)
 

Private Attributes

int loaded
 
int debug
 
rlFileLines file_lines
 
rlFileLinescurrent_line
 

Detailed Description

This class loads a text file to memory.
Then you can iterate to it's lines.

Definition at line 32 of file rlfileload.h.

Constructor & Destructor Documentation

◆ rlFileLoad()

rlFileLoad::rlFileLoad ( )

Definition at line 20 of file rlfileload.cpp.

21 {
22  loaded = debug = 0;
23  file_lines.line = NULL;
24  file_lines.next = NULL;
25  current_line = NULL;
26 }
rlFileLines * current_line
Definition: rlfileload.h:47
char * line
Definition: rlfileload.h:24
rlFileLines file_lines
Definition: rlfileload.h:46
int loaded
Definition: rlfileload.h:44
struct _rlFileLines_ * next
Definition: rlfileload.h:25

◆ ~rlFileLoad()

rlFileLoad::~rlFileLoad ( )
virtual

Definition at line 28 of file rlfileload.cpp.

29 {
30  unload();
31 }
void unload()
Definition: rlfileload.cpp:63

Member Function Documentation

◆ firstLine()

const char * rlFileLoad::firstLine ( )

Definition at line 81 of file rlfileload.cpp.

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 }
rlFileLines * current_line
Definition: rlfileload.h:47
char * line
Definition: rlfileload.h:24
rlFileLines file_lines
Definition: rlfileload.h:46
int loaded
Definition: rlfileload.h:44
struct _rlFileLines_ * next
Definition: rlfileload.h:25

◆ load()

int rlFileLoad::load ( const char *  filename)

Definition at line 33 of file rlfileload.cpp.

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 }
void unload()
Definition: rlfileload.cpp:63
char * line
Definition: rlfileload.h:24
rlFileLines file_lines
Definition: rlfileload.h:46
#define rl_PRINTF_LENGTH
Definition: rldefine.h:71
int loaded
Definition: rlfileload.h:44
struct _rlFileLines_ * next
Definition: rlfileload.h:25
struct _rlFileLines_ rlFileLines

◆ nextLine()

const char * rlFileLoad::nextLine ( )

Definition at line 91 of file rlfileload.cpp.

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 }
rlFileLines * current_line
Definition: rlfileload.h:47
char * line
Definition: rlfileload.h:24
int loaded
Definition: rlfileload.h:44
struct _rlFileLines_ * next
Definition: rlfileload.h:25

◆ setDebug()

void rlFileLoad::setDebug ( int  state)

Definition at line 100 of file rlfileload.cpp.

101 {
102  if(state == 0) debug = 0;
103  else debug = 1;
104 }

◆ text2rlstring()

int rlFileLoad::text2rlstring ( rlString rlstring)

Definition at line 106 of file rlfileload.cpp.

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 }
int loaded
Definition: rlfileload.h:44
const char * nextLine()
Definition: rlfileload.cpp:91
const char * firstLine()
Definition: rlfileload.cpp:81

◆ unload()

void rlFileLoad::unload ( )

Definition at line 63 of file rlfileload.cpp.

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 }
char * line
Definition: rlfileload.h:24
rlFileLines file_lines
Definition: rlfileload.h:46
int loaded
Definition: rlfileload.h:44
struct _rlFileLines_ * next
Definition: rlfileload.h:25

Member Data Documentation

◆ current_line

rlFileLines* rlFileLoad::current_line
private

Definition at line 47 of file rlfileload.h.

◆ debug

int rlFileLoad::debug
private

Definition at line 45 of file rlfileload.h.

◆ file_lines

rlFileLines rlFileLoad::file_lines
private

Definition at line 46 of file rlfileload.h.

◆ loaded

int rlFileLoad::loaded
private

Definition at line 44 of file rlfileload.h.


The documentation for this class was generated from the following files: