Main Page | Alphabetical List | Compound List | File List | Compound Members | File Members

pred_fileio.h

Go to the documentation of this file.
00001 /*  This file is part of OMCSNetCPP
00002 
00003     OMCSNetCPP is free software; you can redistribute it and/or modify
00004     it under the terms of the GNU Lesser General Public License as published by
00005     the Free Software Foundation; either version 2.1 of the License, or
00006     (at your option) any later version.
00007 
00008     OMCSNetCPP is distributed in the hope that it will be useful,
00009     but WITHOUT ANY WARRANTY; without even the implied warranty of
00010     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011     GNU Lesser General Public License for more details.
00012 
00013     You should have received a copy of the GNU Lesser General Public License
00014     along with OMCSNetCPP; if not, write to the Free Software
00015     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00016  */
00017 
00020 #ifndef _PRED_FILEIO
00021 
00022 #define _PRED_FILEIO
00023 
00024 #include <stdio.h>
00025 
00026 #include <string>
00027 
00028 
00030 class CPredicateFileIO
00031 {
00032         public:
00034                 bool OpenInputFile(const std::string &a_path)
00035                 {
00036                         if (m_fp)
00037                         {
00038                                 return false;
00039                         }
00040 
00041                         m_fp = fopen(a_path.c_str(), "rt");
00042 
00043                         if (m_fp)
00044                         {
00045                                 return true;
00046                         }
00047 
00048                         return false;
00049                 }
00050 
00052                 bool CloseInputFile()
00053                 {
00054                         if (m_fp)
00055                         {
00056                                 fclose(m_fp);
00057 
00058                                 m_fp = NULL;
00059 
00060                                 return true;
00061                         }
00062 
00063                         return false;
00064                 }
00065 
00067                 std::string GetPredicate(bool *a_flag)
00068                 {
00069                         if (m_fp)
00070                         {
00071                                 std::string l_string;
00072 
00073                                 if (true == p_GetLine(l_string))
00074                                 {
00075                                         if (a_flag)
00076                                         {
00077                                                 *a_flag = true;
00078                                         }
00079 
00080                                         return l_string;
00081                                 }       
00082                         }
00083 
00084                         if (a_flag)
00085                         {
00086                                 *a_flag = false;
00087                         }
00088 
00089                         return "";
00090                 }
00091 
00093                 CPredicateFileIO()
00094                 {
00095                         m_fp = NULL;
00096                 }
00097 
00099                 ~CPredicateFileIO()
00100                 {
00101                         CloseInputFile();
00102                 }
00103 
00104         private:
00106                 bool p_GetLine(std::string &a_string) const
00107                 {
00108                         if (!m_fp)
00109                                 return false;
00110 
00111                         while (!feof(m_fp))
00112                         {
00113                                 char l_buffer[512];
00114 
00115                                 l_buffer[0] = 0;
00116 
00117                                 fgets(l_buffer, 512, m_fp);
00118 
00119                                 if (strlen(l_buffer) > 0)
00120                                 {
00121                                         int l_end = strlen(l_buffer);
00122 
00123                                         while (l_end > 0)
00124                                         {
00125                                                 if (('\r' == l_buffer[l_end - 1]) ||
00126                                                     ('\n' == l_buffer[l_end - 1]))
00127                                                 {
00128                                                         l_end--;
00129                                                 }
00130                                                 else break;
00131                                         }
00132 
00133                                         if ((l_end >  0) &&
00134                                             ('#'   != l_buffer[0]))
00135                                         {
00136                                                 a_string.assign(l_buffer, l_end);
00137 
00138                                                 return true;
00139                                         }
00140                                 }
00141                         }
00142 
00143                         return false;
00144                 }
00145 
00147                 FILE *m_fp;
00148 };
00149 
00150 #endif
00151 

Generated on Tue Sep 16 09:43:50 2003 for OMCSNetCPP by doxygen 1.3.3