OWLNext    7.0
Borland's Object Windows Library for the modern age
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
wskerr.cpp
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// ObjectWindows
3// Copyright (c) 1995, 1996 by Borland International, All Rights Reserved
4//
5/// \file
6/// Winsock for OWL subsystem.
7/// Based on work by Paul Pedriana, 70541.3223@compuserve.com
8//----------------------------------------------------------------------------
9#include <owl/pch.h>
10#include <owl/defs.h>
11#include <owl/module.h>
12#include <owl/winsock.h>
13#include <owl/winsock.rh>
14#include <stdio.h>
15
16namespace owl {
17
19
20//
21/// This function constructs the object with the error code and the size of the
22/// buffer to allocate.
23//
25:
26 Error(error),
27 SizeToAllocate(sizeToAllocate),
28 String(0)
29{
31}
32
33//
34/// This function destroys the allocated string.
35//
37{
38 delete[] String;
39}
40
41//
42/// Copies the error code and string.
43//
45:
46 Error (src.Error),
47 String (strnewdup(src.String)),
48 SizeToAllocate (src.SizeToAllocate)
49{
50}
51
52//
53/// This function copies the error code and string.
54//
56{
57 Error = src.Error;
58 delete[] String;
59 String = strnewdup(src.String);
60 SizeToAllocate = src.SizeToAllocate;
61
62 return *this;
63}
64
65//
66/// This function tests for equality between two TSocketError objects. The important
67/// criteria for determining equality is the error value. The string is unimportant.
68//
71{
72 return socketError1.Error == socketError2.Error;
73}
74
75//
76/// This function initializes the error code.
77//
79{
80 Error = error;
82}
83
84//
85/// This function returns the error code.
86//
88{
89 return Error;
90}
91
92//
93/// This function hands the pointer to the string to the caller. The caller
94/// shouldn't alter this string (because it doesn't belong to the caller).
95//
97{
98 return String;
99}
100
101//
102/// This function appends the error string to whatever is in the string
103/// 'stringToAppendErrorTo' and put the result in 'destination'. You may
104/// want to put something specific about the error in the string and then use
105/// AppendError() to add the Winsock error code and description to it.
106///
107/// For example, you could say:
108/// \code
109// MessageBox(TSocketError(WSAENOTCONN).AppendError("Unable to send your mail"),
110// "Error", MB_OK);
111/// \endcode
112/// And AppendError() will put "\n\nWinsock Error 10057: Socket is not presently connected"
113/// after the "Unable to send you mail" string. Quite convenient.
114///
115/// If destination is valid, destination provides the space for the result.
116/// \note It is assumed that desination has enough space.
117/// If destination is 0, our internal string buffer is used for the result.
118///
119/// \note Using the internal string changes the result of subsequent calls to GetReasonString.
120/// Use Init to reset the error string.
121//
123{
124 // Create the error string.
125 //
127 s += _T("\n\n");
128 s += GetReasonString();
129
130 if (!destination) {
131 //
132 // Expand the internal string buffer if needed.
133 //
134 size_t n = s.length() + 1;
135 if (n > SizeToAllocate) {
136 delete [] String;
137 String = new tchar[SizeToAllocate = n];
138 }
140 }
141
142 // Copy the string to the destination.
143
144 _tcscpy(destination, s.c_str());
145 return destination;
146}
147
148//
149/// This function is similar to AppendError(char*, char*), but the pre-string comes
150/// from a string resource and szStringToAppendErrorTo will be overwritten with what
151/// is in the string resource and appended to the Windows Sockets Error description.
152/// stringToAppendErrorTo must be able to hold at least 128 characters.
153//
159
160//
161/// This function gets a string, suitable for display, based on the nError value.
162/// The previous string is deleted if necessary. Note that the string allocated must
163/// be at least 128 characters long. Even though the error strings you see don't
164/// have error numbers associated with them, the function prepends the error number
165/// to the szString before returning.
166///
167/// If you are writing string resources for the error strings, don't put error
168/// numbers in the string, because that will be done for you later.
169//
171{
172 int resIdLookup[] = {
173 WSAEINTR,
174 WSAEBADF,
175 WSAEACCES,
176 WSAEFAULT,
177 WSAEINVAL,
178 WSAEMFILE,
206 WSAELOOP,
212 WSAEUSERS,
213 WSAEDQUOT,
214 WSAESTALE,
224 };
225 unsigned id = 0;
226 while (id < COUNTOF(resIdLookup) && resIdLookup[id] != Error)
227 id++;
228
229 tstring s = GetModule().LoadString(IDS_WINSOCK_BASE+id+1);
230 size_t n = s.length() + 26; // Add space for prefix "Winsock Error 123456789: ".
232 delete[] String;
234 String[0] = 0;
235
236 _sntprintf(String, SizeToAllocate, _T("Winsock Error %d: %s"), Error, (tchar*) s.c_str());
237}
238
239//
240/// Return the current module. Used when loading strings.
241/// Important when linking to OWL dynamically because ::LoadString
242/// would try to load from the EXE, but the WinSock resource
243/// strings are in the DLL.
244//
245TModule&
246TSocketError::GetModule() // this is a static function
247{
248 return GetGlobalModule();
249}
250
251} // OWL namespace
252
253/* ========================================================================== */
254
ObjectWindows dynamic-link libraries (DLLs) construct an instance of TModule, which acts as an object...
Definition module.h:75
TSocketError converts Windows Sockets errors to string messages.
Definition wskerr.h:30
void GetErrorString()
Convert the error code to string.
Definition wskerr.cpp:170
const tchar * GetReasonString() const
This function hands the pointer to the string to the caller.
Definition wskerr.cpp:96
TSocketError & operator=(const TSocketError &src)
This function copies the error code and string.
Definition wskerr.cpp:55
virtual ~TSocketError()
This function destroys the allocated string.
Definition wskerr.cpp:36
int GetReasonValue() const
This function returns the error code.
Definition wskerr.cpp:87
int Error
Error code.
Definition wskerr.h:50
size_t SizeToAllocate
Size to allocate for String.
Definition wskerr.h:51
tchar * String
Error code converted to string.
Definition wskerr.h:53
TSocketError(int error=0, unsigned sizeToAllocate=128)
This function constructs the object with the error code and the size of the buffer to allocate.
Definition wskerr.cpp:24
void Init(int error)
This function initializes the error code.
Definition wskerr.cpp:78
tchar * AppendError(const tchar *stringToAppendErrorTo, tchar *destination=0)
This function appends the error string to whatever is in the string 'stringToAppendErrorTo' and put t...
Definition wskerr.cpp:122
#define _tcscpy
Definition cygwin.h:79
#define _T(x)
Definition cygwin.h:51
int operator==(const TBitSet< T > &bs1, const TBitSet< T > &bs2)
Definition bitset.h:86
char * strnewdup(const char *s, size_t minAllocSize=0)
Definition memory.cpp:25
Definition of class TModule.
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
char tchar
Definition defs.h:77
OWL_DIAGINFO
Definition animctrl.cpp:14
std::string tstring
Definition defs.h:79
TModule & GetGlobalModule()
Definition global.cpp:48
General definitions used by all ObjectWindows programs.
#define COUNTOF(s)
Array element count Important: Only use this with an argument of array type.
Definition defs.h:376
Main header of the Winsock OWL subsystem.