1 /*
2 *
3 * Copyright (c) 1998-2002
4 * John Maddock
5 *
6 * Use, modification and distribution are subject to the
7 * Boost Software License, Version 1.0. (See accompanying file
8 * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 *
10 */
11
12 /*
13 * LOCATION: see http://www.boost.org for most recent version.
14 * FILE regex_match_example.cpp
15 * VERSION see <boost/version.hpp>
16 * DESCRIPTION: ftp based regex_match example.
17 */
18
19 #include <boost/regex.hpp>
20 #include <cstdlib>
21 #include <stdlib.h>
22 #include <string>
23 #include <iostream>
24
25 using namespace std;
26 using namespace boost;
27
28 regex expression("^([0-9]+)(\\-| |$)(.*)$");
29
30 // process_ftp:
31 // on success returns the ftp response code, and fills
32 // msg with the ftp response message.
process_ftp(const char * response,std::string * msg)33 int process_ftp(const char* response, std::string* msg)
34 {
35 cmatch what;
36 if(regex_match(response, what, expression))
37 {
38 // what[0] contains the whole string
39 // what[1] contains the response code
40 // what[2] contains the separator character
41 // what[3] contains the text message.
42 if(msg)
43 msg->assign(what[3].first, what[3].second);
44 return ::atoi(what[1].first);
45 }
46 // failure did not match
47 if(msg)
48 msg->erase();
49 return -1;
50 }
51
52 #if defined(BOOST_MSVC) || (defined(BOOST_BORLANDC) && (BOOST_BORLANDC == 0x550))
53 //
54 // problem with std::getline under MSVC6sp3
getline(istream & is,std::string & s)55 istream& getline(istream& is, std::string& s)
56 {
57 s.erase();
58 char c = static_cast<char>(is.get());
59 while(c != '\n')
60 {
61 s.append(1, c);
62 c = static_cast<char>(is.get());
63 }
64 return is;
65 }
66 #endif
67
main(int argc,const char * [])68 int main(int argc, const char*[])
69 {
70 std::string in, out;
71 do
72 {
73 if(argc == 1)
74 {
75 cout << "enter test string" << endl;
76 getline(cin, in);
77 if(in == "quit")
78 break;
79 }
80 else
81 in = "100 this is an ftp message text";
82 int result;
83 result = process_ftp(in.c_str(), &out);
84 if(result != -1)
85 {
86 cout << "Match found:" << endl;
87 cout << "Response code: " << result << endl;
88 cout << "Message text: " << out << endl;
89 }
90 else
91 {
92 cout << "Match not found" << endl;
93 }
94 cout << endl;
95 } while(argc == 1);
96 return 0;
97 }
98
99
100
101
102
103
104
105
106