Stupid winsock needs special way to close sockets.
[dftpd.git] / String.cpp
blob6b67b4136b732d1fb8c6528de9160e8f11cc2b7f
1 #include <ctype.h>
2 #include <algorithm>
3 #include "String.hpp"
4 #include "Exceptions.hpp"
6 inline static bool IsSpace( char c )
8 return isspace( c );
11 inline static bool IsNotSpace( char c )
13 return !isspace( c );
16 inline static bool IsSlash( char c )
18 return c == '/';
21 inline static bool IsNotSlash( char c )
23 return c != '/';
26 inline static bool IsComma( char c )
28 return c == ',';
31 inline static bool IsNotComma( char c )
33 return c != ',';
36 PathVector SplitPath( const std::string& str )
38 PathVector ret;
40 typedef std::string::const_iterator Iter;
42 Iter i = str.begin(), j;
44 while( i != str.end() )
46 i = std::find_if( i, str.end(), IsNotSlash );
47 j = std::find_if( i, str.end(), IsSlash );
49 if( i != str.end() )
51 ret.push_back( std::string( i, j ) );
54 i = j;
57 if( ret.size() > 0 && ret[0] == "~" )
59 ret.erase( ret.begin() );
62 return ret;
65 PortVector SplitPort( const std::string& str )
67 PortVector ret;
69 typedef std::string::const_iterator Iter;
71 Iter i = str.begin(), j;
73 while( i != str.end() )
75 i = std::find_if( i, str.end(), IsNotComma );
76 j = std::find_if( i, str.end(), IsComma );
78 if( i != str.end() )
80 ret.push_back( std::string( i, j ) );
83 i = j;
86 return ret;
89 Command Split( const std::string& str )
91 Command ret;
93 std::string::const_iterator it = std::find_if( str.begin(), str.end(), IsSpace );
95 if( it == str.end() )
97 ret.push_back( str );
99 else
101 ret.push_back( std::string( str.begin(), it ) );
102 ret.push_back( std::string( it + 1, str.end() ) );
105 return ret;
108 Command ParseCommand( const std::string& cmd )
110 Command ret( Split( cmd ) );
112 if( cmd.size() == 0 )
114 throw SyntaxErrorException;
117 ToUpper( ret[0] );
119 return ret;
122 void ToUpper( std::string& str )
124 std::transform( str.begin(), str.end(), str.begin(), toupper );