ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

compat.c
Go to the documentation of this file.
00001 /*
00002     compat: Some compatibility functions.
00003 
00004     The mpg123 code is determined to keep it's legacy. A legacy of old, old UNIX.
00005     So anything possibly somewhat advanced should be considered to be put here, with proper #ifdef;-)
00006 
00007     copyright 2007-8 by the mpg123 project - free software under the terms of the LGPL 2.1
00008     see COPYING and AUTHORS files in distribution or http://mpg123.org
00009     initially written by Thomas Orgis, Windows Unicode stuff by JonY.
00010 */
00011 
00012 #include "config.h"
00013 #include "compat.h"
00014 
00015 #ifdef _MSC_VER
00016 #include <io.h>
00017 #else
00018 #include <fcntl.h>
00019 #endif
00020 
00021 #ifdef WANT_WIN32_UNICODE
00022 #include <wchar.h>
00023 #include <windows.h>
00024 #include <winnls.h>
00025 #endif
00026 
00027 #include "debug.h"
00028 
00029 /* A safe realloc also for very old systems where realloc(NULL, size) returns NULL. */
00030 void *safe_realloc(void *ptr, size_t size)
00031 {
00032     if(ptr == NULL) return malloc(size);
00033     else return realloc(ptr, size);
00034 }
00035 
00036 #ifndef HAVE_STRERROR
00037 const char *strerror(int errnum)
00038 {
00039     extern int sys_nerr;
00040     extern char *sys_errlist[];
00041 
00042     return (errnum < sys_nerr) ?  sys_errlist[errnum]  :  "";
00043 }
00044 #endif
00045 
00046 #ifndef HAVE_STRDUP
00047 char *strdup(const char *src)
00048 {
00049     char *dest;
00050 
00051     if (!(dest = (char *) malloc(strlen(src)+1)))
00052     return NULL;
00053     else
00054     return strcpy(dest, src);
00055 }
00056 #endif
00057 
00058 int compat_open(const char *filename, int mode)
00059 {
00060     int ret;
00061 #if defined (WANT_WIN32_UNICODE)
00062     const wchar_t *frag = NULL;
00063 
00064     ret = win32_utf8_wide(filename, &frag, NULL);
00065     if ((frag == NULL) || (ret == 0)) goto fallback; /* Fallback to plain open when ucs-2 conversion fails */
00066 
00067     ret = _wopen(frag, mode); /*Try _wopen */
00068     if (ret != -1 ) goto open_ok; /* msdn says -1 means failure */
00069 
00070 fallback:
00071 #endif
00072 
00073 #ifdef __MSVCRT__ /* MSDN says POSIX function is deprecated beginning in Visual C++ 2005 */
00074     ret = _open (filename, mode); /* Try plain old _open(), if it fails, do nothing */
00075 #else
00076     ret = open (filename, mode);
00077 #endif
00078 
00079 #if defined (WANT_WIN32_UNICODE)
00080 open_ok:
00081     free ((void *)frag); /* Freeing a NULL should be OK */
00082 #endif
00083 
00084     return ret;
00085 }
00086 
00087 int compat_close(int infd)
00088 {
00089 #ifdef __MSVCRT__ /* MSDN says POSIX function is deprecated beginning in Visual C++ 2005 */
00090     return _close(infd);
00091 #else
00092     return close(infd);
00093 #endif
00094 }
00095 
00096 /* Windows Unicode stuff */
00097 
00098 #ifdef WANT_WIN32_UNICODE
00099 int win32_wide_utf8(const wchar_t * const wptr, const char **const mbptr, size_t * const buflen)
00100 {
00101     size_t len;
00102     char *buf;
00103     int ret;
00104 
00105     len = WideCharToMultiByte(CP_UTF8, 0, wptr, -1, NULL, 0, NULL, NULL); /* Get utf-8 string length */
00106     buf = calloc(len, sizeof (char)); /* Can we assume sizeof char always = 1? */
00107     debug2("win32_wide_utf8 allocated %u bytes at %p", len, buf);
00108 
00109     if(buf != NULL)
00110     {
00111         ret = WideCharToMultiByte(CP_UTF8, 0, wptr, -1, buf, len, NULL, NULL); /*Do actual conversion*/
00112         *mbptr = buf; /* Set string pointer to allocated buffer */
00113         if(buflen != NULL) *buflen = len * sizeof (char); /* Give length of allocated memory if needed. */
00114 
00115         return ret;
00116     }
00117     else
00118     {
00119         if(buflen != NULL) *buflen = 0;
00120 
00121         return 0;
00122     }
00123 }
00124 
00125 int win32_utf8_wide(const char *const mbptr, const wchar_t ** const wptr, size_t * const buflen)
00126 {
00127     size_t len;
00128     wchar_t *buf;
00129     int ret;
00130 
00131     len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, mbptr, -1, NULL, 0); /* Get converted size */
00132     buf = calloc(len, sizeof (wchar_t)); /* Allocate memory accordingly */
00133     debug2("win32_utf8_wide allocated %u bytes at %p", len, buf);
00134 
00135     if(buf != NULL)
00136     {
00137         ret = MultiByteToWideChar (CP_UTF8, MB_ERR_INVALID_CHARS, mbptr, -1, buf, len); /* Do conversion */
00138         *wptr = buf; /* Set string pointer to allocated buffer */
00139         if (buflen != NULL) *buflen = len * sizeof (wchar_t); /* Give length of allocated memory if needed. */
00140 
00141         return ret;
00142     }
00143     else
00144     {
00145         if (buflen != NULL) *buflen = 0;
00146 
00147         return 0;
00148     }
00149 }
00150 #endif

Generated on Sun May 27 2012 04:23:26 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.