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

📄 storechr.cpp

📁 This software performs code conversion of Chinese characters, including GB2312/GBK and BIG5. It a
💻 CPP
字号:
// storechr.cpp

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "msc.h"
#include "storechr.h"



//////////////////////////////
//          ANSI斉          //
//////////////////////////////



// 僶僢僼傽偺枛旜偵暥帤傪奿擺偡傞乮ANSI斉乯
// C 偑 EOF 側傜僶僢僼傽偺僒僀僘偩偗憹傗偡
// 惉岟偟偨傜僶僢僼傽傪曉偡丄幐攕偟偨傜 STOREDCHARS_ERROR 傪曉偡
STOREDCHARSA *StoreCharsA ( int C, STOREDCHARSA *StoredChars ) {

   if ( StoredChars == STOREDCHARS_ERROR ) return (STOREDCHARSA*) STOREDCHARS_ERROR ;

   if ( ! StoredChars ) {
      StoredChars = (STOREDCHARSA*) malloc ( sizeof(STOREDCHARSA) ) ;
      if ( ! StoredChars ) return (STOREDCHARSA*) STOREDCHARS_ERROR ;
      memzero ( StoredChars, sizeof(STOREDCHARSA) ) ;
   }

   if ( (size_t)( StoredChars->szEnd - StoredChars->szStart + 1 ) >= StoredChars->dwBufferSize ) {
      size_t dwNewBufferSize = ( StoredChars->dwBufferSize ) ? StoredChars->dwBufferSize * 4 : 0x100 ;
      char *szNewBuffer = (char*) realloc ( StoredChars->szStart, dwNewBufferSize * sizeof(char) ) ;
      if ( ! szNewBuffer || dwNewBufferSize < StoredChars->dwBufferSize ) {
         FreeStoredCharsA ( StoredChars ) ;
         return (STOREDCHARSA*) STOREDCHARS_ERROR ;
      }
      if ( ! StoredChars->szStart ) *szNewBuffer = 0 ;
      char *szOldBuffer = StoredChars->szStart ;
      StoredChars->szStart = szNewBuffer ;
      StoredChars->szWrite += szNewBuffer - szOldBuffer ;
      StoredChars->szEnd += szNewBuffer - szOldBuffer ;
      StoredChars->dwBufferSize = dwNewBufferSize ;
   }

   if ( C != EOF ) {
      *( StoredChars->szWrite ) = C ;
      StoredChars->szWrite ++ ;
      *( StoredChars->szWrite ) = 0 ;
   }

   StoredChars->szEnd ++ ;

   return StoredChars ;
}



// 僶僢僼傽偺枛旜偵暋悢偺暥帤傪奿擺偡傞乮ANSI斉乯
// 惉岟偟偨傜僶僢僼傽傪曉偡丄幐攕偟偨傜 STOREDCHARS_ERROR 傪曉偡
STOREDCHARSA *StoreCharsStringA ( const char *szString, STOREDCHARSA *StoredChars ) {
   for ( ; *szString ; szString ++ ) {
      StoredChars = StoreCharsA ( *szString, StoredChars ) ;
   }
   return StoredChars ;
}



// 僶僢僼傽偺暥帤楍傪嬻偵偡傞乮ANSI斉乯
void ClearStoredCharsA ( STOREDCHARSA *StoredChars ) {
   if ( ! StoredChars || StoredChars == STOREDCHARS_ERROR ) return ;
   StoredChars->szWrite = StoredChars->szStart ;
   StoredChars->szEnd = StoredChars->szStart ;
   *StoredChars->szStart = 0 ;
}



// 僶僢僼傽傪夝曻偡傞乮ANSI斉乯
void FreeStoredCharsA ( STOREDCHARSA *StoredChars ) {
   if ( ! StoredChars || StoredChars == STOREDCHARS_ERROR ) return ;
   free ( StoredChars->szStart ) ;
   free ( StoredChars ) ;
}



// 僶僢僼傽偺愭摢傾僪儗僗傪曉偡乮ANSI斉乯
// 幐攕偟偨傜挿偝 0 偺暥帤楍傪曉偡
char *GetStoredCharsA ( STOREDCHARSA *StoredChars ) {
   static char szDummy [] = "" ;
   if ( ! StoredChars || StoredChars == STOREDCHARS_ERROR ) return szDummy ;
   return StoredChars->szStart ;
}



// 僶僢僼傽偺愭摢偵暥帤傪奿擺偡傞乮ANSI斉乯
// 僄儔乕側傜 EOF 傪曉偡
STOREDCHARSA *PushStoredCharsA ( int C, STOREDCHARSA *StoredChars ) {

   StoredChars = StoreCharsA ( C, StoredChars ) ;
   if ( StoredChars == STOREDCHARS_ERROR ) return (STOREDCHARSA*) STOREDCHARS_ERROR ;

   for ( char *p = StoredChars->szWrite - 2 ; p >= StoredChars->szStart ; p -- ) *( p + 1 ) = *p ;

   *StoredChars->szStart = C ;

   return StoredChars ;
}



// 僶僢僼傽偺愭摢偺暥帤傪曉偟僶僢僼傽傪弅傔傞乮ANSI斉乯
// 僄儔乕側傜 EOF 傪曉偡
int PopStoredCharsA ( STOREDCHARSA *StoredChars ) {

   if ( ! StoredChars || StoredChars == STOREDCHARS_ERROR ) return EOF ;
   if ( StoredChars->szWrite <= StoredChars->szStart ) return EOF ;

   int C = (unsigned char) *StoredChars->szStart ;
   for ( char *p = StoredChars->szStart ; p < StoredChars->szWrite ; p ++ ) *p = *( p + 1 ) ;
   StoredChars->szWrite -- ;
   StoredChars->szEnd -- ;

   return C ;
}



//////////////////////////////
//         UNICODE斉        //
//////////////////////////////



// 僶僢僼傽偺枛旜偵暥帤傪奿擺偡傞乮UNICODE斉乯
// C 偑 EOF 側傜僶僢僼傽偺僒僀僘偩偗憹傗偡
// 惉岟偟偨傜僶僢僼傽傪曉偡丄幐攕偟偨傜 STOREDCHARS_ERROR 傪曉偡
STOREDCHARSW *StoreCharsW ( int C, STOREDCHARSW *StoredChars ) {

   if ( StoredChars == STOREDCHARS_ERROR ) return (STOREDCHARSW*) STOREDCHARS_ERROR ;

   if ( ! StoredChars ) {
      StoredChars = (STOREDCHARSW*) malloc ( sizeof(STOREDCHARSW) ) ;
      if ( ! StoredChars ) return (STOREDCHARSW*) STOREDCHARS_ERROR ;
      memzero ( StoredChars, sizeof(STOREDCHARSW) ) ;
   }

   if ( (size_t)( StoredChars->szEnd - StoredChars->szStart + 1 ) >= StoredChars->dwBufferSize ) {
      size_t dwNewBufferSize = ( StoredChars->dwBufferSize ) ? StoredChars->dwBufferSize * 4 : 0x100 ;
      wchar_t *szNewBuffer = (wchar_t*) realloc ( StoredChars->szStart, dwNewBufferSize * sizeof(wchar_t) ) ;
      if ( ! szNewBuffer || dwNewBufferSize < StoredChars->dwBufferSize ) {
         FreeStoredCharsW ( StoredChars ) ;
         return (STOREDCHARSW*) STOREDCHARS_ERROR ;
      }
      if ( ! StoredChars->szStart ) *szNewBuffer = 0 ;
      wchar_t *szOldBuffer = StoredChars->szStart ;
      StoredChars->szStart = szNewBuffer ;
      StoredChars->szWrite += szNewBuffer - szOldBuffer ;
      StoredChars->szEnd += szNewBuffer - szOldBuffer ;
      StoredChars->dwBufferSize = dwNewBufferSize ;
   }

   if ( C != EOF ) {
      *( StoredChars->szWrite ) = C ;
      StoredChars->szWrite ++ ;
      *( StoredChars->szWrite ) = 0 ;
   }

   StoredChars->szEnd ++ ;

   return StoredChars ;
}



// 僶僢僼傽偺枛旜偵暋悢偺暥帤傪奿擺偡傞乮UNICODE斉乯
// 惉岟偟偨傜僶僢僼傽傪曉偡丄幐攕偟偨傜 STOREDCHARS_ERROR 傪曉偡
STOREDCHARSW *StoreCharsStringW ( const wchar_t *szString, STOREDCHARSW *StoredChars ) {
   for ( ; *szString ; szString ++ ) {
      StoredChars = StoreCharsW ( *szString, StoredChars ) ;
   }
   return StoredChars ;
}



// 僶僢僼傽偺暥帤楍傪嬻偵偡傞乮UNICODE斉乯
void ClearStoredCharsW ( STOREDCHARSW *StoredChars ) {
   if ( ! StoredChars || StoredChars == STOREDCHARS_ERROR ) return ;
   StoredChars->szWrite = StoredChars->szStart ;
   StoredChars->szEnd = StoredChars->szStart ;
   *StoredChars->szStart = 0 ;
}



// 僶僢僼傽傪夝曻偡傞乮UNICODE斉乯
void FreeStoredCharsW ( STOREDCHARSW *StoredChars ) {
   if ( ! StoredChars || StoredChars == STOREDCHARS_ERROR ) return ;
   free ( StoredChars->szStart ) ;
   free ( StoredChars ) ;
}



// 僶僢僼傽偺愭摢傾僪儗僗傪曉偡乮UNICODE斉乯
// 幐攕偟偨傜挿偝 0 偺暥帤楍傪曉偡
wchar_t *GetStoredCharsW ( STOREDCHARSW *StoredChars ) {
   static wchar_t szDummy [] = L"" ;
   if ( ! StoredChars || StoredChars == STOREDCHARS_ERROR ) return szDummy ;
   return StoredChars->szStart ;
}



// 僶僢僼傽偺愭摢偵暥帤傪奿擺偡傞乮UNICODE斉乯
// 僄儔乕側傜 EOF 傪曉偡
STOREDCHARSW *PushStoredCharsW ( int C, STOREDCHARSW *StoredChars ) {

   StoredChars = StoreCharsW ( C, StoredChars ) ;
   if ( StoredChars == STOREDCHARS_ERROR ) return (STOREDCHARSW*) STOREDCHARS_ERROR ;

   for ( wchar_t *p = StoredChars->szWrite - 2 ; p >= StoredChars->szStart ; p -- ) *( p + 1 ) = *p ;

   *StoredChars->szStart = C ;

   return StoredChars ;
}



// 僶僢僼傽偺愭摢偺暥帤傪曉偟僶僢僼傽傪弅傔傞乮UNICODE斉乯
// 僄儔乕側傜 EOF 傪曉偡
int PopStoredCharsW ( STOREDCHARSW *StoredChars ) {

   if ( ! StoredChars || StoredChars == STOREDCHARS_ERROR ) return EOF ;
   if ( StoredChars->szWrite <= StoredChars->szStart ) return EOF ;

   int C = *StoredChars->szStart ;
   for ( wchar_t *p = StoredChars->szStart ; p < StoredChars->szWrite ; p ++ ) *p = *( p + 1 ) ;
   StoredChars->szWrite -- ;
   StoredChars->szEnd -- ;

   return C ;
}



//////////////////////////////
//          UTF32斉         //
//////////////////////////////



// 僶僢僼傽偺枛旜偵暥帤傪奿擺偡傞乮UTF32斉乯
// C 偑 EOF 側傜僶僢僼傽偺僒僀僘偩偗憹傗偡
// 惉岟偟偨傜僶僢僼傽傪曉偡丄幐攕偟偨傜 STOREDCHARS_ERROR 傪曉偡
STOREDCHARSW32 *StoreCharsW32 ( int C, STOREDCHARSW32 *StoredChars ) {

   if ( StoredChars == STOREDCHARS_ERROR ) return (STOREDCHARSW32*) STOREDCHARS_ERROR ;

   if ( ! StoredChars ) {
      StoredChars = (STOREDCHARSW32*) malloc ( sizeof(STOREDCHARSW32) ) ;
      if ( ! StoredChars ) return (STOREDCHARSW32*) STOREDCHARS_ERROR ;
      memzero ( StoredChars, sizeof(STOREDCHARSW32) ) ;
   }

   if ( (size_t)( StoredChars->szEnd - StoredChars->szStart + 1 ) >= StoredChars->dwBufferSize ) {
      size_t dwNewBufferSize = ( StoredChars->dwBufferSize ) ? StoredChars->dwBufferSize * 4 : 0x100 ;
      wchar32_t *szNewBuffer = (wchar32_t*) realloc ( StoredChars->szStart, dwNewBufferSize * sizeof(wchar32_t) ) ;
      if ( ! szNewBuffer || dwNewBufferSize < StoredChars->dwBufferSize ) {
         FreeStoredCharsW32 ( StoredChars ) ;
         return (STOREDCHARSW32*) STOREDCHARS_ERROR ;
      }
      if ( ! StoredChars->szStart ) *szNewBuffer = 0 ;
      wchar32_t *szOldBuffer = StoredChars->szStart ;
      StoredChars->szStart = szNewBuffer ;
      StoredChars->szWrite += szNewBuffer - szOldBuffer ;
      StoredChars->szEnd += szNewBuffer - szOldBuffer ;
      StoredChars->dwBufferSize = dwNewBufferSize ;
   }

   if ( C != EOF ) {
      *( StoredChars->szWrite ) = C ;
      StoredChars->szWrite ++ ;
      *( StoredChars->szWrite ) = 0 ;
   }

   StoredChars->szEnd ++ ;

   return StoredChars ;
}



// 僶僢僼傽偺枛旜偵暋悢偺暥帤傪奿擺偡傞乮UTF32斉乯
// 惉岟偟偨傜僶僢僼傽傪曉偡丄幐攕偟偨傜 STOREDCHARS_ERROR 傪曉偡
STOREDCHARSW32 *StoreCharsStringW32 ( const wchar32_t *szString, STOREDCHARSW32 *StoredChars ) {
   for ( ; *szString ; szString ++ ) {
      StoredChars = StoreCharsW32 ( *szString, StoredChars ) ;
   }
   return StoredChars ;
}



// 僶僢僼傽偺暥帤楍傪嬻偵偡傞乮UTF32斉乯
void ClearStoredCharsW32 ( STOREDCHARSW32 *StoredChars ) {
   if ( ! StoredChars || StoredChars == STOREDCHARS_ERROR ) return ;
   StoredChars->szWrite = StoredChars->szStart ;
   StoredChars->szEnd = StoredChars->szStart ;
   *StoredChars->szStart = 0 ;
}



// 僶僢僼傽傪夝曻偡傞乮UTF32斉乯
void FreeStoredCharsW32 ( STOREDCHARSW32 *StoredChars ) {
   if ( ! StoredChars || StoredChars == STOREDCHARS_ERROR ) return ;
   free ( StoredChars->szStart ) ;
   free ( StoredChars ) ;
}



// 僶僢僼傽偺愭摢傾僪儗僗傪曉偡乮UTF32斉乯
// 幐攕偟偨傜挿偝 0 偺暥帤楍傪曉偡
wchar32_t *GetStoredCharsW32 ( STOREDCHARSW32 *StoredChars ) {
   static wchar32_t szDummy [] = { 0 } ;
   if ( ! StoredChars || StoredChars == STOREDCHARS_ERROR ) return szDummy ;
   return StoredChars->szStart ;
}



// 僶僢僼傽偺愭摢偵暥帤傪奿擺偡傞乮UTF32斉乯
// 僄儔乕側傜 EOF 傪曉偡
STOREDCHARSW32 *PushStoredCharsW32 ( int C, STOREDCHARSW32 *StoredChars ) {

   StoredChars = StoreCharsW32 ( C, StoredChars ) ;
   if ( StoredChars == STOREDCHARS_ERROR ) return (STOREDCHARSW32*) STOREDCHARS_ERROR ;

   for ( wchar32_t *p = StoredChars->szWrite - 2 ; p >= StoredChars->szStart ; p -- ) *( p + 1 ) = *p ;

   *StoredChars->szStart = C ;

   return StoredChars ;
}



// 僶僢僼傽偺愭摢偺暥帤傪曉偟僶僢僼傽傪弅傔傞乮UTF32斉乯
// 僄儔乕側傜 EOF 傪曉偡
int PopStoredCharsW32 ( STOREDCHARSW32 *StoredChars ) {

   if ( ! StoredChars || StoredChars == STOREDCHARS_ERROR ) return EOF ;
   if ( StoredChars->szWrite <= StoredChars->szStart ) return EOF ;

   int C = *StoredChars->szStart ;
   for ( wchar32_t *p = StoredChars->szStart ; p < StoredChars->szWrite ; p ++ ) *p = *( p + 1 ) ;
   StoredChars->szWrite -- ;
   StoredChars->szEnd -- ;

   return C ;
}



⌨️ 快捷键说明

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