~ubuntu-branches/ubuntu/vivid/qgo/vivid-proposed

« back to all changes in this revision

Viewing changes to src/newline_pipe.h

  • Committer: Package Import Robot
  • Author(s): Yann Dirson
  • Date: 2012-05-19 19:05:05 UTC
  • mfrom: (1.1.12)
  • Revision ID: package-import@ubuntu.com-20120519190505-b23f5tzx7y8cu946
Tags: 2~svn764-1
* The "Raise dead" release (Closes: #673520), new maintainer.
* New upstream snapshot with Qt4 support (Closes: #604589), adjusted
  build-deps.
* Switched to source format "3.0 (quilt)", adjusted build-deps.
* Switched to dh and debhelper compat level 9, adjusted build-deps.
* Build with -fpermissive.
* New build-dep libasound2-dev, remove obsolete build-dep on libxinerama-dev.
* Refreshed patches 01_gnugo and 04_desktop, leaving 20_kfreebsd away
  for this release, and removing the remaining ones, now obsolete.
* Added patch 02_usrgames for FHS-correct install location.
* Adjusted icon names in menu file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2009 by The qGo Project                                 *
 
3
 *                                                                         *
 
4
 *   This file is part of qGo.                                             *
 
5
 *                                                                         *
 
6
 *   qGo is free software: you can redistribute it and/or modify           *
 
7
 *   it under the terms of the GNU General Public License as published by  *
 
8
 *   the Free Software Foundation; either version 2 of the License, or     *
 
9
 *   (at your option) any later version.                                   *
 
10
 *                                                                         *
 
11
 *   This program is distributed in the hope that it will be useful,       *
 
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
14
 *   GNU General Public License for more details.                          *
 
15
 *                                                                         *
 
16
 *   You should have received a copy of the GNU General Public License     *
 
17
 *   along with this program; if not, see <http://www.gnu.org/licenses/>   *
 
18
 *   or write to the Free Software Foundation, Inc.,                       *
 
19
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 
20
 ***************************************************************************/
 
21
 
 
22
 
 
23
#ifndef NEWLINE_PIPE_H
 
24
#define NEWLINE_PIPE_H
 
25
#include <deque>
 
26
 
 
27
template <typename T>
 
28
class newline_pipe 
 
29
{
 
30
        public:
 
31
                newline_pipe()
 
32
                {
 
33
                };
 
34
                ~newline_pipe()
 
35
                {
 
36
                };
 
37
                void write(T src)
 
38
                {
 
39
                        q.push_back(src);
 
40
                };
 
41
                void write(T * src)
 
42
                {
 
43
                        unsigned int i = 0;
 
44
                        while(src[i] != 0x00)
 
45
                                q.push_back(src[i++]);
 
46
                };
 
47
                void write(T * src, unsigned int max)
 
48
                {
 
49
                        unsigned int i = 0;
 
50
                        while(i < max)
 
51
                                q.push_back(src[i++]);
 
52
                };
 
53
                unsigned int read(T * dst, unsigned int n)
 
54
                {
 
55
                        unsigned int i = 0;
 
56
                        unsigned int bytes = (n > q.size() ? q.size() : n);
 
57
                        while(i < bytes)
 
58
                                { dst[i++] = q.front(); q.pop_front(); }
 
59
                        return i;
 
60
 
 
61
                };
 
62
                unsigned int peek(T * dst, unsigned int n)
 
63
                {
 
64
                        unsigned int i = 0;
 
65
                        unsigned int bytes = (n > q.size() ? q.size() : n);
 
66
                        while(i < bytes)
 
67
                                { dst[i] = q[i]; i++;}
 
68
                        return i;
 
69
                }
 
70
                unsigned int readLine(T * dst, unsigned int max)
 
71
                {
 
72
                        unsigned int i;
 
73
                        i = canReadLine();
 
74
                        if(i && i <= max)
 
75
                        {
 
76
                                i = read(dst, i);
 
77
                                dst[i] = '\0';
 
78
                                return i;
 
79
                        }
 
80
                        else
 
81
                                return 0;
 
82
 
 
83
                };
 
84
                unsigned int canReadLine(void)
 
85
                {
 
86
                        unsigned int i = 0;
 
87
                        while(i < q.size())
 
88
                        {
 
89
                                if(q[i] == '\n')        
 
90
                                        break;
 
91
                                //else if(q[i] == '\r')
 
92
                                        //break;
 
93
                                i++;
 
94
                        }
 
95
                        if(i == q.size())
 
96
                                return 0;
 
97
                        else
 
98
                                return i + 1;
 
99
                };
 
100
                unsigned int canReadHTTPLine(void)
 
101
                {
 
102
                        unsigned int i = 3;
 
103
                        if(q.size() < 4)
 
104
                                return 0;
 
105
                        while(i < q.size())
 
106
                        {
 
107
                                if(q[i - 3] == '\r' &&
 
108
                                   q[i - 2] == '\n' &&
 
109
                                   q[i - 1] == '\r' &&
 
110
                                   q[i] == '\n')        
 
111
                                        break;
 
112
                                i++;
 
113
                        }
 
114
                        if(i == q.size())
 
115
                                return 0;
 
116
                        else
 
117
                                return i + 1;
 
118
                };
 
119
                unsigned int canRead(void)
 
120
                {
 
121
                        return q.size();
 
122
                };
 
123
        private:
 
124
                std::deque <T> q;
 
125
                
 
126
};
 
127
#endif //NEWLINE_PIPE_H