|
rllib
1
|
00001 /*************************************************************************** 00002 rlfileload.cpp - description 00003 ------------------- 00004 begin : Fri Jul 28 2006 00005 copyright : (C) 2006 by Rainer Lehrig 00006 email : lehrig@t-online.de 00007 ***************************************************************************/ 00008 00009 /*************************************************************************** 00010 * * 00011 * This library is free software; you can redistribute it and/or modify * 00012 * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as * 00013 * published by the Free Software Foundation * 00014 * * 00015 ***************************************************************************/ 00016 #include "rlfileload.h" 00017 #include <stdio.h> 00018 #include <string.h> 00019 00020 rlFileLoad::rlFileLoad() 00021 { 00022 loaded = debug = 0; 00023 file_lines.line = NULL; 00024 file_lines.next = NULL; 00025 current_line = NULL; 00026 } 00027 00028 rlFileLoad::~rlFileLoad() 00029 { 00030 unload(); 00031 } 00032 00033 int rlFileLoad::load(const char *filename) 00034 { 00035 FILE *fin; 00036 rlFileLines *fl; 00037 char line[rl_PRINTF_LENGTH], *cptr; 00038 00039 unload(); 00040 fin = fopen(filename,"r"); 00041 if(fin == NULL) return -1; 00042 00043 fl = &file_lines; 00044 while(fgets(line,sizeof(line)-1,fin) != NULL) 00045 { 00046 cptr = strchr(line,'\n'); 00047 if(cptr != NULL) *cptr = '\0'; 00048 cptr = strchr(line,0x0D); 00049 if(cptr != NULL) *cptr = '\0'; 00050 if(debug) printf("rlFileLoad::load line=%s\n",line); 00051 fl->next = new rlFileLines; 00052 fl = fl->next; 00053 fl->line = new char [strlen(line)+1]; 00054 strcpy(fl->line,line); 00055 fl->next = NULL; 00056 } 00057 00058 fclose(fin); 00059 loaded = 1; 00060 return 1; 00061 } 00062 00063 void rlFileLoad::unload() 00064 { 00065 rlFileLines *fl,*flold; 00066 00067 if(loaded == 0) return; 00068 fl = &file_lines; 00069 fl = fl->next; 00070 while(fl != NULL) 00071 { 00072 if(debug) printf("rlFileLoad::unload line=%s",fl->line); 00073 delete [] fl->line; 00074 flold = fl; 00075 fl = fl->next; 00076 delete flold; 00077 } 00078 loaded = 0; 00079 } 00080 00081 const char *rlFileLoad::firstLine() 00082 { 00083 if(loaded == 0) return NULL; 00084 current_line = &file_lines; 00085 current_line = current_line->next; 00086 if(current_line == NULL) return NULL; 00087 if(debug) printf("rlFileLoad::firstLine=%s",current_line->line); 00088 return current_line->line; 00089 } 00090 00091 const char *rlFileLoad::nextLine() 00092 { 00093 if(loaded == 0) return NULL; 00094 if(current_line != NULL) current_line = current_line->next; 00095 if(current_line == NULL) return NULL; 00096 if(debug) printf("rlFileLoad::nextLine=%s",current_line->line); 00097 return current_line->line; 00098 } 00099 00100 void rlFileLoad::setDebug(int state) 00101 { 00102 if(state == 0) debug = 0; 00103 else debug = 1; 00104 }
1.7.5.1