OS_NS_stdio.cpp

Go to the documentation of this file.
00001 // OS_NS_stdio.cpp,v 1.29 2006/05/30 10:34:47 jwillemsen Exp
00002 
00003 #include "ace/OS_NS_stdio.h"
00004 #include "ace/OS_NS_Thread.h"
00005 
00006 ACE_RCSID (ace,
00007            OS_NS_stdio,
00008            "OS_NS_stdio.cpp,v 1.29 2006/05/30 10:34:47 jwillemsen Exp")
00009 
00010 
00011 #if !defined (ACE_HAS_INLINED_OSCALLS)
00012 # include "ace/OS_NS_stdio.inl"
00013 #endif /* ACE_HAS_INLINED_OS_CALLS */
00014 
00015 # if defined (ACE_WIN32)
00016 
00017 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00018 OSVERSIONINFO ACE_OS::win32_versioninfo_;
00019 HINSTANCE ACE_OS::win32_resource_module_;
00020 ACE_END_VERSIONED_NAMESPACE_DECL
00021 
00022 #   if defined (ACE_HAS_DLL) && (ACE_HAS_DLL == 1) && !defined (ACE_HAS_WINCE)
00023 // This function is called by the OS when the ACE DLL is loaded. We
00024 // use it to determine the default module containing ACE's resources.
00025 BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID)
00026 {
00027   if (reason == DLL_PROCESS_ATTACH)
00028     {
00029 #     if defined (ACE_DISABLES_THREAD_LIBRARY_CALLS) && (ACE_DISABLES_THREAD_LIBRARY_CALLS == 1)
00030       ::DisableThreadLibraryCalls (instance);
00031 #     endif /* ACE_DISABLES_THREAD_LIBRARY_CALLS */
00032       ACE_OS::set_win32_resource_module(instance);
00033     }
00034   else if (reason == DLL_THREAD_DETACH)
00035     {
00036       ACE_OS::cleanup_tss (0);
00037     }
00038   return TRUE;
00039 }
00040 #   endif /* ACE_HAS_DLL && ACE_HAS_DLL == 1 */
00041 # endif /* ACE_WIN32 */
00042 
00043 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00044 
00045 void
00046 ACE_OS::ace_flock_t::dump (void) const
00047 {
00048 #if defined (ACE_HAS_DUMP)
00049   ACE_OS_TRACE ("ACE_OS::ace_flock_t::dump");
00050 
00051 # if 0
00052   ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00053   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("handle_ = %u"), this->handle_));
00054 #   if defined (ACE_WIN32)
00055   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\nInternal = %d"),
00056               this->overlapped_.Internal));
00057   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\nInternalHigh = %d"),
00058               this->overlapped_.InternalHigh));
00059   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\nOffsetHigh = %d"),
00060               this->overlapped_.OffsetHigh));
00061   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\nhEvent = %d"),
00062               this->overlapped_.hEvent));
00063 #   else
00064   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\nl_whence = %d"),
00065               this->lock_.l_whence));
00066   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\nl_start = %d"), this->lock_.l_start));
00067   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\nl_len = %d"), this->lock_.l_len));
00068   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\nl_type = %d"), this->lock_.l_type));
00069 #   endif /* ACE_WIN32 */
00070   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00071 # endif /* 0 */
00072 #endif /* ACE_HAS_DUMP */
00073 }
00074 
00075 /*****************************************************************************/
00076 
00077 #if defined (ACE_USES_WCHAR)
00078 void ACE_OS::checkUnicodeFormat (FILE* fp)
00079 {
00080   if (fp != 0)
00081     {
00082       // Due to the ACE_TCHAR definition, all default input files, such as
00083       // svc.conf, have to be in Unicode format (small endian) on WinCE
00084       // because ACE has all 'char' converted into ACE_TCHAR.
00085       // However, for TAO, ASCII files, such as IOR file, can still be read
00086       // and be written without any error since given buffers are all in 'char'
00087       // type instead of ACE_TCHAR.  Therefore, it is user's reponsibility to
00088       // select correct buffer type.
00089 
00090       // At this point, check if the file is Unicode or not.
00091       ACE_UINT16 first_two_bytes;
00092       size_t numRead =
00093         ACE_OS::fread(&first_two_bytes, sizeof (first_two_bytes), 1, fp);
00094 
00095       if (numRead == 1)
00096         {
00097           if ((first_two_bytes != 0xFFFE) && // not a small endian Unicode file
00098               (first_two_bytes != 0xFEFF))   // not a big endian Unicode file
00099             {
00100               // set file pointer back to the beginning
00101 #if defined (ACE_WIN32)
00102               ACE_OS::fseek(fp, 0, FILE_BEGIN);
00103 #else
00104               ACE_OS::fseek(fp, 0, SEEK_SET);
00105 #endif /* ACE_WIN32 */
00106             }
00107         }
00108       // if it is a Unicode file, file pointer will be right next to the first
00109       // two-bytes
00110     }
00111 }
00112 #endif  // ACE_USES_WCHAR
00113 
00114 #if defined (ACE_WIN32) && !defined (ACE_HAS_WINCE)
00115 FILE *
00116 ACE_OS::fopen (const char *filename,
00117                const ACE_TCHAR *mode)
00118 {
00119   ACE_OS_TRACE ("ACE_OS::fopen");
00120   int hmode = _O_TEXT;
00121 
00122   for (const ACE_TCHAR *mode_ptr = mode; *mode_ptr != 0; mode_ptr++)
00123     ACE_OS::fopen_mode_to_open_mode_converter (*mode_ptr, hmode);
00124 
00125   ACE_HANDLE handle = ACE_OS::open (filename, hmode);
00126   if (handle != ACE_INVALID_HANDLE)
00127     {
00128       hmode &= _O_TEXT | _O_RDONLY | _O_APPEND;
00129 
00130 #   if defined (ACE_LACKS_INTPTR_T)
00131       int fd = ::_open_osfhandle (long (handle), hmode);
00132 #   else
00133       int fd = ::_open_osfhandle (intptr_t (handle), hmode);
00134 #   endif
00135 
00136       if (fd != -1)
00137         {
00138 #   if defined (__BORLANDC__) && !defined (ACE_USES_WCHAR)
00139           FILE *fp = ::_fdopen (fd, const_cast<ACE_TCHAR *> (mode));
00140 #   elif defined (__BORLANDC__) && defined (ACE_USES_WCHAR)
00141           FILE *fp = ::_wfdopen (fd, const_cast<ACE_TCHAR *> (mode));
00142 #   elif defined (ACE_USES_WCHAR)
00143           FILE *fp = ::_wfdopen (fd, mode);
00144 #   else
00145           FILE *fp = ::fdopen (fd, mode);
00146 #   endif /* defined(__BORLANDC__) && !defined (ACE_USES_WCHAR)) */
00147           if (fp != 0)
00148           {
00149 #   if defined (ACE_USES_WCHAR)
00150             checkUnicodeFormat(fp);
00151 #   endif  // ACE_USES_WCHAR
00152             return fp;
00153           }
00154           ::_close (fd);
00155         }
00156 
00157       ACE_OS::close (handle);
00158     }
00159   return 0;
00160 }
00161 
00162 #if defined (ACE_HAS_WCHAR)
00163 FILE *
00164 ACE_OS::fopen (const wchar_t *filename,
00165                const ACE_TCHAR *mode)
00166 {
00167   ACE_OS_TRACE ("ACE_OS::fopen");
00168   int hmode = _O_TEXT;
00169 
00170   for (const ACE_TCHAR *mode_ptr = mode; *mode_ptr != 0; mode_ptr++)
00171     ACE_OS::fopen_mode_to_open_mode_converter (*mode_ptr, hmode);
00172 
00173   ACE_HANDLE handle = ACE_OS::open (filename, hmode);
00174   if (handle != ACE_INVALID_HANDLE)
00175     {
00176       hmode &= _O_TEXT | _O_RDONLY | _O_APPEND;
00177 
00178 #   if defined (ACE_LACKS_INTPTR_T)
00179       int fd = ::_open_osfhandle (long (handle), hmode);
00180 #   else
00181       int fd = ::_open_osfhandle (intptr_t (handle), hmode);
00182 #   endif
00183 
00184       if (fd != -1)
00185         {
00186 #   if defined (__BORLANDC__) && !defined (ACE_USES_WCHAR)
00187           FILE *fp = ::_fdopen (fd, const_cast<char *> (mode));
00188 #   elif defined (__BORLANDC__) && defined (ACE_USES_WCHAR)
00189           FILE *fp = ::_wfdopen (fd, const_cast<wchar_t *> (mode));
00190 #   elif defined (ACE_USES_WCHAR)
00191           FILE *fp = ::_wfdopen (fd, mode);
00192 #   else
00193           FILE *fp = ::fdopen (fd, mode);
00194 #   endif /* defined(__BORLANDC__) && !defined (ACE_USES_WCHAR)) */
00195           if (fp != 0)
00196           {
00197 #   if defined (ACE_USES_WCHAR)
00198             checkUnicodeFormat(fp);
00199 #   endif  // ACE_USES_WCHAR
00200             return fp;
00201           }
00202           ::_close (fd);
00203         }
00204 
00205       ACE_OS::close (handle);
00206     }
00207   return 0;
00208 }
00209 #endif /* ACE_HAS_WCHAR */
00210 
00211 #endif /* ACE_WIN32 */
00212 
00213 int
00214 ACE_OS::fprintf (FILE *fp, const char *format, ...)
00215 {
00216   ACE_OS_TRACE ("ACE_OS::fprintf");
00217   int result = 0;
00218   va_list ap;
00219   va_start (ap, format);
00220   ACE_OSCALL (::vfprintf (fp, format, ap), int, -1, result);
00221   va_end (ap);
00222   return result;
00223 }
00224 
00225 #if defined (ACE_HAS_WCHAR)
00226 int
00227 ACE_OS::fprintf (FILE *fp, const wchar_t *format, ...)
00228 {
00229   ACE_OS_TRACE ("ACE_OS::fprintf");
00230 
00231 # if !defined (ACE_HAS_VFWPRINTF)
00232   ACE_UNUSED_ARG (fp);
00233   ACE_UNUSED_ARG (format);
00234   ACE_NOTSUP_RETURN (-1);
00235 
00236 # else
00237   int result = 0;
00238   va_list ap;
00239   va_start (ap, format);
00240   ACE_OSCALL (ACE_STD_NAMESPACE::vfwprintf (fp, format, ap), int, -1, result);
00241   va_end (ap);
00242   return result;
00243 
00244 # endif /* ACE_HAS_VFWPRINTF */
00245 }
00246 #endif /* ACE_HAS_WCHAR */
00247 
00248 
00249 // The following *printf functions aren't inline because
00250 // they use varargs.
00251 
00252 int
00253 ACE_OS::printf (const char *format, ...)
00254 {
00255   ACE_OS_TRACE ("ACE_OS::printf");
00256   int result;
00257   va_list ap;
00258   va_start (ap, format);
00259   ACE_OSCALL (::vprintf (format, ap), int, -1, result);
00260   va_end (ap);
00261   return result;
00262 }
00263 
00264 int
00265 ACE_OS::snprintf (char *buf, size_t maxlen, const char *format, ...)
00266 {
00267   // ACE_OS_TRACE ("ACE_OS::snprintf");
00268 #if defined (ACE_HAS_SNPRINTF)
00269   int result;
00270   va_list ap;
00271   va_start (ap, format);
00272 #  if !defined (ACE_WIN32) || (defined (__BORLANDC__) && (__BORLANDC__ >= 0x600))
00273   ACE_OSCALL (ACE_SPRINTF_ADAPTER (::vsnprintf (buf, maxlen, format, ap)),
00274               int, -1, result);
00275 #  else
00276   ACE_OSCALL (ACE_SPRINTF_ADAPTER (::_vsnprintf (buf, maxlen, format, ap)),
00277               int, -1, result);
00278   // Win32 doesn't regard a full buffer with no 0-terminate as an
00279   // overrun.
00280   if (result == static_cast <int> (maxlen))
00281     result = -1;
00282 
00283   // Win32 doesn't 0-terminate the string if it overruns maxlen.
00284   if (result == -1)
00285     buf[maxlen-1] = '\0';
00286 #  endif /* !ACE_WIN32 || __BORLANDC__ >= 0x600 */
00287   va_end (ap);
00288   // In out-of-range conditions, C99 defines vsnprintf to return the number
00289   // of characters that would have been written if enough space was available.
00290   // Earlier variants of the vsnprintf() (e.g. UNIX98) defined it to return
00291   // -1. This method follows the C99 standard, but needs to guess at the
00292   // value; uses maxlen + 1.
00293   if (result == -1)
00294     result = static_cast <int> (maxlen + 1);
00295   return result;
00296 
00297 #else
00298   ACE_UNUSED_ARG (buf);
00299   ACE_UNUSED_ARG (maxlen);
00300   ACE_UNUSED_ARG (format);
00301   ACE_NOTSUP_RETURN (-1);
00302 #endif /* ACE_HAS_SNPRINTF */
00303 }
00304 
00305 #if defined (ACE_HAS_WCHAR)
00306 
00307 int
00308 ACE_OS::snprintf (wchar_t *buf, size_t maxlen, const wchar_t *format, ...)
00309 {
00310   // ACE_OS_TRACE ("ACE_OS::snprintf");
00311 #if (defined _XOPEN_SOURCE && (_XOPEN_SOURCE - 0) >= 500) || defined (ACE_WIN32)
00312   int result;
00313   va_list ap;
00314   va_start (ap, format);
00315 #  if defined (ACE_WIN32)
00316   // Microsoft's vswprintf() doesn't have the maxlen argument that
00317   // XPG4/UNIX98 define. They do, however, recommend use of _vsnwprintf()
00318   // as a substitute, which does have the same signature as the UNIX98 one.
00319   ACE_OSCALL (ACE_SPRINTF_ADAPTER (::_vsnwprintf (buf, maxlen, format, ap)),
00320               int, -1, result);
00321   // Win32 doesn't regard a full buffer with no 0-terminate as an
00322   // overrun.
00323   if (result == static_cast <int> (maxlen))
00324     result = -1;
00325 
00326   // Win32 doesn't 0-terminate the string if it overruns maxlen.
00327   if (result == -1)
00328     buf[maxlen-1] = '\0';
00329 #  else
00330   ACE_OSCALL (ACE_SPRINTF_ADAPTER (::vswprintf (buf, maxlen, format, ap)),
00331               int, -1, result);
00332 #  endif /* ACE_WIN32 */
00333   va_end (ap);
00334   // In out-of-range conditions, C99 defines vsnprintf to return the number
00335   // of characters that would have been written if enough space was available.
00336   // Earlier variants of the vsnprintf() (e.g. UNIX98) defined it to return
00337   // -1. This method follows the C99 standard, but needs to guess at the
00338   // value; uses maxlen + 1.
00339   if (result == -1)
00340     result = static_cast <int> (maxlen + 1);
00341   return result;
00342 
00343 #else
00344   ACE_UNUSED_ARG (buf);
00345   ACE_UNUSED_ARG (maxlen);
00346   ACE_UNUSED_ARG (format);
00347   ACE_NOTSUP_RETURN (-1);
00348 #endif /* ACE_HAS_SNPRINTF */
00349 }
00350 #endif /* ACE_HAS_WCHAR */
00351 
00352 int
00353 ACE_OS::sprintf (char *buf, const char *format, ...)
00354 {
00355   // ACE_OS_TRACE ("ACE_OS::sprintf");
00356 
00357   int result;
00358   va_list ap;
00359   va_start (ap, format);
00360   ACE_OSCALL (ACE_SPRINTF_ADAPTER (::vsprintf (buf, format, ap)), int, -1, result);
00361   va_end (ap);
00362   return result;
00363 }
00364 
00365 #if defined (ACE_HAS_WCHAR)
00366 int
00367 ACE_OS::sprintf (wchar_t *buf, const wchar_t *format, ...)
00368 {
00369   ACE_OS_TRACE ("ACE_OS::sprintf");
00370 
00371 # if (defined _XOPEN_SOURCE && (_XOPEN_SOURCE - 0) >= 500) || (defined ACE_HAS_DINKUM_STL) || defined (__DMC__)
00372 
00373   // The XPG4/UNIX98/C99 signature of the wide-char sprintf has a
00374   // maxlen argument. Since this method doesn't supply one, pass in
00375   // a length that works (ULONG_MAX doesn't on all platform since some check
00376   // to see if the operation will remain in bounds). If this isn't ok, use
00377   // ACE_OS::snprintf().
00378   int result;
00379   va_list ap;
00380   va_start (ap, format);
00381   ACE_OSCALL (ACE_STD_NAMESPACE::vswprintf (buf, 4096, format, ap), int, -1, result);
00382   va_end (ap);
00383   return result;
00384 
00385 # elif defined (ACE_WIN32)
00386   // Windows has vswprintf, but the signature is from the older ISO C
00387   // standard. Also see ACE_OS::snprintf() for more info on this.
00388 
00389   int result;
00390   va_list ap;
00391   va_start (ap, format);
00392   ACE_OSCALL (::vswprintf (buf, format, ap), int, -1, result);
00393   va_end (ap);
00394   return result;
00395 
00396 # else
00397 
00398   ACE_UNUSED_ARG (buf);
00399   ACE_UNUSED_ARG (format);
00400   ACE_NOTSUP_RETURN (-1);
00401 
00402 # endif /* XPG5 || ACE_HAS_DINKUM_STL */
00403 }
00404 #endif /* ACE_HAS_WCHAR */
00405 
00406 ACE_END_VERSIONED_NAMESPACE_DECL

Generated on Thu Nov 9 09:41:57 2006 for ACE by doxygen 1.3.6