gfactoryparser.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2013 Graeme Walker <graeme_walker@users.sourceforge.net>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 //
18 // gfactoryparser.cpp
19 //
20 
21 #include "gdef.h"
22 #include "gsmtp.h"
23 #include "gfactoryparser.h"
24 #include "gresolver.h"
25 #include "gexecutable.h"
26 #include "gstr.h"
27 #include "gfile.h"
28 
29 std::pair<std::string,std::string> GSmtp::FactoryParser::parse( const std::string & address ,
30  const std::string & extra_net_prefix )
31 {
32  G_DEBUG( "GSmtp::FactoryParser::parse: [" << address << "] [" << extra_net_prefix << "]" ) ;
33  if( address.find("ip:") == 0U || address.find("net:") == 0U )
34  {
35  return std::make_pair( std::string("net") , G::Str::tail(address,address.find(":")) ) ;
36  }
37  else if( address.find(extra_net_prefix+":") == 0U )
38  {
39  return std::make_pair( extra_net_prefix , G::Str::tail(address,address.find(":")) ) ;
40  }
41  else if( address.find("file:") == 0U || address.find("exe:") == 0U )
42  {
43  return std::make_pair( std::string("file") , G::Str::tail(address,address.find(":")) ) ;
44  }
45  else if( address.find("exit:") == 0U )
46  {
47  return std::make_pair( std::string("exit") , G::Str::tail(address,address.find(":")) ) ;
48  }
49  else if( address.empty() )
50  {
51  return std::make_pair( std::string() , std::string() ) ;
52  }
53  else
54  {
55  return std::make_pair( std::string("file") , address ) ;
56  }
57 }
58 
59 std::string GSmtp::FactoryParser::check( const std::string & address , const std::string & extra_net_prefix )
60 {
61  std::pair<std::string,std::string> p = parse( address , extra_net_prefix ) ;
62  if( p.first == "net" || p.first == extra_net_prefix )
63  {
64  std::string s1 , s2 ;
65  if( ! GNet::Resolver::parse(p.second,s1,s2) )
66  return std::string() + "invalid network address: " + p.second ;
67  else
68  return std::string() ;
69  }
70  else if( p.first == "file" )
71  {
72  G::Executable exe( p.second ) ;
73  if( ! G::File::exists(exe.exe(),G::File::NoThrow()) )
74  return "no such file" ;
75  else if( ! G::File::executable(exe.exe()) )
76  return "probably not executable" ;
77  else if( ! exe.exe().isAbsolute() )
78  return "not an absolute path" ;
79  else
80  return std::string() ;
81  }
82  else if( p.first == "exit" )
83  {
84  if( !G::Str::isUInt(p.second) )
85  return "not a numeric exit code" ;
86  else
87  return std::string() ;
88  }
89  else
90  {
91  return std::string() ;
92  }
93 }
94 
A structure representing an external program, holding a path and a set of arguments.
Definition: gexecutable.h:43
static std::string tail(const std::string &in, std::string::size_type pos, const std::string &default_=std::string())
Returns the last part of the string after the given position.
Definition: gstr.cpp:842
static bool executable(const Path &)
Returns true if the path is probably executable.
Definition: gfile_unix.cpp:50
static bool parse(const std::string &in, std::string &host_or_address, std::string &service_or_port)
Parses a string that contains a hostname or ip address plus a server name or port number...
Definition: gresolver.cpp:61
static std::pair< std::string, std::string > parse(const std::string &address, const std::string &)
Parses an identifier.
static std::string check(const std::string &address, const std::string &)
Parses and checks an identifier.
bool isAbsolute() const
Returns !isRelative().
Definition: gpath.cpp:150
static bool exists(const Path &file)
Returns true if the file (directory, link, device etc.) exists.
Definition: gfile.cpp:130
#define G_DEBUG(expr)
Definition: glog.h:95
An overload discriminator class for File methods.
Definition: gfile.h:56
static bool isUInt(const std::string &s)
Returns true if the string can be converted into an unsigned integer without throwing an exception...
Definition: gstr.cpp:190
Path exe() const
Returns the executable.
Definition: gexecutable.cpp:62