-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprep.cpp
108 lines (101 loc) · 3.1 KB
/
prep.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
* prep.cpp -
* @author: Jonathan Beard
* @version: Wed Jan 30 12:46:53 2013
*/
#include <vector>
#include <sstream>
#include <iostream>
#include <cassert>
#include <fstream>
#include <iterator>
#include <algorithm>
#include <cstring>
#include "prep.hpp"
#include "signalhooks.hpp"
std::set< std::string >*
Prep::get_rf_includes ( const std::string main_ap,
Raft::Data &data )
{
std::set< std::string > *ret_val( nullptr );
std::ifstream in_file( main_ap.c_str(), std::ifstream::in );
if( in_file.is_open() ){
ret_val = new std::set< std::string >();
std::string line;
while( in_file.good() ){
std::getline( in_file, line );
auto *tokens( Prep::get_tokens( line ));
assert( tokens != nullptr );
std::string filename;
if( Prep::is_rf_file( (*tokens), filename ) ){
ret_val->insert( filename );
}
}
}else{
/* error */
data.get_rf_errorstream() <<
"Unable to open file: " << main_ap << "\n";
raise( TERM_ERR_SIG );
}
return( ret_val );
}
std::vector<std::string>*
Prep::get_tokens( const std::string &line )
{
std::istringstream iss( line );
std::vector< std::string > *ret_val( nullptr );
ret_val = new std::vector< std::string >();
std::copy( std::istream_iterator< std::string >(iss),
std::istream_iterator< std::string> (),
std::back_inserter< std::vector< std::string > >(*ret_val));
return( ret_val );
}
bool
Prep::is_rf_file( std::vector< std::string > &tokens,
std::string &filename )
{
if( tokens.size() != 2 ) return( false );
/* we're looking for 2 tokens */
for( int i = 0; i < 2; i++){
switch( i ){
case( 0 ) :
{
if( tokens[0].compare( "#include" ) != 0){
return( false );
}
}
break;
case( 1 ) :
{
std::string &str( tokens[1] );
const auto length( str.length() );
char *buffer_name( nullptr );
buffer_name = (char*) malloc( sizeof( char ) * length );
assert( buffer_name != nullptr );
char *buffer_ext( nullptr );
buffer_ext = (char*) malloc( sizeof( char ) * length );
assert( buffer_ext != nullptr );
const auto num_read = sscanf( str.c_str(),
"\"%[^.].%[^\"]",
buffer_name,
buffer_ext );
if( num_read != 2 ) return( false );
/* if we're here go ahead and set filename */
std::stringstream ss;
ss << buffer_name << "." << buffer_ext;
filename = ss.str();
/* don't need this anymore */
free( buffer_name );
if( strcmp( buffer_ext, (char*) "raft" ) != 0 ){
free( buffer_ext );
return( false );
}
free( buffer_ext );
}
break;
default:
return( false );
}
}
return( true );
}