]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/regex/example/snippets/regex_match_example.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / regex / example / snippets / regex_match_example.cpp
CommitLineData
7c673cae
FG
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
b32b8144 19#include <boost/regex.hpp>
7c673cae
FG
20#include <cstdlib>
21#include <stdlib.h>
7c673cae
FG
22#include <string>
23#include <iostream>
24
25using namespace std;
26using namespace boost;
27
28regex expression("^([0-9]+)(\\-| |$)(.*)$");
29
30// process_ftp:
31// on success returns the ftp response code, and fills
32// msg with the ftp response message.
33int 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(__BORLANDC__) && (__BORLANDC__ == 0x550))
53//
54// problem with std::getline under MSVC6sp3
55istream& 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
68int 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