Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

PolicyFile.cc File Reference

#include "config.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include <map>
#include <stdio.h>
#include <string>
#include <vector>
#include "Debug.h"
#include "util.h"
#include "PolicyFile.h"

Include dependency graph for PolicyFile.cc:

Include dependency graph

Go to the source code of this file.

Classes

struct  PolicyFile

Typedefs

typedef map< string, PolicyFile * > PolicyFileMap

Functions

int how_many_lines_in (const char *policy_filename)
bool LoadPolicyFileText (const char *policy_filename)
bool PrintLines (const char *policy_filename, unsigned int start_line, unsigned int how_many_lines, bool show_numbers)

Variables

PolicyFileMap policy_files


Typedef Documentation

typedef map<string, PolicyFile*> PolicyFileMap
 

Definition at line 29 of file PolicyFile.cc.


Function Documentation

int how_many_lines_in const char *  policy_filename  ) 
 

Definition at line 32 of file PolicyFile.cc.

References debug_msg(), internal_error(), PolicyFile::lines, and policy_files.

Referenced by dbg_cmd_list(), and parse_location_string().

00033         {
00034         if ( ! policy_filename )
00035                 internal_error("NULL value passed to how_many_lines_in\n");
00036 
00037         FILE* throwaway = fopen(policy_filename, "r");
00038         if ( ! throwaway )
00039                 {
00040                 debug_msg("No such policy file: %s.\n", policy_filename);
00041                 return -1;
00042                 }
00043 
00044         fclose(throwaway);
00045 
00046         PolicyFileMap::iterator match;
00047         match = policy_files.find(policy_filename);
00048 
00049         if ( match == policy_files.end() )
00050                 {
00051                 match = policy_files.find(policy_filename);
00052                 if ( match == policy_files.end() )
00053                         {
00054                         debug_msg("Policy file %s was not loaded.\n", policy_filename);
00055                         return -1;
00056                         }
00057                 }
00058 
00059         PolicyFile* pf = match->second;
00060         return pf->lines.size();
00061         }

bool LoadPolicyFileText const char *  policy_filename  ) 
 

Definition at line 63 of file PolicyFile.cc.

References debug_msg(), PolicyFile::filedata, fstat(), int, PolicyFile::lines, PolicyFile::lmtime, policy_files, size, and stat().

00064         {
00065         if ( ! policy_filename )
00066                 return true;
00067 
00068         FILE* f = fopen(policy_filename, "r");
00069 
00070         if ( ! f )
00071                 {
00072                 debug_msg("No such policy file: %s.\n", policy_filename);
00073                 return false;
00074                 }
00075 
00076         PolicyFile* pf = new PolicyFile;
00077 
00078         if ( policy_files.find(policy_filename) != policy_files.end() )
00079                 debug_msg("Policy file %s already loaded\n", policy_filename);
00080 
00081         policy_files.insert(PolicyFileMap::value_type(policy_filename, pf));
00082 
00083         struct stat st;
00084         fstat(fileno(f), &st);
00085 
00086         pf->lmtime = st.st_mtime;
00087         off_t size = st.st_size;
00088 
00089         // ### This code is not necessarily Unicode safe!
00090         // (probably fine with UTF-8)
00091         pf->filedata = new char[size+1];
00092         fread(pf->filedata, size, 1, f);
00093         pf->filedata[size] = 0;
00094         fclose(f);
00095 
00096         // Separate the string by newlines.
00097         pf->lines.push_back(pf->filedata);
00098         for ( char* iter = pf->filedata; *iter; ++iter )
00099                 {
00100                 if ( *iter == '\n' )
00101                         {
00102                         *iter = 0;
00103                         if ( *(iter + 1) )
00104                                 pf->lines.push_back(iter + 1);
00105                         }
00106                 }
00107 
00108         for ( int i = 0; i < int(pf->lines.size()); ++i )
00109                 assert(pf->lines[i][0] != '\n');
00110 
00111         return true;
00112         }

bool PrintLines const char *  policy_filename,
unsigned int  start_line,
unsigned int  how_many_lines,
bool  show_numbers
 

Definition at line 115 of file PolicyFile.cc.

References debug_msg(), PolicyFile::lines, and policy_files.

Referenced by dbg_cmd_list(), and dbg_handle_debug_input().

00117         {
00118         if ( ! policy_filename )
00119                 return true;
00120 
00121         FILE* throwaway = fopen(policy_filename, "r");
00122         if ( ! throwaway )
00123                 {
00124                 debug_msg("No such policy file: %s.\n", policy_filename);
00125                 return false;
00126                 }
00127 
00128         fclose(throwaway);
00129 
00130         PolicyFileMap::iterator match;
00131         match = policy_files.find(policy_filename);
00132 
00133         if ( match == policy_files.end() )
00134                 {
00135                 match = policy_files.find(policy_filename);
00136                 if ( match == policy_files.end() )
00137                         {
00138                         debug_msg("Policy file %s was not loaded.\n", policy_filename);
00139                         return false;
00140                         }
00141                 }
00142 
00143         PolicyFile* pf = match->second;
00144 
00145         if ( start_line < 1 )
00146                 start_line = 1;
00147 
00148         if ( start_line > pf->lines.size() )
00149                 {
00150                 debug_msg("Line number %d out of range; %s has %d lines\n",
00151                         start_line, policy_filename, pf->lines.size());
00152                 return false;
00153                 }
00154 
00155         if ( start_line + how_many_lines - 1 > pf->lines.size() )
00156                 how_many_lines = pf->lines.size() - start_line + 1;
00157 
00158         for ( unsigned int i = 0; i < how_many_lines; ++i )
00159                 {
00160                 if ( show_numbers )
00161                         debug_msg("%d\t", i + start_line);
00162 
00163                 const char* line = pf->lines[start_line + i - 1];
00164                 debug_msg("%s\n", line);
00165                 }
00166 
00167         return true;
00168         }


Variable Documentation

PolicyFileMap policy_files [static]
 

Definition at line 30 of file PolicyFile.cc.

Referenced by how_many_lines_in(), LoadPolicyFileText(), and PrintLines().


Generated on Wed Sep 14 03:03:14 2005 for bro_docs by doxygen 1.3.5