1 /* GST123 - GStreamer based command line media player
2 * Copyright (C) 2010 Siddhesh Poyarekar
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library 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 GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
18 */
19
20 #include <cstdio>
21 #include "iostream.h"
22 #include <errno.h>
23 #include <glib.h>
24 #include <cstring>
25 #include <unistd.h>
26
27 #include <iostream>
28 #include <sstream>
29
30 using std::cerr;
31 using std::endl;
32 using std::string;
33 using std::stringstream;
34
35 using namespace Gst123;
36
IOStream()37 IOStream::IOStream()
38 {
39 bof = true;
40 eof = false;
41 fd = -1;
42 status = 0;
43 }
44
~IOStream()45 IOStream::~IOStream()
46 {
47 }
48
49 string
get_content_type()50 IOStream::get_content_type()
51 {
52 return "";
53 }
54
55 /*
56 * Read a line of input from in
57 * The newline string is to specify the separator we have
58 * between lines. It is \n by default, but can be set to
59 * "\r\n" or any other arbitrary string
60 *
61 * Returns length of the line read
62 */
63 int
readline(const string & newline)64 IOStream::readline (const string& newline)
65 {
66 char buf [4096];
67 int len = -1;
68 size_t pos = string::npos;
69
70 if (!eof)
71 {
72 do
73 {
74 len = read (fd, buf, sizeof (buf) - 1);
75
76 if (len == 0)
77 break;
78
79 if (len < 0)
80 {
81 cerr << "Read error on fd " << fd
82 << ": " << strerror (errno) << endl;
83 status = errno;
84 return -status;
85 }
86
87 buf[len] = '\0';
88
89 strbuf += buf;
90 }
91 while ((pos = strbuf.find (newline)) == string::npos);
92 }
93 else if (strbuf == "")
94 return (status = IO_STREAM_EOF);
95
96 pos = strbuf.find (newline);
97
98 if (pos == string::npos)
99 {
100 curline = strbuf;
101 strbuf = "";
102 eof = true;
103 }
104 else
105 {
106 curline = strbuf.substr (0, pos);
107 strbuf = strbuf.substr (pos + newline.length(), string::npos);
108 }
109
110 bof = false;
111 return curline.length();
112 }
113
114 /* Look for a specific pattern in the first line of content */
115 bool
content_begins_with(const string & match)116 IOStream::content_begins_with (const string& match)
117 {
118 if (bof)
119 readline();
120
121 return (curline.find (match) == 0);
122 }
123
124 std::string&
get_current_line()125 IOStream::get_current_line()
126 {
127 return curline;
128 }
129
130 int
get_status()131 IOStream::get_status()
132 {
133 return status;
134 }
135
136 string
str_error(int error)137 IOStream::str_error (int error)
138 {
139 stringstream o;
140
141 if (!error)
142 error = status;
143
144 o << "IOStream: Unknown error "
145 << error;
146
147 return o.str();
148 }
149
150 string
read_str_error(int error)151 IOStream::read_str_error(int error)
152 {
153 if (!error)
154 error = status;
155
156 if (error == IO_STREAM_EOF)
157 return "Read Error: End of File";
158
159 return string ("Read error: ") + strerror (-error);
160 }
161