⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 swiprintf.cpp

📁 sloedgy open sip stack source code
💻 CPP
📖 第 1 页 / 共 2 页
字号:

 /****************License************************************************
  *
  * Copyright 2000-2003.  ScanSoft, Inc.    
  *
  * Use of this software is subject to notices and obligations set forth 
  * in the SpeechWorks Public License - Software Version 1.2 which is 
  * included with this software. 
  *
  * ScanSoft is a registered trademark of ScanSoft, Inc., and OpenSpeech, 
  * SpeechWorks and the SpeechWorks logo are registered trademarks or 
  * trademarks of SpeechWorks International, Inc. in the United States 
  * and other countries.
  *
  ***********************************************************************/
 

 // -----1=0-------2=0-------3=0-------4=0-------5=0-------6=0-------7=0-------8
 
#include <vxibuildopts.h>
#if P_VXI

 #include <stdio.h>
 #include <stdlib.h>
 #include <stddef.h>
 #include <string.h>
 #include <stdarg.h>
 #include <ctype.h>
 #include <malloc.h>
 
 #include "vxi/SWIprintf.h"
 #include "misc_string.h"
 
 static const char BAD_FORMAT_TEXT[] = " [bad format!]";
 static const wchar_t BAD_FORMAT_TEXT_W[] = L" [bad format!]";
 static const size_t BAD_FORMAT_TEXT_LEN = 14;
 
 // Convert wide to narrow characters
 #define w2c(w) (((w) & 0xff00)?'\277':((unsigned char) ((w) & 0x00ff)))
 
 #if !defined(WIN32)
 static wchar_t* ConvertFormatForWidePrintf(const wchar_t* format)
 {
   int formatLen = wcslen(format);
 
   // The maximum length of the new format string is 1 and 2/3 that
   //    of the original.
   wchar_t* newFormat = new wchar_t [2 * formatLen];
   int nfIndex = 0;
 
   for (int fIndex = 0; fIndex < formatLen; ++fIndex)
   {
     // We found %s in format.
     if ((format[fIndex] == L'%') && (fIndex != (formatLen - 1)))
     {
       newFormat[nfIndex++] = L'%';
       fIndex++;
       while((SWIchar_iswdigit(format[fIndex]) || (format[fIndex] == L'+') ||
             (format[fIndex] == L'-') || (format[fIndex] == L'.') ||
             (format[fIndex] == L'*') || (format[fIndex] == L'#')) &&
             (fIndex < formatLen - 1)) {
         newFormat[nfIndex++] = format[fIndex++];
       }
 
       switch(format[fIndex]) {
         case L's': {
           newFormat[nfIndex++] = L'l';
           newFormat[nfIndex++] = L's';
           break;
         }
         case L'S': {
           newFormat[nfIndex++] = L's';
           break;
         }
         case L'c': {
           newFormat[nfIndex++] = L'l';
           newFormat[nfIndex++] = L'c';
           break;
         }
         case L'C': {
           newFormat[nfIndex++] = L'c';
           break;
         }
         default: {
           newFormat[nfIndex++] = format[fIndex];
           break;
         }
       }
     }
     else
     {
       newFormat[nfIndex++] = format[fIndex];
     }
   }
 
   newFormat[nfIndex] = 0;
 
   return newFormat;
 }
 
 static char* ConvertFormatForNarrowPrintf(const char* format)
 {
   int formatLen = strlen(format);
 
   // The maximum length of the new format string is 1 and 2/3 that
   //    of the original.
   char* newFormat = new char [2 * formatLen];
   int nfIndex = 0;
 
   for (int fIndex = 0; fIndex < formatLen; ++fIndex)
   {
     // We found %s in format.
     if ((format[fIndex] == '%') && (fIndex != (formatLen - 1)))
     {
       newFormat[nfIndex++] = '%';
       fIndex++;
       while((SWIchar_isdigit(format[fIndex]) || (format[fIndex] == L'+') ||
             (format[fIndex] == L'-') || (format[fIndex] == L'.') ||
             (format[fIndex] == L'*') || (format[fIndex] == L'#')) &&
             (fIndex < formatLen - 1)) {
         newFormat[nfIndex++] = format[fIndex++];
       }
 
       if (format[fIndex] == 'S')
       {
         newFormat[nfIndex++] = 'l';
         newFormat[nfIndex++] = 's';
       }
       else if (format[fIndex] == 'C')
       {
         newFormat[nfIndex++] = 'l';
         newFormat[nfIndex++] = 'c';
       }
       else
       {
         newFormat[nfIndex++] = format[fIndex];
       }
     }
     else
     {
       newFormat[nfIndex++] = format[fIndex];
     }
   }
 
   newFormat[nfIndex] = 0;
 
   return newFormat;
 }
 
 static char* ConvertWideFormatForNarrowPrintf(const wchar_t* format)
 {
   int formatLen = wcslen(format);
 
   // The maximum length of the new format string is 1 and 2/3 that
   //    of the original.
   char* newFormat = new char [2 * formatLen];
   int nfIndex = 0;
 
   for (int fIndex = 0; fIndex < formatLen; ++fIndex)
   {
     // We found %s in format.
     if ((format[fIndex] == L'%') && (fIndex != (formatLen - 1)))
     {
       newFormat[nfIndex++] = '%';
       fIndex++;
       while((SWIchar_iswdigit(format[fIndex]) || (format[fIndex] == L'+') ||
             (format[fIndex] == L'-') || (format[fIndex] == L'.') ||
             (format[fIndex] == L'*') || (format[fIndex] == L'#')) &&
             (fIndex < formatLen - 1)) {
         newFormat[nfIndex++] = w2c(format[fIndex++]);
       }
 
       switch(format[fIndex]) {
         case L's': {
           newFormat[nfIndex++] = 'l';
           newFormat[nfIndex++] = 's';
           break;
         }
         case L'S': {
           newFormat[nfIndex++] = 's';
           break;
         }
         case L'c': {
           newFormat[nfIndex++] = 'l';
           newFormat[nfIndex++] = 'c';
           break;
         }
         case L'C': {
           newFormat[nfIndex++] = 'c';
           break;
         }
         default: {
           newFormat[nfIndex++] = w2c(format[fIndex]);
           break;
         }
       }
     }
     else
     {
       newFormat[nfIndex++] = w2c(format[fIndex]);
     }
   }
 
   newFormat[nfIndex] = 0;
 
   return newFormat;
 }
 #endif
 
 /*
  *  Common functions for all OSes
  */
 
 SWIPRINTF_API int SWIfprintf(FILE* file, const char* format, ...)
 {
   va_list ap;
   int rc;
 
   va_start(ap, format);
 
 #if 0
   try {
 #endif
 #if !defined(WIN32)
     char* newFormat = ConvertFormatForNarrowPrintf(format);
     rc = vfprintf(file, newFormat, ap);
     delete []newFormat;
 #else
     rc = vfprintf(file, format, ap);
 #endif
 #if 0
   } catch(...) {
     rc = fputs(format, file);
   }
 #endif
   va_end(ap);
 
   return rc;
 }
 
 
 SWIPRINTF_API int SWIfwprintf(FILE* file, const wchar_t* format, ...)
 {
   va_list ap;
   int rc;
 
   va_start(ap, format);
 
 #if !defined(WIN32)
   char* newFormat = ConvertWideFormatForNarrowPrintf(format);
   rc = vfprintf(file, newFormat, ap);
   delete [] newFormat;
 #else
   rc = vfwprintf(file, format, ap);
 #endif
 
   va_end(ap);
 
   return rc;
 }
 
 
 SWIPRINTF_API int SWIsprintf(char* str, size_t maxlen, const char* format, ...)
 {
   va_list ap;
   int rc;
 
   va_start(ap, format);
   rc = SWIvsprintf(str, maxlen, format, ap);
   va_end(ap);
 
   return rc;
 }
 
 
   // if maxlen == 0, wcs is untouched, returns -1
   // if there is overflow, returns -1 and maxlen-1 # of chars are written
 SWIPRINTF_API int SWIswprintf (wchar_t* wcs, size_t maxlen,
 			       const wchar_t* format, ...)
 {
   va_list ap;
   int rc;
   wchar_t strbuf[200]; // try to avoid malloc
   wchar_t *tstr;
 
   if (maxlen < 1)
     return -1;
 
   if ( maxlen >= 200 ) {
     tstr = (wchar_t *)malloc( (maxlen+1)*sizeof(wchar_t) );
     if (tstr == NULL) {
       //FIXME-- can't log, how can we notify user?
       return -1;
     }
   } else {
     tstr = strbuf;
   }
 
   va_start(ap, format);
   rc = SWIvswprintf(tstr, maxlen, format, ap);
   va_end(ap);
 
   wcscpy( wcs,tstr );
 
   if ( tstr != strbuf ) free(tstr);
 
   return rc;
 }
 
 
 /*
  *  OS specific functions below, UNIX variants start first
  */
 
 #ifndef WIN32
 static int wcs2str(const wchar_t *wcs, char *str)
 {
   int len, i;
 
   len = wcslen(wcs);
   for (i = 0; i < len; i++)
     str[i] = w2c(wcs[i]);
   str[i] = '\0';
 
   return len;
 }
 
 static int str2wcs(const char *str, wchar_t *wcs)
 {
   int len, i;
 
   len = strlen(str);
   for (i = 0; i < len; i++)
     wcs[i] = (wchar_t)str[i];
   wcs[i] = L'\0';
 
   return len;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -