From 8c603fe7c66d41e605b6bf122dfcdfa50488defa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Sat, 7 Oct 2006 16:35:16 +0000 Subject: [PATCH] Build fixes for Haiku - is this WIN32 stuff really needed, or can we get rid of it? git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@19022 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- .../print/transports/ipp/IppContent.cpp | 2125 +++++++++-------- .../print/transports/ipp/IppTransport.cpp | 12 +- .../print/transports/ipp/IppURLConnection.cpp | 220 +- .../print/transports/lpr/LprTransport.cpp | 27 +- .../print/transports/lpr/LpsClient.cpp | 439 ++-- .../print/transports/shared/Socket.cpp | 17 +- .../print/transports/shared/SocketStream2.cpp | 258 +- 7 files changed, 1557 insertions(+), 1541 deletions(-) diff --git a/src/add-ons/print/transports/ipp/IppContent.cpp b/src/add-ons/print/transports/ipp/IppContent.cpp index 4b9350a9a7..ccec5e6475 100644 --- a/src/add-ons/print/transports/ipp/IppContent.cpp +++ b/src/add-ons/print/transports/ipp/IppContent.cpp @@ -1,1061 +1,1064 @@ -// Sun, 18 Jun 2000 -// Y.Takagi - -#ifdef WIN32 -#include -#else -#include -#endif - -#include -#include -#include - -#include "IppContent.h" - -/*----------------------------------------------------------------------*/ - -short readLength(istream &is) -{ - short len = 0; - is.read((char *)&len, sizeof(short)); - len = ntohs(len); - return len; -} - -void writeLength(ostream &os, short len) -{ - len = htons(len); - os.write((char *)&len, sizeof(short)); -} - -/*----------------------------------------------------------------------*/ - -DATETIME::DATETIME() -{ - memset(this, 0, sizeof(DATETIME)); -} - -DATETIME::DATETIME(const DATETIME &dt) -{ - memcpy(this, &dt.datetime, sizeof(DATETIME)); -} - -DATETIME & DATETIME::operator = (const DATETIME &dt) -{ - memcpy(this, &dt.datetime, sizeof(DATETIME)); - return *this; -} - -istream& operator >> (istream &is, DATETIME &attr) -{ - return is; -} - -ostream& operator << (ostream &os, const DATETIME &attr) -{ - return os; -} - - -/*----------------------------------------------------------------------*/ - -IppAttribute::IppAttribute(IPP_TAG t) - : tag(t) -{ -} - -int IppAttribute::length() const -{ - return 1; -} - -istream &IppAttribute::input(istream &is) -{ - return is; -} - -ostream &IppAttribute::output(ostream &os) const -{ - os << (unsigned char)tag; - return os; -} - -ostream &IppAttribute::print(ostream &os) const -{ - os << "Tag: " << hex << (int)tag << '\n'; - return os; -} - -/*----------------------------------------------------------------------*/ - -IppNamedAttribute::IppNamedAttribute(IPP_TAG t) - : IppAttribute(t) -{ -} - -IppNamedAttribute::IppNamedAttribute(IPP_TAG t, const char *s) - : IppAttribute(t), name(s ? s : "") -{ -} - -int IppNamedAttribute::length() const -{ - return IppAttribute::length() + 2 + name.length(); -} - -istream &IppNamedAttribute::input(istream &is) -{ - short len = readLength(is); - - if (0 < len) { - char *buffer = new char[len + 1]; - is.read(buffer, len); - buffer[len] = '\0'; - name = buffer; - delete [] buffer; - } - - return is; -} - -ostream &IppNamedAttribute::output(ostream &os) const -{ - IppAttribute::output(os); - - writeLength(os, name.length()); - os << name; - - return os; -} - -ostream &IppNamedAttribute::print(ostream &os) const -{ - IppAttribute::print(os); - os << '\t' << "Name: " << name << '\n'; - return os; -} - -/*----------------------------------------------------------------------*/ - -IppNoValueAttribute::IppNoValueAttribute(IPP_TAG t) - : IppNamedAttribute(t) -{ -} - -IppNoValueAttribute::IppNoValueAttribute(IPP_TAG t, const char *n) - : IppNamedAttribute(t, n) -{ -} - -int IppNoValueAttribute::length() const -{ - return IppAttribute::length() + 2; -} - -istream &IppNoValueAttribute::input(istream &is) -{ - IppNamedAttribute::input(is); - - short len = readLength(is); - - if (0 < len) { - is.seekg(len, ios::cur); - } - - return is; -} - -ostream &IppNoValueAttribute::output(ostream &os) const -{ - IppAttribute::output(os); - - writeLength(os, 0); - - return os; -} - -ostream &IppNoValueAttribute::print(ostream &os) const -{ - return IppNamedAttribute::print(os); -} - -/*----------------------------------------------------------------------*/ - -IppIntegerAttribute::IppIntegerAttribute(IPP_TAG t) - : IppNamedAttribute(t), value(0) -{ -} - -IppIntegerAttribute::IppIntegerAttribute(IPP_TAG t, const char *n, int v) - : IppNamedAttribute(t, n), value(v) -{ -} - -int IppIntegerAttribute::length() const -{ - return IppNamedAttribute::length() + 2 + 4; -} - -istream &IppIntegerAttribute::input(istream &is) -{ - IppNamedAttribute::input(is); - - short len = readLength(is); - - if (0 < len && len <= 4) { - is.read((char *)&value, sizeof(value)); - value = ntohl(value); - } else { - is.seekg(len, ios::cur); - } - - return is; -} - -ostream &IppIntegerAttribute::output(ostream &os) const -{ - IppNamedAttribute::output(os); - - writeLength(os, 4); - unsigned long val = htonl(value); - os.write((char *)&val, sizeof(val)); - return os; -} - -ostream &IppIntegerAttribute::print(ostream &os) const -{ - IppNamedAttribute::print(os); - os << '\t' << "Value: " << dec << value << '\n'; - return os; -} - -/*----------------------------------------------------------------------*/ - -IppBooleanAttribute::IppBooleanAttribute(IPP_TAG t) - : IppNamedAttribute(t), value(false) -{ -} - -IppBooleanAttribute::IppBooleanAttribute(IPP_TAG t, const char *n, bool f) - : IppNamedAttribute(t, n), value(f) -{ -} - -int IppBooleanAttribute::length() const -{ - return IppNamedAttribute::length() + 2 + 1; -} - - -istream &IppBooleanAttribute::input(istream &is) -{ - IppNamedAttribute::input(is); - - short len = readLength(is); - - if (0 < len && len <= 1) { - char c; - is.read((char *)&c, sizeof(c)); - value = c ? true : false; - } else { - is.seekg(len, ios::cur); - } - - return is; -} - -ostream &IppBooleanAttribute::output(ostream &os) const -{ - IppNamedAttribute::output(os); - - writeLength(os, 1); - char c = (char)value; - os.write((char *)&c, sizeof(c)); - - return os; -} - -ostream &IppBooleanAttribute::print(ostream &os) const -{ - IppNamedAttribute::print(os); - os << '\t' << "Value: " << value << '\n'; - return os; -} - -/*----------------------------------------------------------------------*/ - -IppDatetimeAttribute::IppDatetimeAttribute(IPP_TAG t) - : IppNamedAttribute(t) -{ -} - -IppDatetimeAttribute::IppDatetimeAttribute(IPP_TAG t, const char *n, const DATETIME *dt) - : IppNamedAttribute(t, n), datetime(*dt) -{ -} - -int IppDatetimeAttribute::length() const -{ - return IppNamedAttribute::length() + 2 + 11; -} - -istream &IppDatetimeAttribute::input(istream &is) -{ - IppNamedAttribute::input(is); - - short len = readLength(is); - - if (0 < len) { - if (len == 11) { - is >> datetime; - } else { - is.seekg(len, ios::cur); - } - } - - return is; -} - -ostream &IppDatetimeAttribute::output(ostream &os) const -{ - IppNamedAttribute::output(os); - - writeLength(os, 11); - os << datetime; - - return os; -} - -ostream &IppDatetimeAttribute::print(ostream &os) const -{ - IppNamedAttribute::print(os); - os << '\t' << "Value(DateTime): " << datetime << '\n'; - return os; -} - -/*----------------------------------------------------------------------*/ - -IppStringAttribute::IppStringAttribute(IPP_TAG t) - : IppNamedAttribute(t) -{ -} - -IppStringAttribute::IppStringAttribute(IPP_TAG t, const char *n, const char *s) -: IppNamedAttribute(t, n), text(s ? s : "") -{ -} - -int IppStringAttribute::length() const -{ - return IppNamedAttribute::length() + 2 + text.length(); -} - -istream &IppStringAttribute::input(istream &is) -{ - IppNamedAttribute::input(is); - - short len = readLength(is); - - if (0 < len) { - char *buffer = new char[len + 1]; - is.read(buffer, len); - buffer[len] = '\0'; - text = buffer; - delete [] buffer; - } - - return is; -} - -ostream &IppStringAttribute::output(ostream &os) const -{ - IppNamedAttribute::output(os); - - writeLength(os, text.length()); - os << text; - - return os; -} - -ostream &IppStringAttribute::print(ostream &os) const -{ - IppNamedAttribute::print(os); - os << '\t' << "Value: " << text << '\n'; - return os; -} - -/*----------------------------------------------------------------------*/ - -IppDoubleStringAttribute::IppDoubleStringAttribute(IPP_TAG t) - : IppNamedAttribute(t) -{ -} - -IppDoubleStringAttribute::IppDoubleStringAttribute(IPP_TAG t, const char *n, const char *s1, const char *s2) -: IppNamedAttribute(t, n), text1(s1 ? s1 : ""), text2(s2 ? s2 : "") -{ -} - -int IppDoubleStringAttribute::length() const -{ - return IppNamedAttribute::length() + 2 + text1.length() + 2 + text2.length(); -} - -istream &IppDoubleStringAttribute::input(istream &is) -{ - IppNamedAttribute::input(is); - - short len = readLength(is); - - if (0 < len) { - char *buffer = new char[len + 1]; - is.read(buffer, len); - buffer[len] = '\0'; - text1 = buffer; - delete [] buffer; - } - - len = readLength(is); - - if (0 < len) { - char *buffer = new char[len + 1]; - is.read(buffer, len); - buffer[len] = '\0'; - text2 = buffer; - delete [] buffer; - } - - return is; -} - -ostream &IppDoubleStringAttribute::output(ostream &os) const -{ - IppNamedAttribute::output(os); - - writeLength(os, text1.length()); - os << text1; - - writeLength(os, text2.length()); - os << text2; - - return os; -} - -ostream &IppDoubleStringAttribute::print(ostream &os) const -{ - IppNamedAttribute::print(os); - os << '\t' << "Value1: " << text1 << '\n'; - os << '\t' << "Value2: " << text2 << '\n'; - return os; -} - -/*----------------------------------------------------------------------*/ - -IppResolutionAttribute::IppResolutionAttribute(IPP_TAG t) - : IppNamedAttribute(t), xres(0), yres(0), resolution_units((IPP_RESOLUTION_UNITS)0) -{ -} - -IppResolutionAttribute::IppResolutionAttribute(IPP_TAG t, const char *n, int x, int y, IPP_RESOLUTION_UNITS u) - : IppNamedAttribute(t, n), xres(x), yres(y), resolution_units(u) -{ -} - -int IppResolutionAttribute::length() const -{ - return IppNamedAttribute::length() + 2 + 4 + 2 + 4 + 2 + 1; -} - -istream &IppResolutionAttribute::input(istream &is) -{ - IppNamedAttribute::input(is); - - short len = readLength(is); - - if (0 < len && len <= 4) { - is.read((char *)&xres, sizeof(xres)); - xres = ntohl(xres); - } else { - is.seekg(len, ios::cur); - } - - len = readLength(is); - - if (0 < len && len <= 4) { - is.read((char *)&yres, sizeof(yres)); - yres = ntohl(yres); - } else { - is.seekg(len, ios::cur); - } - - len = readLength(is); - - if (len == 1) { - char c; - is.read((char *)&c, sizeof(c)); - resolution_units = (IPP_RESOLUTION_UNITS)c; - } else { - is.seekg(len, ios::cur); - } - - return is; -} - -ostream &IppResolutionAttribute::output(ostream &os) const -{ - IppNamedAttribute::output(os); - - writeLength(os, 4); - unsigned long val = htonl(xres); - os.write((char *)&val, sizeof(val)); - - writeLength(os, 4); - val = htonl(yres); - os.write((char *)&val, sizeof(val)); - - writeLength(os, 1); - unsigned char c = (unsigned char)resolution_units; - os.write((char *)&c, sizeof(c)); - - return os; -} - -ostream &IppResolutionAttribute::print(ostream &os) const -{ - IppNamedAttribute::print(os); - os << '\t' << "Value(xres): " << dec << xres << '\n'; - os << '\t' << "Value(yres): " << dec << yres << '\n'; - os << '\t' << "Value(unit): " << dec << resolution_units << '\n'; - return os; -} - -/*----------------------------------------------------------------------*/ - -IppRangeOfIntegerAttribute::IppRangeOfIntegerAttribute(IPP_TAG t) - : IppNamedAttribute(t), lower(0), upper(0) -{ -} - -IppRangeOfIntegerAttribute::IppRangeOfIntegerAttribute(IPP_TAG t, const char *n, int l, int u) - : IppNamedAttribute(t, n), lower(l), upper(u) -{ -} - -int IppRangeOfIntegerAttribute::length() const -{ - return IppNamedAttribute::length() + 2 + 4 + 2 + 4; -} - -istream &IppRangeOfIntegerAttribute::input(istream &is) -{ - IppNamedAttribute::input(is); - - short len = readLength(is); - - if (0 < len && len <= 4) { - is.read((char *)&lower, sizeof(lower)); - lower = ntohl(lower); - } else { - is.seekg(len, ios::cur); - } - - len = readLength(is); - - if (0 < len && len <= 4) { - is.read((char *)&upper, sizeof(upper)); - upper = ntohl(upper); - } else { - is.seekg(len, ios::cur); - } - - return is; -} - -ostream &IppRangeOfIntegerAttribute::output(ostream &os) const -{ - IppNamedAttribute::output(os); - - writeLength(os, 4); - unsigned long val = htonl(lower); - os.write((char *)&val, sizeof(val)); - - writeLength(os, 4); - val = htonl(upper); - os.write((char *)&val, sizeof(val)); - - return os; -} - -ostream &IppRangeOfIntegerAttribute::print(ostream &os) const -{ - IppNamedAttribute::print(os); - os << '\t' << "Value(lower): " << dec << lower << '\n'; - os << '\t' << "Value(upper): " << dec << upper << '\n'; - return os; -} - -/*----------------------------------------------------------------------*/ - -IppContent::IppContent() -{ - version = 0x0100; - operation_id = IPP_GET_PRINTER_ATTRIBUTES; - request_id = 0x00000001; - - is = NULL; - size = -1; -} - -IppContent::~IppContent() -{ - for (list::const_iterator it = attrs.begin(); it != attrs.end(); it++) { - delete (*it); - } -} - -unsigned short IppContent::getVersion() const -{ - return version; -} - -void IppContent::setVersion(unsigned short i) -{ - version = i; -} - - -IPP_OPERATION_ID IppContent::getOperationId() const -{ - return (IPP_OPERATION_ID)operation_id; -} - -void IppContent::setOperationId(IPP_OPERATION_ID i) -{ - operation_id = i; -} - -IPP_STATUS_CODE IppContent::getStatusCode() const -{ - return (IPP_STATUS_CODE)operation_id; -} - -unsigned long IppContent::getRequestId() const -{ - return request_id; -} - -void IppContent::setRequestId(unsigned long i) -{ - request_id = i; -} - -istream &IppContent::input(istream &is) -{ - if (!is.read((char *)&version, sizeof(version))) { - return is; - } - - version = ntohs(version); - - if (!is.read((char *)&operation_id, sizeof(operation_id))) { - return is; - } - - operation_id = ntohs(operation_id); - - if (!is.read((char *)&request_id, sizeof(request_id))) { - return is; - } - - request_id = ntohl(request_id); - char tag; - - while (1) { - - if (!is.read((char *)&tag, sizeof(tag))) { - return is; - } - - if (tag <= 0x0F) { // delimiter - -// case IPP_OPERATION_ATTRIBUTES_TAG: -// case IPP_JOB_ATTRIBUTES_TAG: -// case IPP_END_OF_ATTRIBUTES_TAG: -// case IPP_PRINTER_ATTRIBUTES_TAG: -// case IPP_UNSUPPORTED_ATTRIBUTES_TAG: - - attrs.push_back(new IppAttribute((IPP_TAG)tag)); - if (tag == IPP_END_OF_ATTRIBUTES_TAG) { - break; - } - - } else if (tag <= 0x1F) { - - IppNoValueAttribute *attr = new IppNoValueAttribute((IPP_TAG)tag); - is >> *attr; - attrs.push_back(attr); - - } else if (tag <= 0x2F) { // integer values - - switch (tag) { - case IPP_INTEGER: - case IPP_ENUM: - { - IppIntegerAttribute *attr = new IppIntegerAttribute((IPP_TAG)tag); - is >> *attr; - attrs.push_back(attr); - } - break; - case IPP_BOOLEAN: - { - IppBooleanAttribute *attr = new IppBooleanAttribute((IPP_TAG)tag); - is >> *attr; - attrs.push_back(attr); - } - break; - default: - { - short len = readLength(is); - is.seekg(len, ios::cur); - len = readLength(is); - is.seekg(len, ios::cur); - } - break; - } - - } else if (tag <= 0x3F) { // octetString values - - switch (tag) { - case IPP_STRING: - { - IppStringAttribute *attr = new IppStringAttribute((IPP_TAG)tag); - is >> *attr; - attrs.push_back(attr); - } - break; - case IPP_DATETIME: - { - IppDatetimeAttribute *attr = new IppDatetimeAttribute((IPP_TAG)tag); - is >> *attr; - attrs.push_back(attr); - } - break; - case IPP_RESOLUTION: - { - IppResolutionAttribute *attr = new IppResolutionAttribute((IPP_TAG)tag); - is >> *attr; - attrs.push_back(attr); - } - break; - case IPP_RANGE_OF_INTEGER: - { - IppRangeOfIntegerAttribute *attr = new IppRangeOfIntegerAttribute((IPP_TAG)tag); - is >> *attr; - attrs.push_back(attr); - } - break; - case IPP_TEXT_WITH_LANGUAGE: - case IPP_NAME_WITH_LANGUAGE: - { - IppDoubleStringAttribute *attr = new IppDoubleStringAttribute((IPP_TAG)tag); - is >> *attr; - attrs.push_back(attr); - } - break; - default: - { - short len = readLength(is); - is.seekg(len, ios::cur); - len = readLength(is); - is.seekg(len, ios::cur); - } - break; - } - - } else if (tag <= 0x5F) { // character-string values - -// case IPP_TEXT_WITHOUT_LANGUAGE: -// case IPP_NAME_WITHOUT_LANGUAGE: -// case IPP_KEYWORD: -// case IPP_URI: -// case IPP_URISCHEME: -// case IPP_CHARSET: -// case IPP_NATURAL_LANGUAGE: -// case IPP_MIME_MEDIA_TYPE: - - IppStringAttribute *attr = new IppStringAttribute((IPP_TAG)tag); - is >> *attr; - attrs.push_back(attr); - } - } - return is; -} - -ostream &IppContent::output(ostream &os) const -{ - unsigned short ns_version = htons(version); // version-number - os.write((char *)&ns_version, sizeof(ns_version)); // version-number - - unsigned short ns_operation_id = htons(operation_id); // operation-id - os.write((char *)&ns_operation_id, sizeof(ns_operation_id)); // operation-id - - unsigned long ns_request_id = htonl(request_id); // request-id - os.write((char *)&ns_request_id, sizeof(ns_request_id)); // request-id - - for (list::const_iterator it = attrs.begin(); it != attrs.end(); it++) { - os << *(*it); - } - - ifstream ifs; - istream *iss = is; - if (iss == NULL) { - if (!file_path.empty()) { - ifs.open(file_path.c_str(), ios::in | ios::binary); - iss = &ifs; - } - } - if (iss && iss->good()) { - if (iss->good()) { - char c; - while (iss->get(c)) { - os.put(c); - } - } - } - - return os; -} - -void IppContent::setDelimiter(IPP_TAG tag) -{ - attrs.push_back(new IppAttribute(tag)); -} - -void IppContent::setInteger(const char *name, int value) -{ - attrs.push_back(new IppIntegerAttribute(IPP_INTEGER, name, value)); -} - -void IppContent::setBoolean(const char *name, bool value) -{ - attrs.push_back(new IppBooleanAttribute(IPP_BOOLEAN, name, value)); -} - -void IppContent::setString(const char *name, const char *value) -{ - attrs.push_back(new IppStringAttribute(IPP_STRING, name, value)); -} - -void IppContent::setDateTime(const char *name, const DATETIME *dt) -{ - attrs.push_back(new IppDatetimeAttribute(IPP_DATETIME, name, dt)); -} - -void IppContent::setResolution(const char *name, int x, int y, IPP_RESOLUTION_UNITS u) -{ - attrs.push_back(new IppResolutionAttribute(IPP_RESOLUTION, name, x, y, u)); -} - -void IppContent::setRangeOfInteger(const char *name, int lower, int upper) -{ - attrs.push_back(new IppRangeOfIntegerAttribute(IPP_RANGE_OF_INTEGER, name, lower, upper)); -} - -void IppContent::setTextWithLanguage(const char *name, const char *s1, const char *s2) -{ - attrs.push_back(new IppDoubleStringAttribute(IPP_TEXT_WITH_LANGUAGE, name, s1, s2)); -} - -void IppContent::setNameWithLanguage(const char *name, const char *s1, const char *s2) -{ - attrs.push_back(new IppDoubleStringAttribute(IPP_NAME_WITH_LANGUAGE, name, s1, s2)); -} - -void IppContent::setTextWithoutLanguage(const char *name, const char *value) -{ - attrs.push_back(new IppStringAttribute(IPP_TEXT_WITHOUT_LANGUAGE, name, value)); -} - -void IppContent::setNameWithoutLanguage(const char *name, const char *value) -{ - attrs.push_back(new IppStringAttribute(IPP_NAME_WITHOUT_LANGUAGE, name, value)); -} - -void IppContent::setKeyword(const char *name, const char *value) -{ - attrs.push_back(new IppStringAttribute(IPP_KEYWORD, name, value)); -} - -void IppContent::setURI(const char *name, const char *value) -{ - attrs.push_back(new IppStringAttribute(IPP_URI, name, value)); -} - -void IppContent::setURIScheme(const char *name, const char *value) -{ - attrs.push_back(new IppStringAttribute(IPP_URISCHEME, name, value)); -} - -void IppContent::setCharset(const char *name, const char *value) -{ - attrs.push_back(new IppStringAttribute(IPP_CHARSET, name, value)); -} - -void IppContent::setNaturalLanguage(const char *name, const char *value) -{ - attrs.push_back(new IppStringAttribute(IPP_NATURAL_LANGUAGE, name, value)); -} - -void IppContent::setMimeMediaType(const char *name, const char *value) -{ - attrs.push_back(new IppStringAttribute(IPP_MIME_MEDIA_TYPE, name, value)); -} - -int IppContent::length() const -{ - int length = 8; // sizeof(version-number + operation-id + request-id) - - for (list::const_iterator it = attrs.begin(); it != attrs.end(); it++) { - length += (*it)->length(); - } - - ifstream ifs; - istream *iss = is; - if (iss == NULL) { - if (!file_path.empty()) { - ifs.open(file_path.c_str(), ios::in | ios::binary); - iss = &ifs; - } - } - if (iss && iss->good()) { - int fsize = size; - if (fsize < 0) { - streampos pos = iss->tellg(); - iss->seekg(0, ios::end); - fsize = iss->tellg(); - iss->seekg(pos, ios::beg); - } - if (fsize > 0) { - length += fsize; - } - } - - return length; -} - -void IppContent::setRawData(const char *file, int n) -{ - file_path = file; - size = n; -} - -void IppContent::setRawData(istream &ifs, int n) -{ - is = &ifs; - size = n; -} - -ostream &IppContent::print(ostream &os) const -{ - os << "version: " << hex << version << '\n'; - os << "operation_id: " << hex << operation_id << '\n'; - os << "request_id: " << hex << request_id << '\n'; - - for (list::const_iterator it = attrs.begin(); it != attrs.end(); it++) { - (*it)->print(os); - } - - return os; -} - -bool IppContent::fail() const -{ - return !good(); -} - -bool IppContent::good() const -{ - return (IPP_SUCCESSFUL_OK_S <= operation_id) && (operation_id <= IPP_SUCCESSFUL_OK_E); -} - -bool IppContent::operator !() const -{ - return fail(); -} - -const char *IppContent::getStatusMessage() const -{ - if (good()) { - switch (operation_id) { - case IPP_SUCCESSFUL_OK: - return "successful-ok"; - case IPP_SUCCESSFUL_OK_IGNORED_OR_SUBSTITUTED_ATTRIBUTES: - return "successful-ok-ignored-or-substituted-attributes"; - case IPP_SUCCESSFUL_OK_CONFLICTING_ATTRIBUTES: - return "successful-ok-conflicting-attributes"; - default: - return "successful-ok-???"; - } - } else if (IPP_CLIENT_ERROR_S <= operation_id && operation_id <= IPP_CLIENT_ERROR_E) { - switch (operation_id) { - case IPP_CLIENT_ERROR_BAD_REQUEST: - return "client-error-bad-request"; - case IPP_CLIENT_ERROR_FORBIDDEN: - return "client-error-forbidden"; - case IPP_CLIENT_ERROR_NOT_AUTHENTICATED: - return "client-error-not-authenticated"; - case IPP_CLIENT_ERROR_NOT_AUTHORIZED: - return "client-error-not-authorized"; - case IPP_CLIENT_ERROR_NOT_POSSIBLE: - return "client-error-not-possible"; - case IPP_CLIENT_ERROR_TIMEOUT: - return "client-error-timeout"; - case IPP_CLIENT_ERROR_NOT_FOUND: - return "client-error-not-found"; - case IPP_CLIENT_ERROR_GONE: - return "client-error-gone"; - case IPP_CLIENT_ERROR_REQUEST_ENTITY_TOO_LARGE: - return "client-error-request-entity-too-large"; - case IPP_CLIENT_ERROR_REQUEST_VALUE_TOO_LONG: - return "client-error-request-value-too-long"; - case IPP_CLIENT_ERROR_DOCUMENT_FORMAT_NOT_SUPPORTED: - return "client-error-document-format-not-supported"; - case IPP_CLIENT_ERROR_ATTRIBUTES_OR_VALUES_NOT_SUPPORTED: - return "client-error-attributes-or-values-not-supported"; - case IPP_CLIENT_ERROR_URI_SCHEME_NOT_SUPPORTED: - return "client-error-uri-scheme-not-supported"; - case IPP_CLIENT_ERROR_CHARSET_NOT_SUPPORTED: - return "client-error-charset-not-supported"; - case IPP_CLIENT_ERROR_CONFLICTING_ATTRIBUTES: - return "client-error-conflicting-attributes"; - default: - return "client-error-???"; - } - } else if (IPP_SERVER_ERROR_S <= operation_id && operation_id <= IPP_SERVER_ERROR_E) { - switch (operation_id) { - case IPP_SERVER_ERROR_INTERNAL_ERROR: - return "server-error-internal-error"; - case IPP_SERVER_ERROR_OPERATION_NOT_SUPPORTED: - return "server-error-operation-not-supported"; - case IPP_SERVER_ERROR_SERVICE_UNAVAILABLE: - return "server-error-service-unavailable"; - case IPP_SERVER_ERROR_VERSION_NOT_SUPPORTED: - return "server-error-version-not-supported"; - case IPP_SERVER_ERROR_DEVICE_ERROR: - return "server-error-device-error"; - case IPP_SERVER_ERROR_TEMPORARY_ERROR: - return "server-error-temporary-error"; - case IPP_SERVER_ERROR_NOT_ACCEPTING_JOBS: - return "server-error-not-accepting-jobs"; - case IPP_SERVER_ERROR_BUSY: - return "server-error-busy"; - case IPP_SERVER_ERROR_JOB_CANCELED: - return "server-error-job-canceled"; - default: - return "server-error-???"; - } - } else { - return "unknown error."; - } -} +// Sun, 18 Jun 2000 +// Y.Takagi + +#ifdef WIN32 +# include +#elif defined(__HAIKU__) +# include +# include +#else +# include +#endif + +#include +#include +#include + +#include "IppContent.h" + +/*----------------------------------------------------------------------*/ + +short readLength(istream &is) +{ + short len = 0; + is.read((char *)&len, sizeof(short)); + len = ntohs(len); + return len; +} + +void writeLength(ostream &os, short len) +{ + len = htons(len); + os.write((char *)&len, sizeof(short)); +} + +/*----------------------------------------------------------------------*/ + +DATETIME::DATETIME() +{ + memset(this, 0, sizeof(DATETIME)); +} + +DATETIME::DATETIME(const DATETIME &dt) +{ + memcpy(this, &dt.datetime, sizeof(DATETIME)); +} + +DATETIME & DATETIME::operator = (const DATETIME &dt) +{ + memcpy(this, &dt.datetime, sizeof(DATETIME)); + return *this; +} + +istream& operator >> (istream &is, DATETIME &attr) +{ + return is; +} + +ostream& operator << (ostream &os, const DATETIME &attr) +{ + return os; +} + + +/*----------------------------------------------------------------------*/ + +IppAttribute::IppAttribute(IPP_TAG t) + : tag(t) +{ +} + +int IppAttribute::length() const +{ + return 1; +} + +istream &IppAttribute::input(istream &is) +{ + return is; +} + +ostream &IppAttribute::output(ostream &os) const +{ + os << (unsigned char)tag; + return os; +} + +ostream &IppAttribute::print(ostream &os) const +{ + os << "Tag: " << hex << (int)tag << '\n'; + return os; +} + +/*----------------------------------------------------------------------*/ + +IppNamedAttribute::IppNamedAttribute(IPP_TAG t) + : IppAttribute(t) +{ +} + +IppNamedAttribute::IppNamedAttribute(IPP_TAG t, const char *s) + : IppAttribute(t), name(s ? s : "") +{ +} + +int IppNamedAttribute::length() const +{ + return IppAttribute::length() + 2 + name.length(); +} + +istream &IppNamedAttribute::input(istream &is) +{ + short len = readLength(is); + + if (0 < len) { + char *buffer = new char[len + 1]; + is.read(buffer, len); + buffer[len] = '\0'; + name = buffer; + delete [] buffer; + } + + return is; +} + +ostream &IppNamedAttribute::output(ostream &os) const +{ + IppAttribute::output(os); + + writeLength(os, name.length()); + os << name; + + return os; +} + +ostream &IppNamedAttribute::print(ostream &os) const +{ + IppAttribute::print(os); + os << '\t' << "Name: " << name << '\n'; + return os; +} + +/*----------------------------------------------------------------------*/ + +IppNoValueAttribute::IppNoValueAttribute(IPP_TAG t) + : IppNamedAttribute(t) +{ +} + +IppNoValueAttribute::IppNoValueAttribute(IPP_TAG t, const char *n) + : IppNamedAttribute(t, n) +{ +} + +int IppNoValueAttribute::length() const +{ + return IppAttribute::length() + 2; +} + +istream &IppNoValueAttribute::input(istream &is) +{ + IppNamedAttribute::input(is); + + short len = readLength(is); + + if (0 < len) { + is.seekg(len, ios::cur); + } + + return is; +} + +ostream &IppNoValueAttribute::output(ostream &os) const +{ + IppAttribute::output(os); + + writeLength(os, 0); + + return os; +} + +ostream &IppNoValueAttribute::print(ostream &os) const +{ + return IppNamedAttribute::print(os); +} + +/*----------------------------------------------------------------------*/ + +IppIntegerAttribute::IppIntegerAttribute(IPP_TAG t) + : IppNamedAttribute(t), value(0) +{ +} + +IppIntegerAttribute::IppIntegerAttribute(IPP_TAG t, const char *n, int v) + : IppNamedAttribute(t, n), value(v) +{ +} + +int IppIntegerAttribute::length() const +{ + return IppNamedAttribute::length() + 2 + 4; +} + +istream &IppIntegerAttribute::input(istream &is) +{ + IppNamedAttribute::input(is); + + short len = readLength(is); + + if (0 < len && len <= 4) { + is.read((char *)&value, sizeof(value)); + value = ntohl(value); + } else { + is.seekg(len, ios::cur); + } + + return is; +} + +ostream &IppIntegerAttribute::output(ostream &os) const +{ + IppNamedAttribute::output(os); + + writeLength(os, 4); + unsigned long val = htonl(value); + os.write((char *)&val, sizeof(val)); + return os; +} + +ostream &IppIntegerAttribute::print(ostream &os) const +{ + IppNamedAttribute::print(os); + os << '\t' << "Value: " << dec << value << '\n'; + return os; +} + +/*----------------------------------------------------------------------*/ + +IppBooleanAttribute::IppBooleanAttribute(IPP_TAG t) + : IppNamedAttribute(t), value(false) +{ +} + +IppBooleanAttribute::IppBooleanAttribute(IPP_TAG t, const char *n, bool f) + : IppNamedAttribute(t, n), value(f) +{ +} + +int IppBooleanAttribute::length() const +{ + return IppNamedAttribute::length() + 2 + 1; +} + + +istream &IppBooleanAttribute::input(istream &is) +{ + IppNamedAttribute::input(is); + + short len = readLength(is); + + if (0 < len && len <= 1) { + char c; + is.read((char *)&c, sizeof(c)); + value = c ? true : false; + } else { + is.seekg(len, ios::cur); + } + + return is; +} + +ostream &IppBooleanAttribute::output(ostream &os) const +{ + IppNamedAttribute::output(os); + + writeLength(os, 1); + char c = (char)value; + os.write((char *)&c, sizeof(c)); + + return os; +} + +ostream &IppBooleanAttribute::print(ostream &os) const +{ + IppNamedAttribute::print(os); + os << '\t' << "Value: " << value << '\n'; + return os; +} + +/*----------------------------------------------------------------------*/ + +IppDatetimeAttribute::IppDatetimeAttribute(IPP_TAG t) + : IppNamedAttribute(t) +{ +} + +IppDatetimeAttribute::IppDatetimeAttribute(IPP_TAG t, const char *n, const DATETIME *dt) + : IppNamedAttribute(t, n), datetime(*dt) +{ +} + +int IppDatetimeAttribute::length() const +{ + return IppNamedAttribute::length() + 2 + 11; +} + +istream &IppDatetimeAttribute::input(istream &is) +{ + IppNamedAttribute::input(is); + + short len = readLength(is); + + if (0 < len) { + if (len == 11) { + is >> datetime; + } else { + is.seekg(len, ios::cur); + } + } + + return is; +} + +ostream &IppDatetimeAttribute::output(ostream &os) const +{ + IppNamedAttribute::output(os); + + writeLength(os, 11); + os << datetime; + + return os; +} + +ostream &IppDatetimeAttribute::print(ostream &os) const +{ + IppNamedAttribute::print(os); + os << '\t' << "Value(DateTime): " << datetime << '\n'; + return os; +} + +/*----------------------------------------------------------------------*/ + +IppStringAttribute::IppStringAttribute(IPP_TAG t) + : IppNamedAttribute(t) +{ +} + +IppStringAttribute::IppStringAttribute(IPP_TAG t, const char *n, const char *s) +: IppNamedAttribute(t, n), text(s ? s : "") +{ +} + +int IppStringAttribute::length() const +{ + return IppNamedAttribute::length() + 2 + text.length(); +} + +istream &IppStringAttribute::input(istream &is) +{ + IppNamedAttribute::input(is); + + short len = readLength(is); + + if (0 < len) { + char *buffer = new char[len + 1]; + is.read(buffer, len); + buffer[len] = '\0'; + text = buffer; + delete [] buffer; + } + + return is; +} + +ostream &IppStringAttribute::output(ostream &os) const +{ + IppNamedAttribute::output(os); + + writeLength(os, text.length()); + os << text; + + return os; +} + +ostream &IppStringAttribute::print(ostream &os) const +{ + IppNamedAttribute::print(os); + os << '\t' << "Value: " << text << '\n'; + return os; +} + +/*----------------------------------------------------------------------*/ + +IppDoubleStringAttribute::IppDoubleStringAttribute(IPP_TAG t) + : IppNamedAttribute(t) +{ +} + +IppDoubleStringAttribute::IppDoubleStringAttribute(IPP_TAG t, const char *n, const char *s1, const char *s2) +: IppNamedAttribute(t, n), text1(s1 ? s1 : ""), text2(s2 ? s2 : "") +{ +} + +int IppDoubleStringAttribute::length() const +{ + return IppNamedAttribute::length() + 2 + text1.length() + 2 + text2.length(); +} + +istream &IppDoubleStringAttribute::input(istream &is) +{ + IppNamedAttribute::input(is); + + short len = readLength(is); + + if (0 < len) { + char *buffer = new char[len + 1]; + is.read(buffer, len); + buffer[len] = '\0'; + text1 = buffer; + delete [] buffer; + } + + len = readLength(is); + + if (0 < len) { + char *buffer = new char[len + 1]; + is.read(buffer, len); + buffer[len] = '\0'; + text2 = buffer; + delete [] buffer; + } + + return is; +} + +ostream &IppDoubleStringAttribute::output(ostream &os) const +{ + IppNamedAttribute::output(os); + + writeLength(os, text1.length()); + os << text1; + + writeLength(os, text2.length()); + os << text2; + + return os; +} + +ostream &IppDoubleStringAttribute::print(ostream &os) const +{ + IppNamedAttribute::print(os); + os << '\t' << "Value1: " << text1 << '\n'; + os << '\t' << "Value2: " << text2 << '\n'; + return os; +} + +/*----------------------------------------------------------------------*/ + +IppResolutionAttribute::IppResolutionAttribute(IPP_TAG t) + : IppNamedAttribute(t), xres(0), yres(0), resolution_units((IPP_RESOLUTION_UNITS)0) +{ +} + +IppResolutionAttribute::IppResolutionAttribute(IPP_TAG t, const char *n, int x, int y, IPP_RESOLUTION_UNITS u) + : IppNamedAttribute(t, n), xres(x), yres(y), resolution_units(u) +{ +} + +int IppResolutionAttribute::length() const +{ + return IppNamedAttribute::length() + 2 + 4 + 2 + 4 + 2 + 1; +} + +istream &IppResolutionAttribute::input(istream &is) +{ + IppNamedAttribute::input(is); + + short len = readLength(is); + + if (0 < len && len <= 4) { + is.read((char *)&xres, sizeof(xres)); + xres = ntohl(xres); + } else { + is.seekg(len, ios::cur); + } + + len = readLength(is); + + if (0 < len && len <= 4) { + is.read((char *)&yres, sizeof(yres)); + yres = ntohl(yres); + } else { + is.seekg(len, ios::cur); + } + + len = readLength(is); + + if (len == 1) { + char c; + is.read((char *)&c, sizeof(c)); + resolution_units = (IPP_RESOLUTION_UNITS)c; + } else { + is.seekg(len, ios::cur); + } + + return is; +} + +ostream &IppResolutionAttribute::output(ostream &os) const +{ + IppNamedAttribute::output(os); + + writeLength(os, 4); + unsigned long val = htonl(xres); + os.write((char *)&val, sizeof(val)); + + writeLength(os, 4); + val = htonl(yres); + os.write((char *)&val, sizeof(val)); + + writeLength(os, 1); + unsigned char c = (unsigned char)resolution_units; + os.write((char *)&c, sizeof(c)); + + return os; +} + +ostream &IppResolutionAttribute::print(ostream &os) const +{ + IppNamedAttribute::print(os); + os << '\t' << "Value(xres): " << dec << xres << '\n'; + os << '\t' << "Value(yres): " << dec << yres << '\n'; + os << '\t' << "Value(unit): " << dec << resolution_units << '\n'; + return os; +} + +/*----------------------------------------------------------------------*/ + +IppRangeOfIntegerAttribute::IppRangeOfIntegerAttribute(IPP_TAG t) + : IppNamedAttribute(t), lower(0), upper(0) +{ +} + +IppRangeOfIntegerAttribute::IppRangeOfIntegerAttribute(IPP_TAG t, const char *n, int l, int u) + : IppNamedAttribute(t, n), lower(l), upper(u) +{ +} + +int IppRangeOfIntegerAttribute::length() const +{ + return IppNamedAttribute::length() + 2 + 4 + 2 + 4; +} + +istream &IppRangeOfIntegerAttribute::input(istream &is) +{ + IppNamedAttribute::input(is); + + short len = readLength(is); + + if (0 < len && len <= 4) { + is.read((char *)&lower, sizeof(lower)); + lower = ntohl(lower); + } else { + is.seekg(len, ios::cur); + } + + len = readLength(is); + + if (0 < len && len <= 4) { + is.read((char *)&upper, sizeof(upper)); + upper = ntohl(upper); + } else { + is.seekg(len, ios::cur); + } + + return is; +} + +ostream &IppRangeOfIntegerAttribute::output(ostream &os) const +{ + IppNamedAttribute::output(os); + + writeLength(os, 4); + unsigned long val = htonl(lower); + os.write((char *)&val, sizeof(val)); + + writeLength(os, 4); + val = htonl(upper); + os.write((char *)&val, sizeof(val)); + + return os; +} + +ostream &IppRangeOfIntegerAttribute::print(ostream &os) const +{ + IppNamedAttribute::print(os); + os << '\t' << "Value(lower): " << dec << lower << '\n'; + os << '\t' << "Value(upper): " << dec << upper << '\n'; + return os; +} + +/*----------------------------------------------------------------------*/ + +IppContent::IppContent() +{ + version = 0x0100; + operation_id = IPP_GET_PRINTER_ATTRIBUTES; + request_id = 0x00000001; + + is = NULL; + size = -1; +} + +IppContent::~IppContent() +{ + for (list::const_iterator it = attrs.begin(); it != attrs.end(); it++) { + delete (*it); + } +} + +unsigned short IppContent::getVersion() const +{ + return version; +} + +void IppContent::setVersion(unsigned short i) +{ + version = i; +} + + +IPP_OPERATION_ID IppContent::getOperationId() const +{ + return (IPP_OPERATION_ID)operation_id; +} + +void IppContent::setOperationId(IPP_OPERATION_ID i) +{ + operation_id = i; +} + +IPP_STATUS_CODE IppContent::getStatusCode() const +{ + return (IPP_STATUS_CODE)operation_id; +} + +unsigned long IppContent::getRequestId() const +{ + return request_id; +} + +void IppContent::setRequestId(unsigned long i) +{ + request_id = i; +} + +istream &IppContent::input(istream &is) +{ + if (!is.read((char *)&version, sizeof(version))) { + return is; + } + + version = ntohs(version); + + if (!is.read((char *)&operation_id, sizeof(operation_id))) { + return is; + } + + operation_id = ntohs(operation_id); + + if (!is.read((char *)&request_id, sizeof(request_id))) { + return is; + } + + request_id = ntohl(request_id); + char tag; + + while (1) { + + if (!is.read((char *)&tag, sizeof(tag))) { + return is; + } + + if (tag <= 0x0F) { // delimiter + +// case IPP_OPERATION_ATTRIBUTES_TAG: +// case IPP_JOB_ATTRIBUTES_TAG: +// case IPP_END_OF_ATTRIBUTES_TAG: +// case IPP_PRINTER_ATTRIBUTES_TAG: +// case IPP_UNSUPPORTED_ATTRIBUTES_TAG: + + attrs.push_back(new IppAttribute((IPP_TAG)tag)); + if (tag == IPP_END_OF_ATTRIBUTES_TAG) { + break; + } + + } else if (tag <= 0x1F) { + + IppNoValueAttribute *attr = new IppNoValueAttribute((IPP_TAG)tag); + is >> *attr; + attrs.push_back(attr); + + } else if (tag <= 0x2F) { // integer values + + switch (tag) { + case IPP_INTEGER: + case IPP_ENUM: + { + IppIntegerAttribute *attr = new IppIntegerAttribute((IPP_TAG)tag); + is >> *attr; + attrs.push_back(attr); + } + break; + case IPP_BOOLEAN: + { + IppBooleanAttribute *attr = new IppBooleanAttribute((IPP_TAG)tag); + is >> *attr; + attrs.push_back(attr); + } + break; + default: + { + short len = readLength(is); + is.seekg(len, ios::cur); + len = readLength(is); + is.seekg(len, ios::cur); + } + break; + } + + } else if (tag <= 0x3F) { // octetString values + + switch (tag) { + case IPP_STRING: + { + IppStringAttribute *attr = new IppStringAttribute((IPP_TAG)tag); + is >> *attr; + attrs.push_back(attr); + } + break; + case IPP_DATETIME: + { + IppDatetimeAttribute *attr = new IppDatetimeAttribute((IPP_TAG)tag); + is >> *attr; + attrs.push_back(attr); + } + break; + case IPP_RESOLUTION: + { + IppResolutionAttribute *attr = new IppResolutionAttribute((IPP_TAG)tag); + is >> *attr; + attrs.push_back(attr); + } + break; + case IPP_RANGE_OF_INTEGER: + { + IppRangeOfIntegerAttribute *attr = new IppRangeOfIntegerAttribute((IPP_TAG)tag); + is >> *attr; + attrs.push_back(attr); + } + break; + case IPP_TEXT_WITH_LANGUAGE: + case IPP_NAME_WITH_LANGUAGE: + { + IppDoubleStringAttribute *attr = new IppDoubleStringAttribute((IPP_TAG)tag); + is >> *attr; + attrs.push_back(attr); + } + break; + default: + { + short len = readLength(is); + is.seekg(len, ios::cur); + len = readLength(is); + is.seekg(len, ios::cur); + } + break; + } + + } else if (tag <= 0x5F) { // character-string values + +// case IPP_TEXT_WITHOUT_LANGUAGE: +// case IPP_NAME_WITHOUT_LANGUAGE: +// case IPP_KEYWORD: +// case IPP_URI: +// case IPP_URISCHEME: +// case IPP_CHARSET: +// case IPP_NATURAL_LANGUAGE: +// case IPP_MIME_MEDIA_TYPE: + + IppStringAttribute *attr = new IppStringAttribute((IPP_TAG)tag); + is >> *attr; + attrs.push_back(attr); + } + } + return is; +} + +ostream &IppContent::output(ostream &os) const +{ + unsigned short ns_version = htons(version); // version-number + os.write((char *)&ns_version, sizeof(ns_version)); // version-number + + unsigned short ns_operation_id = htons(operation_id); // operation-id + os.write((char *)&ns_operation_id, sizeof(ns_operation_id)); // operation-id + + unsigned long ns_request_id = htonl(request_id); // request-id + os.write((char *)&ns_request_id, sizeof(ns_request_id)); // request-id + + for (list::const_iterator it = attrs.begin(); it != attrs.end(); it++) { + os << *(*it); + } + + ifstream ifs; + istream *iss = is; + if (iss == NULL) { + if (!file_path.empty()) { + ifs.open(file_path.c_str(), ios::in | ios::binary); + iss = &ifs; + } + } + if (iss && iss->good()) { + if (iss->good()) { + char c; + while (iss->get(c)) { + os.put(c); + } + } + } + + return os; +} + +void IppContent::setDelimiter(IPP_TAG tag) +{ + attrs.push_back(new IppAttribute(tag)); +} + +void IppContent::setInteger(const char *name, int value) +{ + attrs.push_back(new IppIntegerAttribute(IPP_INTEGER, name, value)); +} + +void IppContent::setBoolean(const char *name, bool value) +{ + attrs.push_back(new IppBooleanAttribute(IPP_BOOLEAN, name, value)); +} + +void IppContent::setString(const char *name, const char *value) +{ + attrs.push_back(new IppStringAttribute(IPP_STRING, name, value)); +} + +void IppContent::setDateTime(const char *name, const DATETIME *dt) +{ + attrs.push_back(new IppDatetimeAttribute(IPP_DATETIME, name, dt)); +} + +void IppContent::setResolution(const char *name, int x, int y, IPP_RESOLUTION_UNITS u) +{ + attrs.push_back(new IppResolutionAttribute(IPP_RESOLUTION, name, x, y, u)); +} + +void IppContent::setRangeOfInteger(const char *name, int lower, int upper) +{ + attrs.push_back(new IppRangeOfIntegerAttribute(IPP_RANGE_OF_INTEGER, name, lower, upper)); +} + +void IppContent::setTextWithLanguage(const char *name, const char *s1, const char *s2) +{ + attrs.push_back(new IppDoubleStringAttribute(IPP_TEXT_WITH_LANGUAGE, name, s1, s2)); +} + +void IppContent::setNameWithLanguage(const char *name, const char *s1, const char *s2) +{ + attrs.push_back(new IppDoubleStringAttribute(IPP_NAME_WITH_LANGUAGE, name, s1, s2)); +} + +void IppContent::setTextWithoutLanguage(const char *name, const char *value) +{ + attrs.push_back(new IppStringAttribute(IPP_TEXT_WITHOUT_LANGUAGE, name, value)); +} + +void IppContent::setNameWithoutLanguage(const char *name, const char *value) +{ + attrs.push_back(new IppStringAttribute(IPP_NAME_WITHOUT_LANGUAGE, name, value)); +} + +void IppContent::setKeyword(const char *name, const char *value) +{ + attrs.push_back(new IppStringAttribute(IPP_KEYWORD, name, value)); +} + +void IppContent::setURI(const char *name, const char *value) +{ + attrs.push_back(new IppStringAttribute(IPP_URI, name, value)); +} + +void IppContent::setURIScheme(const char *name, const char *value) +{ + attrs.push_back(new IppStringAttribute(IPP_URISCHEME, name, value)); +} + +void IppContent::setCharset(const char *name, const char *value) +{ + attrs.push_back(new IppStringAttribute(IPP_CHARSET, name, value)); +} + +void IppContent::setNaturalLanguage(const char *name, const char *value) +{ + attrs.push_back(new IppStringAttribute(IPP_NATURAL_LANGUAGE, name, value)); +} + +void IppContent::setMimeMediaType(const char *name, const char *value) +{ + attrs.push_back(new IppStringAttribute(IPP_MIME_MEDIA_TYPE, name, value)); +} + +int IppContent::length() const +{ + int length = 8; // sizeof(version-number + operation-id + request-id) + + for (list::const_iterator it = attrs.begin(); it != attrs.end(); it++) { + length += (*it)->length(); + } + + ifstream ifs; + istream *iss = is; + if (iss == NULL) { + if (!file_path.empty()) { + ifs.open(file_path.c_str(), ios::in | ios::binary); + iss = &ifs; + } + } + if (iss && iss->good()) { + int fsize = size; + if (fsize < 0) { + streampos pos = iss->tellg(); + iss->seekg(0, ios::end); + fsize = iss->tellg(); + iss->seekg(pos, ios::beg); + } + if (fsize > 0) { + length += fsize; + } + } + + return length; +} + +void IppContent::setRawData(const char *file, int n) +{ + file_path = file; + size = n; +} + +void IppContent::setRawData(istream &ifs, int n) +{ + is = &ifs; + size = n; +} + +ostream &IppContent::print(ostream &os) const +{ + os << "version: " << hex << version << '\n'; + os << "operation_id: " << hex << operation_id << '\n'; + os << "request_id: " << hex << request_id << '\n'; + + for (list::const_iterator it = attrs.begin(); it != attrs.end(); it++) { + (*it)->print(os); + } + + return os; +} + +bool IppContent::fail() const +{ + return !good(); +} + +bool IppContent::good() const +{ + return /*operation_id >= IPP_SUCCESSFUL_OK_S &&*/ operation_id <= IPP_SUCCESSFUL_OK_E; +} + +bool IppContent::operator !() const +{ + return fail(); +} + +const char *IppContent::getStatusMessage() const +{ + if (good()) { + switch (operation_id) { + case IPP_SUCCESSFUL_OK: + return "successful-ok"; + case IPP_SUCCESSFUL_OK_IGNORED_OR_SUBSTITUTED_ATTRIBUTES: + return "successful-ok-ignored-or-substituted-attributes"; + case IPP_SUCCESSFUL_OK_CONFLICTING_ATTRIBUTES: + return "successful-ok-conflicting-attributes"; + default: + return "successful-ok-???"; + } + } else if (IPP_CLIENT_ERROR_S <= operation_id && operation_id <= IPP_CLIENT_ERROR_E) { + switch (operation_id) { + case IPP_CLIENT_ERROR_BAD_REQUEST: + return "client-error-bad-request"; + case IPP_CLIENT_ERROR_FORBIDDEN: + return "client-error-forbidden"; + case IPP_CLIENT_ERROR_NOT_AUTHENTICATED: + return "client-error-not-authenticated"; + case IPP_CLIENT_ERROR_NOT_AUTHORIZED: + return "client-error-not-authorized"; + case IPP_CLIENT_ERROR_NOT_POSSIBLE: + return "client-error-not-possible"; + case IPP_CLIENT_ERROR_TIMEOUT: + return "client-error-timeout"; + case IPP_CLIENT_ERROR_NOT_FOUND: + return "client-error-not-found"; + case IPP_CLIENT_ERROR_GONE: + return "client-error-gone"; + case IPP_CLIENT_ERROR_REQUEST_ENTITY_TOO_LARGE: + return "client-error-request-entity-too-large"; + case IPP_CLIENT_ERROR_REQUEST_VALUE_TOO_LONG: + return "client-error-request-value-too-long"; + case IPP_CLIENT_ERROR_DOCUMENT_FORMAT_NOT_SUPPORTED: + return "client-error-document-format-not-supported"; + case IPP_CLIENT_ERROR_ATTRIBUTES_OR_VALUES_NOT_SUPPORTED: + return "client-error-attributes-or-values-not-supported"; + case IPP_CLIENT_ERROR_URI_SCHEME_NOT_SUPPORTED: + return "client-error-uri-scheme-not-supported"; + case IPP_CLIENT_ERROR_CHARSET_NOT_SUPPORTED: + return "client-error-charset-not-supported"; + case IPP_CLIENT_ERROR_CONFLICTING_ATTRIBUTES: + return "client-error-conflicting-attributes"; + default: + return "client-error-???"; + } + } else if (IPP_SERVER_ERROR_S <= operation_id && operation_id <= IPP_SERVER_ERROR_E) { + switch (operation_id) { + case IPP_SERVER_ERROR_INTERNAL_ERROR: + return "server-error-internal-error"; + case IPP_SERVER_ERROR_OPERATION_NOT_SUPPORTED: + return "server-error-operation-not-supported"; + case IPP_SERVER_ERROR_SERVICE_UNAVAILABLE: + return "server-error-service-unavailable"; + case IPP_SERVER_ERROR_VERSION_NOT_SUPPORTED: + return "server-error-version-not-supported"; + case IPP_SERVER_ERROR_DEVICE_ERROR: + return "server-error-device-error"; + case IPP_SERVER_ERROR_TEMPORARY_ERROR: + return "server-error-temporary-error"; + case IPP_SERVER_ERROR_NOT_ACCEPTING_JOBS: + return "server-error-not-accepting-jobs"; + case IPP_SERVER_ERROR_BUSY: + return "server-error-busy"; + case IPP_SERVER_ERROR_JOB_CANCELED: + return "server-error-job-canceled"; + default: + return "server-error-???"; + } + } else { + return "unknown error."; + } +} diff --git a/src/add-ons/print/transports/ipp/IppTransport.cpp b/src/add-ons/print/transports/ipp/IppTransport.cpp index 716f1cc4c4..d96cf8245e 100644 --- a/src/add-ons/print/transports/ipp/IppTransport.cpp +++ b/src/add-ons/print/transports/ipp/IppTransport.cpp @@ -1,11 +1,12 @@ // Sun, 18 Jun 2000 // Y.Takagi +#include #include #include #include -#include -#include + +#include #include #include @@ -56,10 +57,11 @@ IppTransport::IppTransport(BMessage *msg) } dir.WriteAttr(IPP_JOB_ID, B_INT32_TYPE, 0, &__jobid, sizeof(__jobid)); - getusername(__user, sizeof(__user)); - if (__user[0] == '\0') { + struct passwd *pwd = getpwuid(geteuid()); + if (pwd != NULL && pwd->pw_name != NULL && pwd->pw_name[0]) + strcpy(__user, pwd->pw_name); + else strcpy(__user, "baron"); - } sprintf(__file, "%s/%s@ipp.%ld", spool_path, __user, __jobid); diff --git a/src/add-ons/print/transports/ipp/IppURLConnection.cpp b/src/add-ons/print/transports/ipp/IppURLConnection.cpp index a210da2b8a..6df420a2ae 100644 --- a/src/add-ons/print/transports/ipp/IppURLConnection.cpp +++ b/src/add-ons/print/transports/ipp/IppURLConnection.cpp @@ -1,108 +1,112 @@ -// Sun, 18 Jun 2000 -// Y.Takagi - -#ifdef WIN32 -#include -#include -#include -#else -#include -#include -#include -#include -char *itoa(int i, char *buf, int unit) -{ - sprintf(buf, "%d", i); - return buf; -} -#define stricmp strcasecmp -#define strnicmp strncasecmp -#endif // WIN32 - -#include -#include "IppURLConnection.h" -#include "IppContent.h" - -IppURLConnection::IppURLConnection(const URL &Url) - : HttpURLConnection(Url) -{ - __ippRequest = NULL; - __ippResponse = new IppContent; - setRequestMethod("POST"); - setRequestProperty("Content-Type", "application/ipp"); - setRequestProperty("Cache-Control", "no-cache"); - setRequestProperty("Pragma", "no-cache"); -} - -IppURLConnection::~IppURLConnection() -{ - if (__ippRequest) { - delete __ippRequest; - } - - if (__ippResponse) { - delete __ippResponse; - } -} - -void IppURLConnection::setIppRequest(IppContent *obj) -{ - if (__ippRequest) { - delete __ippRequest; - } - __ippRequest = obj; -} - - -const IppContent *IppURLConnection::getIppResponse() const -{ - return __ippResponse; -} - -void IppURLConnection::setRequest() -{ - if (connected) { - char buf[64]; - itoa(__ippRequest->length(), buf, 10); - setRequestProperty("Content-Length", buf); - HttpURLConnection::setRequest(); - } -} - -void IppURLConnection::setContent() -{ - if (connected && __ippRequest) { - ostream &os = getOutputStream(); - os << *__ippRequest; - } -} - -inline bool is_contenttype_ipp(const char *s) -{ - return strnicmp(s, "application/ipp", 15) ? false : true; -} - -void IppURLConnection::getContent() -{ - if (connected) { - if (getResponseCode() == HTTP_OK && is_contenttype_ipp(getContentType())) { - istream &is = getInputStream(); - is >> *__ippResponse; - } else { - HttpURLConnection::getContent(); - } - } -} - -ostream &IppURLConnection::printIppRequest(ostream &os) -{ - return __ippRequest->print(os); -} - -ostream &IppURLConnection::printIppResponse(ostream &os) -{ - if (getResponseCode() == HTTP_OK && is_contenttype_ipp(getContentType())) { - return __ippResponse->print(os); - } - return os; -} +// Sun, 18 Jun 2000 +// Y.Takagi + +#ifdef WIN32 +#include +#include +#include +#else +#ifdef __HAIKU__ +# include +#else +# include +#endif +#include +#include +#include +char *itoa(int i, char *buf, int unit) +{ + sprintf(buf, "%d", i); + return buf; +} +#define stricmp strcasecmp +#define strnicmp strncasecmp +#endif // WIN32 + +#include +#include "IppURLConnection.h" +#include "IppContent.h" + +IppURLConnection::IppURLConnection(const URL &Url) + : HttpURLConnection(Url) +{ + __ippRequest = NULL; + __ippResponse = new IppContent; + setRequestMethod("POST"); + setRequestProperty("Content-Type", "application/ipp"); + setRequestProperty("Cache-Control", "no-cache"); + setRequestProperty("Pragma", "no-cache"); +} + +IppURLConnection::~IppURLConnection() +{ + if (__ippRequest) { + delete __ippRequest; + } + + if (__ippResponse) { + delete __ippResponse; + } +} + +void IppURLConnection::setIppRequest(IppContent *obj) +{ + if (__ippRequest) { + delete __ippRequest; + } + __ippRequest = obj; +} + + +const IppContent *IppURLConnection::getIppResponse() const +{ + return __ippResponse; +} + +void IppURLConnection::setRequest() +{ + if (connected) { + char buf[64]; + itoa(__ippRequest->length(), buf, 10); + setRequestProperty("Content-Length", buf); + HttpURLConnection::setRequest(); + } +} + +void IppURLConnection::setContent() +{ + if (connected && __ippRequest) { + ostream &os = getOutputStream(); + os << *__ippRequest; + } +} + +inline bool is_contenttype_ipp(const char *s) +{ + return strnicmp(s, "application/ipp", 15) ? false : true; +} + +void IppURLConnection::getContent() +{ + if (connected) { + if (getResponseCode() == HTTP_OK && is_contenttype_ipp(getContentType())) { + istream &is = getInputStream(); + is >> *__ippResponse; + } else { + HttpURLConnection::getContent(); + } + } +} + +ostream &IppURLConnection::printIppRequest(ostream &os) +{ + return __ippRequest->print(os); +} + +ostream &IppURLConnection::printIppResponse(ostream &os) +{ + if (getResponseCode() == HTTP_OK && is_contenttype_ipp(getContentType())) { + return __ippResponse->print(os); + } + return os; +} diff --git a/src/add-ons/print/transports/lpr/LprTransport.cpp b/src/add-ons/print/transports/lpr/LprTransport.cpp index 523d23516a..8e33bbff04 100644 --- a/src/add-ons/print/transports/lpr/LprTransport.cpp +++ b/src/add-ons/print/transports/lpr/LprTransport.cpp @@ -1,11 +1,13 @@ // Sun, 18 Jun 2000 // Y.Takagi +#include #include #include #include -#include -#include + +#include +#include #include #include #include @@ -33,6 +35,12 @@ LprTransport::LprTransport(BMessage *msg) __jobid = 0; __error = false; + struct passwd *pwd = getpwuid(geteuid()); + if (pwd != NULL && pwd->pw_name != NULL && pwd->pw_name[0]) + strcpy(__user, pwd->pw_name); + else + strcpy(__user, "baron"); + DUMP_BMESSAGE(msg); const char *spool_path = msg->FindString(SPOOL_PATH); @@ -58,11 +66,6 @@ LprTransport::LprTransport(BMessage *msg) } dir.WriteAttr(LPR_JOB_ID, B_INT32_TYPE, 0, &__jobid, sizeof(__jobid)); - getusername(__user, sizeof(__user)); - if (__user[0] == '\0') { - strcpy(__user, "baron"); - } - sprintf(__file, "%s/%s@ipp.%ld", spool_path, __user, __jobid); __fs.open(__file, ios::in | ios::out | ios::binary | ios::trunc); @@ -79,14 +82,6 @@ LprTransport::~LprTransport() char hostname[128]; gethostname(hostname, sizeof(hostname)); - char username[128]; -#ifdef WIN32 - unsigned long length = sizeof(hostname); - GetUserName(username, &length); -#else - getusername(username, sizeof(username)); -#endif - ostringstream cfname; cfname << "cfA" << setw(3) << setfill('0') << __jobid << hostname; @@ -95,7 +90,7 @@ LprTransport::~LprTransport() ostringstream cf; cf << 'H' << hostname << '\n'; - cf << 'P' << username << '\n'; + cf << 'P' << __user << '\n'; cf << 'l' << dfname.str() << '\n'; cf << 'U' << dfname.str() << '\n'; diff --git a/src/add-ons/print/transports/lpr/LpsClient.cpp b/src/add-ons/print/transports/lpr/LpsClient.cpp index 27d4a7f195..9575ada408 100644 --- a/src/add-ons/print/transports/lpr/LpsClient.cpp +++ b/src/add-ons/print/transports/lpr/LpsClient.cpp @@ -1,217 +1,222 @@ -// Sun, 18 Jun 2000 -// Y.Takagi - -#ifdef WIN32 - #include - #include - #include -#else /* BeOS */ - #include - #include - #include - #include "_sstream" -#endif - -#include -#include -#include "LpsClient.h" -#include "Socket.h" -//#include "DbgMsg.h" - -#if (!__MWERKS__ || defined(WIN32)) -using namespace std; -#else -#define std -#endif - -#define LPS_SERVER_PORT 515 -#define LPS_CLIENT_PORT_S 721 -#define LPS_CLIENT_PORT_E 731 - -#define LPS_CHECK_QUEUE '\001' -#define LPS_PRINT_JOB '\002' -#define LPS_DISPLAY_SHORT_QUEUE '\003' -#define LPS_DISPLAY_LONG_QUEUE '\004' -#define LPS_REMOVE_JOB '\005' -#define LPS_END_TRANSFER '\000' -#define LPS_ABORT '\001' -#define LPS_RECEIVE_CONTROL_FILE '\002' -#define LPS_RECEIVE_DATA_FILE '\003' - -#define LPS_OK '\0' -#define LPS_ERROR '\1' -#define LPS_NO_SPOOL_SPACE '\2' - -#ifndef INADDR_NONE -#define INADDR_NONE 0xffffffff -#endif - -#ifdef WIN32 - #define EADDRINUSE WSAEADDRINUSE - #define ERRNO WSAGetLastError() -#else - #define ERRNO errno -#endif - - -LpsClient::LpsClient(const char *host) - : connected(false), __host(host), __sock(NULL) -{ -} - -LpsClient::~LpsClient() -{ - close(); -} - -void LpsClient::connect() throw(LPSException) -{ -// DBGMSG(("connect\n")); - - for (int localPort = LPS_CLIENT_PORT_S ; localPort <= LPS_CLIENT_PORT_E ; localPort++) { - if (__sock) { - delete __sock; - } - __sock = new Socket(__host.c_str(), LPS_SERVER_PORT, localPort); - if (__sock->good()) { - __is = &__sock->getInputStream(); - __os = &__sock->getOutputStream(); - connected = true; - return; - } - } - - throw(LPSException(__sock->getLastError())); -} - -void LpsClient::close() -{ -// DBGMSG(("close\n")); - - connected = false; - if (__sock) { - delete __sock; - __sock = NULL; - } -} - -void LpsClient::receiveJob(const char *printer) throw(LPSException) -{ -// DBGMSG(("tell_receive_job\n")); - - if (connected) { - *__os << LPS_PRINT_JOB << printer << '\n' << flush; - checkAck(); - } -} - -void LpsClient::receiveControlFile(int size, const char *name) throw(LPSException) -{ -// DBGMSG(("tell_receive_control_file\n")); - - if (connected) { - - char cfname[32]; - strncpy(cfname, name, sizeof(cfname)); - cfname[sizeof(cfname) - 1] = '\0'; - - *__os << LPS_RECEIVE_CONTROL_FILE << size << ' ' << cfname << '\n' << flush; - - checkAck(); - } -} - -void LpsClient::receiveDataFile(int size, const char *name) throw(LPSException) -{ -// DBGMSG(("tell_receive_data_file\n")); - - if (connected) { - - char dfname[32]; - strncpy(dfname, name, sizeof(dfname)); - dfname[sizeof(dfname) - 1] = '\0'; - - *__os << LPS_RECEIVE_DATA_FILE << size << ' ' << dfname << '\n' << flush; - - checkAck(); - } -} - -void LpsClient::transferData(const char *buffer, int size) throw(LPSException) -{ -// DBGMSG(("send: %d\n", size)); - - if (connected) { - - if (size < 0) { - size = strlen(buffer); - } - - if (!__os->write(buffer, size)) { - close(); - throw(LPSException("error talking to lpd server")); - } - } -} - -void LpsClient::transferData(istream &is, int size) throw(LPSException) -{ -// DBGMSG(("send: %d\n", size)); - - if (connected) { - - if (size < 0) { - is.seekg(0, ios::end); - size = is.tellg(); - is.seekg(0, ios::beg); - } - - char c; - while (is.get(c)) { - if (!__os->put(c)) { - close(); - throw(LPSException("error reading file.")); - return; - } - } - } -} - -void LpsClient::endTransfer() throw(LPSException) -{ -// DBGMSG(("tell_end_transfer\n")); - - if (connected) { - *__os << LPS_END_TRANSFER << flush; - checkAck(); - } -} - -void LpsClient::checkAck() throw(LPSException) -{ -// DBGMSG(("check_ack\n")); - - if (connected) { - - char c; - - if (!__is->get(c)) { - close(); - throw(LPSException("server not responding.")); - return; - } - - switch (c) { - case LPS_OK: - break; - case LPS_ERROR: - close(); - throw(LPSException("server error.")); - break; - case LPS_NO_SPOOL_SPACE: - close(); - throw(LPSException("not enough spool space on server.")); - break; - } - } -} +// Sun, 18 Jun 2000 +// Y.Takagi + +#ifdef WIN32 +# include +# include +# include +#else +# ifdef __HAIKU__ +# include +# include +# else +# include +# include +# endif +# include +# include "_sstream" +#endif + +#include +#include +#include "LpsClient.h" +#include "Socket.h" +//#include "DbgMsg.h" + +#if (!__MWERKS__ || defined(WIN32)) +using namespace std; +#else +#define std +#endif + +#define LPS_SERVER_PORT 515 +#define LPS_CLIENT_PORT_S 721 +#define LPS_CLIENT_PORT_E 731 + +#define LPS_CHECK_QUEUE '\001' +#define LPS_PRINT_JOB '\002' +#define LPS_DISPLAY_SHORT_QUEUE '\003' +#define LPS_DISPLAY_LONG_QUEUE '\004' +#define LPS_REMOVE_JOB '\005' +#define LPS_END_TRANSFER '\000' +#define LPS_ABORT '\001' +#define LPS_RECEIVE_CONTROL_FILE '\002' +#define LPS_RECEIVE_DATA_FILE '\003' + +#define LPS_OK '\0' +#define LPS_ERROR '\1' +#define LPS_NO_SPOOL_SPACE '\2' + +#ifndef INADDR_NONE +#define INADDR_NONE 0xffffffff +#endif + +#ifdef WIN32 + #define EADDRINUSE WSAEADDRINUSE + #define ERRNO WSAGetLastError() +#else + #define ERRNO errno +#endif + + +LpsClient::LpsClient(const char *host) + : connected(false), __host(host), __sock(NULL) +{ +} + +LpsClient::~LpsClient() +{ + close(); +} + +void LpsClient::connect() throw(LPSException) +{ +// DBGMSG(("connect\n")); + + for (int localPort = LPS_CLIENT_PORT_S ; localPort <= LPS_CLIENT_PORT_E ; localPort++) { + if (__sock) { + delete __sock; + } + __sock = new Socket(__host.c_str(), LPS_SERVER_PORT, localPort); + if (__sock->good()) { + __is = &__sock->getInputStream(); + __os = &__sock->getOutputStream(); + connected = true; + return; + } + } + + throw(LPSException(__sock->getLastError())); +} + +void LpsClient::close() +{ +// DBGMSG(("close\n")); + + connected = false; + if (__sock) { + delete __sock; + __sock = NULL; + } +} + +void LpsClient::receiveJob(const char *printer) throw(LPSException) +{ +// DBGMSG(("tell_receive_job\n")); + + if (connected) { + *__os << LPS_PRINT_JOB << printer << '\n' << flush; + checkAck(); + } +} + +void LpsClient::receiveControlFile(int size, const char *name) throw(LPSException) +{ +// DBGMSG(("tell_receive_control_file\n")); + + if (connected) { + + char cfname[32]; + strncpy(cfname, name, sizeof(cfname)); + cfname[sizeof(cfname) - 1] = '\0'; + + *__os << LPS_RECEIVE_CONTROL_FILE << size << ' ' << cfname << '\n' << flush; + + checkAck(); + } +} + +void LpsClient::receiveDataFile(int size, const char *name) throw(LPSException) +{ +// DBGMSG(("tell_receive_data_file\n")); + + if (connected) { + + char dfname[32]; + strncpy(dfname, name, sizeof(dfname)); + dfname[sizeof(dfname) - 1] = '\0'; + + *__os << LPS_RECEIVE_DATA_FILE << size << ' ' << dfname << '\n' << flush; + + checkAck(); + } +} + +void LpsClient::transferData(const char *buffer, int size) throw(LPSException) +{ +// DBGMSG(("send: %d\n", size)); + + if (connected) { + + if (size < 0) { + size = strlen(buffer); + } + + if (!__os->write(buffer, size)) { + close(); + throw(LPSException("error talking to lpd server")); + } + } +} + +void LpsClient::transferData(istream &is, int size) throw(LPSException) +{ +// DBGMSG(("send: %d\n", size)); + + if (connected) { + + if (size < 0) { + is.seekg(0, ios::end); + size = is.tellg(); + is.seekg(0, ios::beg); + } + + char c; + while (is.get(c)) { + if (!__os->put(c)) { + close(); + throw(LPSException("error reading file.")); + return; + } + } + } +} + +void LpsClient::endTransfer() throw(LPSException) +{ +// DBGMSG(("tell_end_transfer\n")); + + if (connected) { + *__os << LPS_END_TRANSFER << flush; + checkAck(); + } +} + +void LpsClient::checkAck() throw(LPSException) +{ +// DBGMSG(("check_ack\n")); + + if (connected) { + + char c; + + if (!__is->get(c)) { + close(); + throw(LPSException("server not responding.")); + return; + } + + switch (c) { + case LPS_OK: + break; + case LPS_ERROR: + close(); + throw(LPSException("server error.")); + break; + case LPS_NO_SPOOL_SPACE: + close(); + throw(LPSException("not enough spool space on server.")); + break; + } + } +} diff --git a/src/add-ons/print/transports/shared/Socket.cpp b/src/add-ons/print/transports/shared/Socket.cpp index ab9d644e26..8e3f88eb63 100644 --- a/src/add-ons/print/transports/shared/Socket.cpp +++ b/src/add-ons/print/transports/shared/Socket.cpp @@ -4,15 +4,18 @@ #ifdef WIN32 #include #else - #include - #include - #ifdef HAVE_ARPA_INET - // inet_addr() is not defined in netdb.h anymore under BONE & later - #include - #endif +#ifdef __HAIKU__ +# include +#else +# include +#endif + #include + #include #endif // WIN32 -#include +#include +#include + #include "Socket.h" #include "SocketStream.h" diff --git a/src/add-ons/print/transports/shared/SocketStream2.cpp b/src/add-ons/print/transports/shared/SocketStream2.cpp index 01d0bcc898..8ecee6507b 100644 --- a/src/add-ons/print/transports/shared/SocketStream2.cpp +++ b/src/add-ons/print/transports/shared/SocketStream2.cpp @@ -1,127 +1,131 @@ -// Sun, 18 Jun 2000 -// Y.Takagi - -#ifndef WIN32 -#include - -#ifdef _DEBUG -#include -#include -#endif - -#include "Socket.h" -#include "SocketStream.h" - -/*-----------------------------------------------------------------*/ - -socketstreambuf::socketstreambuf(Socket *sock, streamsize n) - : streambuf(), __sock(sock), __alsize(n), __pu(NULL), __po(NULL) -{ - setg(0, 0, 0); - setp(0, 0); -} - -socketstreambuf::~socketstreambuf() -{ - if (__pu) - delete [] __pu; - if (__po) - delete [] __po; -} - -int socketstreambuf::underflow() -{ -// cout << "***** underflow" << endl; - - int bytes; - - if (__pu == NULL) { - __pu = new char[__alsize]; - } - - bytes = __sock->read(__pu, __alsize); - if (bytes > 0) { -#ifdef _DEBUG - ofstream ofs("recv.log", ios::binary | ios::app); - ofs.write(__pu, bytes); -#endif - setg(__pu, __pu, __pu + bytes); - return *gptr(); - } - - return EOF; -} - -int socketstreambuf::overflow(int c) -{ -// cout << "***** overflow" << endl; - - if (__po == NULL) { - __po = new char[__alsize]; - setp(__po, __po + __alsize); - } else if (sync() != 0) { - return EOF; - } - return sputc(c); -} - -int socketstreambuf::sync() -{ -// cout << "***** sync" << endl; - - if (__po) { - int length = pptr() - pbase(); - if (length > 0) { - const char *buffer = pbase(); - int bytes; - while (length > 0) { - bytes = __sock->write(buffer, length); - if (bytes <= 0) { - return EOF; - } -#ifdef _DEBUG - ofstream ofs("send.log", ios::binary | ios::app); - ofs.write(buffer, bytes); -#endif - length -= bytes; - buffer += bytes; - } - pbump(pbase() - pptr()); - } - } - - return 0; -} - -/* -------------------------------------------------------------- */ - -socketstreambase::socketstreambase(Socket *sock, streamsize n) - : buf(sock, n) -{ - ios::init(&this->buf); -} - -/*-----------------------------------------------------------------*/ - -isocketstream::isocketstream(Socket *sock, streamsize n) - : socketstreambase(sock, n), istream(socketstreambase::rdbuf()) -{ -} - -isocketstream::~isocketstream() -{ -} - -/*-----------------------------------------------------------------*/ - -osocketstream::osocketstream(Socket *sock, streamsize n) - : socketstreambase(sock, n), ostream(socketstreambase::rdbuf()) -{ -} - -osocketstream::~osocketstream() -{ - flush(); -} - -#endif // !WIN32 +// Sun, 18 Jun 2000 +// Y.Takagi + +#ifndef WIN32 +#ifdef __HAIKU__ +# include +#else +# include +#endif + +#ifdef _DEBUG +#include +#include +#endif + +#include "Socket.h" +#include "SocketStream.h" + +/*-----------------------------------------------------------------*/ + +socketstreambuf::socketstreambuf(Socket *sock, streamsize n) + : streambuf(), __sock(sock), __alsize(n), __pu(NULL), __po(NULL) +{ + setg(0, 0, 0); + setp(0, 0); +} + +socketstreambuf::~socketstreambuf() +{ + if (__pu) + delete [] __pu; + if (__po) + delete [] __po; +} + +int socketstreambuf::underflow() +{ +// cout << "***** underflow" << endl; + + int bytes; + + if (__pu == NULL) { + __pu = new char[__alsize]; + } + + bytes = __sock->read(__pu, __alsize); + if (bytes > 0) { +#ifdef _DEBUG + ofstream ofs("recv.log", ios::binary | ios::app); + ofs.write(__pu, bytes); +#endif + setg(__pu, __pu, __pu + bytes); + return *gptr(); + } + + return EOF; +} + +int socketstreambuf::overflow(int c) +{ +// cout << "***** overflow" << endl; + + if (__po == NULL) { + __po = new char[__alsize]; + setp(__po, __po + __alsize); + } else if (sync() != 0) { + return EOF; + } + return sputc(c); +} + +int socketstreambuf::sync() +{ +// cout << "***** sync" << endl; + + if (__po) { + int length = pptr() - pbase(); + if (length > 0) { + const char *buffer = pbase(); + int bytes; + while (length > 0) { + bytes = __sock->write(buffer, length); + if (bytes <= 0) { + return EOF; + } +#ifdef _DEBUG + ofstream ofs("send.log", ios::binary | ios::app); + ofs.write(buffer, bytes); +#endif + length -= bytes; + buffer += bytes; + } + pbump(pbase() - pptr()); + } + } + + return 0; +} + +/* -------------------------------------------------------------- */ + +socketstreambase::socketstreambase(Socket *sock, streamsize n) + : buf(sock, n) +{ + ios::init(&this->buf); +} + +/*-----------------------------------------------------------------*/ + +isocketstream::isocketstream(Socket *sock, streamsize n) + : socketstreambase(sock, n), istream(socketstreambase::rdbuf()) +{ +} + +isocketstream::~isocketstream() +{ +} + +/*-----------------------------------------------------------------*/ + +osocketstream::osocketstream(Socket *sock, streamsize n) + : socketstreambase(sock, n), ostream(socketstreambase::rdbuf()) +{ +} + +osocketstream::~osocketstream() +{ + flush(); +} + +#endif // !WIN32