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

📄 storestr.cpp

📁 This software performs code conversion of Chinese characters, including GB2312/GBK and BIG5. It a
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// storestr.cpp

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "msc.h"
#include "storestr.h"


#define DEBUG_STORESTR  0


// 弶婜僶僢僼傽僒僀僘
#define INIT_BUFFER_SIZE 0x400

// 栺侾儊僈僶僀僩
#define ONE_MEGA_BYTE 0x100000

// qsort ()
typedef int ( __cdecl *QSORT_COMPARE_FUNCTION ) ( const void *, const void * ) ;


// This function is not supported in Win95
#define StrCmpLogicalW StrCmpLogicalW_
typedef int ( WINAPI *STRCMPLOGICALW ) ( LPCWSTR psz1, LPCWSTR psz2 ) ;
static HINSTANCE hShlwapi  ;
static STRCMPLOGICALW StrCmpLogicalW ;

static int LoadShlwapiDll ( void ) ;
static int FreeLibraryOnDetach ( void ) ;



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



// 暥帤楍傪僶僢僼傽偵偨傔傞乮ANSI斉乯
// 僶僢僼傽偺妋曐偵幐攕偟偨偲偒偼丄僶僢僼傽傪僋儕傾偟 STOREDSTRINGS_ERROR 傪曉偡
// 僴儞僪儖傪暵偠傞偵偼 FreeStoredStrings () 傪屇傃弌偡偙偲
// 暥帤楍傪庢傝弌偡偵偼 GetFirstString () 偍傛傃 GetNextString () 傪屇傃弌偡偙偲
// 専嶕偟偨梫慺偺屄悢偼 GetStringCount () 傪傛傃偩偡偙偲
// 僴儞僪儖傪暵偠傞傑偱偼丄暥帤楍傪捛壛偡傞偙偲偑偱偒傞
STOREDSTRINGSA *StoreStringsA ( STOREDSTRINGSA *StoredStrings, const char *szString ) {

   if ( StoredStrings == STOREDSTRINGS_ERROR ) return (STOREDSTRINGSA*) STOREDSTRINGS_ERROR ;

   if ( ! StoredStrings ) {
      StoredStrings = (STOREDSTRINGSA*) malloc ( sizeof(STOREDSTRINGSA) ) ;
      if ( ! StoredStrings ) return (STOREDSTRINGSA*) STOREDSTRINGS_ERROR ;
      memzero ( StoredStrings, sizeof(STOREDSTRINGSA) ) ;
   }

   if ( szString ) {

      size_t nStringLen = strlen ( szString ) ;
      if ( ! nStringLen ) return StoredStrings ;
      size_t dwRequiredBufferSize = ( StoredStrings->szWrite - StoredStrings->szStart + nStringLen + 2 ) * sizeof(char) ;

      if ( StoredStrings->dwBufferSize < dwRequiredBufferSize ) {

         size_t dwNewBufferSize ;
         if ( ! StoredStrings->dwBufferSize )                    dwNewBufferSize = INIT_BUFFER_SIZE ;
         else if ( StoredStrings->dwBufferSize < ONE_MEGA_BYTE ) dwNewBufferSize = StoredStrings->dwBufferSize * 4 ;
         else                                                    dwNewBufferSize = StoredStrings->dwBufferSize + ONE_MEGA_BYTE ;

         if ( dwNewBufferSize < dwRequiredBufferSize ) dwNewBufferSize = dwRequiredBufferSize ;

         if ( DEBUG_STORESTR ) {
            FILE *Fout = fopen ( "storestr.debug.txt", "ac" ) ;
            fprintf ( Fout, "%p, %p, %s\n", (void*) StoredStrings->dwBufferSize, (void*) dwNewBufferSize, szString ) ;
            fclose ( Fout ) ;
         }

         char *szNewBuffer = (char*) realloc ( StoredStrings->szStart, dwNewBufferSize ) ;

         if ( ! szNewBuffer || StoredStrings->dwBufferSize > dwNewBufferSize ) {
            FreeStoredStringsA ( StoredStrings ) ;
            return (STOREDSTRINGSA*) STOREDSTRINGS_ERROR ;
         }

         char *szOldBuffer = StoredStrings->szStart ;
         StoredStrings->szStart = szNewBuffer ;
         StoredStrings->szWrite += szNewBuffer - szOldBuffer ;
         StoredStrings->szRead += szNewBuffer - szOldBuffer ;
         StoredStrings->szSortStart += szNewBuffer - szOldBuffer ;
         StoredStrings->dwBufferSize = dwNewBufferSize ;
      }

      strcpy ( StoredStrings->szWrite, szString ) ;
      StoredStrings->szWrite += nStringLen + 1 ;
      *( StoredStrings->szWrite ) = 0 ;
      StoredStrings->nCount ++ ;
   }

   return StoredStrings ;
}



// 僶僢僼傽傪夝曻偡傞乮ANSI斉乯
void FreeStoredStringsA ( STOREDSTRINGSA *StoredStrings ) {
   if ( ! StoredStrings || StoredStrings == STOREDSTRINGS_ERROR ) return ;
   free ( StoredStrings->szStart ) ;
   free ( StoredStrings ) ;
   return ;
}



// 僶僢僼傽偐傜嵟弶偺暥帤楍傪曉偡乮ANSI斉乯
// 暥帤楍偑側偗傟偽丄NULL 傪曉偡
const char *GetFirstStringA ( STOREDSTRINGSA *StoredStrings ) {
   if ( ! StoredStrings || StoredStrings == STOREDSTRINGS_ERROR ) return NULL ;
   return StoredStrings->szRead = StoredStrings->szStart ;
}



// 僶僢僼傽偐傜師偺暥帤楍傪曉偡乮ANSI斉乯
// 暥帤楍偑側偗傟偽丄NULL 傪曉偡
// GetFirstString () 傪屇傃弌偡慜側傜偽丄摦嶌偼晄掕
const char *GetNextStringA ( STOREDSTRINGSA *StoredStrings ) {
   if ( ! StoredStrings || StoredStrings == STOREDSTRINGS_ERROR ) return NULL ;
   char *szNext = strend ( StoredStrings->szRead ) + 1 ;
   if ( *szNext ) return StoredStrings->szRead = szNext ;
   return NULL ;
}



// 僶僢僼傽偺暥帤楍偺屄悢傪曉偡乮ANSI斉乯
size_t GetStringCountA ( STOREDSTRINGSA *StoredStrings ) {
   if ( ! StoredStrings || StoredStrings == STOREDSTRINGS_ERROR ) return 0 ;
   return StoredStrings->nCount ;
}



// 僶僢僼傽撪偺暥帤楍傪僜乕僩偡傞乮ANSI斉乯
// CompareFuntion 偵偼慻傒崬傒偺斾妑娭悢傑偨偼儐乕僓掕媊偺斾妑娭悢傪巜掕
// 帠慜偵 MarkSortStart () 偑屇傃弌偝傟偨傜丄偦傟埲崀偵捛壛偝傟偨暥帤楍偺傒僜乕僩
int SortStoredStringsA ( STOREDSTRINGSA *StoredStrings, int ( __cdecl *CompareFunction ) ( const char **, const char ** ) ) {

   STOREDSTRINGSA *StoredStringsTmp = NULL ;
   size_t nCount ;
   int nResult = 1 ;

   char *szStart ;
   size_t nItem ;

   if ( ! StoredStrings ) return 0 ;
   if ( ! CompareFunction ) return 0 ;

   if ( CompareFunction == CompareStoredStringsNumericalA || CompareFunction == CompareStoredStringsNumericalRevA ) LoadShlwapiDll () ;

   if ( StoredStrings->szSortStart ) {
      szStart = StoredStrings->szSortStart ;
      nItem = StoredStrings->nCount - StoredStrings->nSortStartCount ;
   }
   else {
      szStart = StoredStrings->szStart ;
      nItem = StoredStrings->nCount ;
   }

   if ( nItem <= 1 ) return 0 ;

   char **pPointerToName = (char**) malloc ( ( nItem ) * sizeof(char*) ) ;
   if ( ! pPointerToName ) return 1 ;

   char *szStringGet = szStart ;
   for ( nCount = 0 ; nCount < nItem ; nCount ++ ) {
      pPointerToName [ nCount ] = szStringGet ;
      szStringGet = strend ( szStringGet ) + 1 ;
   }

   qsort ( pPointerToName, nItem, sizeof(char*), (QSORT_COMPARE_FUNCTION) CompareFunction ) ;

   for ( nCount = 0 ; nCount < nItem ; nCount ++ ) {
      StoredStringsTmp = StoreStringsA ( StoredStringsTmp, pPointerToName [ nCount ] ) ;
      if ( StoredStringsTmp == STOREDSTRINGS_ERROR ) break ;
   }
   if ( StoredStringsTmp && StoredStringsTmp != STOREDSTRINGS_ERROR ) {
      memmove ( szStart, GetFirstStringA ( StoredStringsTmp ), ( szStringGet - szStart ) * sizeof(char) ) ;
      FreeStoredStringsA ( StoredStringsTmp ) ;
      nResult = 0 ;
   }

   free ( pPointerToName ) ;
   return nResult ;
}



// 偙傟埲崀偵捛壛偝傟偨暥帤楍偺傒 SortStoredStrings () 偱僜乕僩乮ANSI斉乯
int MarkSortStartA ( STOREDSTRINGSA *StoredStrings ) {
   if ( ! StoredStrings || StoredStrings == STOREDSTRINGS_ERROR ) return 1 ;
   StoredStrings->szSortStart = StoredStrings->szWrite ;
   StoredStrings->nSortStartCount = StoredStrings->nCount ;
   return 0 ;
}



// MarkSortStart偱巜掕偝傟偨儅乕僋傪夝彍乮ANSI斉乯
int UnmarkSortStartA ( STOREDSTRINGSA *StoredStrings ) {
   if ( ! StoredStrings || StoredStrings == STOREDSTRINGS_ERROR ) return 1 ;
   StoredStrings->szSortStart = NULL ;
   StoredStrings->nSortStartCount = 0 ;
   return 0 ;
}



// 僶僢僼傽偐傜儅乕僋偝傟偨嵟弶偺暥帤楍傪曉偡乮ANSI斉乯
// 暥帤楍偑側偗傟偽丄NULL 傪曉偡
const char *GetFirstMarkedStringA ( STOREDSTRINGSA *StoredStrings ) {
   if ( ! StoredStrings || StoredStrings == STOREDSTRINGS_ERROR ) return NULL ;
   if ( StoredStrings->szSortStart ) return StoredStrings->szRead = StoredStrings->szSortStart ;
   else                              return StoredStrings->szRead = StoredStrings->szStart ;
}



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



// 暥帤楍傪僶僢僼傽偵偨傔傞乮UNICODE斉乯
// 僶僢僼傽偺妋曐偵幐攕偟偨偲偒偼丄僶僢僼傽傪僋儕傾偟 STOREDSTRINGS_ERROR 傪曉偡
// 僴儞僪儖傪暵偠傞偵偼 FreeStoredStrings () 傪屇傃弌偡偙偲
// 暥帤楍傪庢傝弌偡偵偼 GetFirstString () 偍傛傃 GetNextString () 傪屇傃弌偡偙偲
// 専嶕偟偨梫慺偺屄悢偼 GetStringCount () 傪傛傃偩偡偙偲
// 僴儞僪儖傪暵偠傞傑偱偼丄暥帤楍傪捛壛偡傞偙偲偑偱偒傞
STOREDSTRINGSW *StoreStringsW ( STOREDSTRINGSW *StoredStrings, const wchar_t *szString ) {

   if ( StoredStrings == STOREDSTRINGS_ERROR ) return (STOREDSTRINGSW*) STOREDSTRINGS_ERROR ;

   if ( ! StoredStrings ) {
      StoredStrings = (STOREDSTRINGSW*) malloc ( sizeof(STOREDSTRINGSW) ) ;
      if ( ! StoredStrings ) return (STOREDSTRINGSW*) STOREDSTRINGS_ERROR ;
      memzero ( StoredStrings, sizeof(STOREDSTRINGSW) ) ;
   }

   if ( szString ) {

      size_t nStringLen = wcslen ( szString ) ;
      if ( ! nStringLen ) return StoredStrings ;
      size_t dwRequiredBufferSize = ( StoredStrings->szWrite - StoredStrings->szStart + nStringLen + 2 ) * sizeof(wchar_t) ;

      if ( StoredStrings->dwBufferSize < dwRequiredBufferSize ) {

         size_t dwNewBufferSize ;
         if ( ! StoredStrings->dwBufferSize )                    dwNewBufferSize = INIT_BUFFER_SIZE ;
         else if ( StoredStrings->dwBufferSize < ONE_MEGA_BYTE ) dwNewBufferSize = StoredStrings->dwBufferSize * 4 ;
         else                                                    dwNewBufferSize = StoredStrings->dwBufferSize + ONE_MEGA_BYTE ;

         if ( dwNewBufferSize < dwRequiredBufferSize ) dwNewBufferSize = dwRequiredBufferSize ;

         if ( DEBUG_STORESTR ) {
            FILE *Fout = fopen ( "storestr.debug.txt", "ac" ) ;
            char *szTmp = w2adup ( szString ) ;
            fprintf ( Fout, "%p, %p, %s\n", (void*) StoredStrings->dwBufferSize, (void*) dwNewBufferSize, szTmp ) ;
            free ( szTmp ) ;
            fclose ( Fout ) ;
         }

         wchar_t *szNewBuffer = (wchar_t*) realloc ( StoredStrings->szStart, dwNewBufferSize ) ;

         if ( ! szNewBuffer || StoredStrings->dwBufferSize > dwNewBufferSize ) {
            FreeStoredStringsW ( StoredStrings ) ;
            return (STOREDSTRINGSW*) STOREDSTRINGS_ERROR ;
         }

         wchar_t *szOldBuffer = StoredStrings->szStart ;
         StoredStrings->szStart = szNewBuffer ;
         StoredStrings->szWrite += szNewBuffer - szOldBuffer ;
         StoredStrings->szRead += szNewBuffer - szOldBuffer ;
         StoredStrings->szSortStart += szNewBuffer - szOldBuffer ;
         StoredStrings->dwBufferSize = dwNewBufferSize ;
      }

      wcscpy ( StoredStrings->szWrite, szString ) ;
      StoredStrings->szWrite += nStringLen + 1 ;
      *( StoredStrings->szWrite ) = 0 ;
      StoredStrings->nCount ++ ;
   }

   return StoredStrings ;
}



// 僶僢僼傽傪夝曻偡傞
void FreeStoredStringsW ( STOREDSTRINGSW *StoredStrings ) {
   if ( ! StoredStrings || StoredStrings == STOREDSTRINGS_ERROR ) return ;
   free ( StoredStrings->szStart ) ;
   free ( StoredStrings ) ;
   return ;
}



// 僶僢僼傽偐傜嵟弶偺暥帤楍傪曉偡乮UNICODE斉乯
// 暥帤楍偑側偗傟偽丄NULL 傪曉偡
const wchar_t *GetFirstStringW ( STOREDSTRINGSW *StoredStrings ) {
   if ( ! StoredStrings || StoredStrings == STOREDSTRINGS_ERROR ) return NULL ;
   return StoredStrings->szRead = StoredStrings->szStart ;
}



// 僶僢僼傽偐傜師偺暥帤楍傪曉偡乮UNICODE斉乯
// 暥帤楍偑側偗傟偽丄NULL 傪曉偡
// GetFirstString () 傪屇傃弌偡慜側傜偽丄摦嶌偼晄掕
const wchar_t *GetNextStringW ( STOREDSTRINGSW *StoredStrings ) {
   if ( ! StoredStrings || StoredStrings == STOREDSTRINGS_ERROR ) return NULL ;
   wchar_t *szNext = wcsend ( StoredStrings->szRead ) + 1 ;
   if ( *szNext ) return StoredStrings->szRead = szNext ;
   return NULL ;
}



// 僶僢僼傽偺暥帤楍偺屄悢傪曉偡乮UNICODE斉乯
size_t GetStringCountW ( STOREDSTRINGSW *StoredStrings ) {
   if ( ! StoredStrings || StoredStrings == STOREDSTRINGS_ERROR ) return 0 ;
   return StoredStrings->nCount ;
}



// 僶僢僼傽撪偺暥帤楍傪僜乕僩偡傞乮UNICODE斉乯
// CompareFuntion 偵偼慻傒崬傒偺斾妑娭悢傑偨偼儐乕僓掕媊偺斾妑娭悢傪巜掕
// 帠慜偵 MarkSortStart () 偑屇傃弌偝傟偨傜丄偦傟埲崀偵捛壛偝傟偨暥帤楍偺傒僜乕僩
int SortStoredStringsW ( STOREDSTRINGSW *StoredStrings, int ( __cdecl *CompareFunction ) ( const wchar_t **, const wchar_t ** ) ) {

   STOREDSTRINGSW *StoredStringsTmp = NULL ;
   size_t nCount ;
   int nResult = 1 ;

   wchar_t *szStart ;
   size_t nItem ;

   if ( ! StoredStrings ) return 0 ;
   if ( ! CompareFunction ) return 0 ;

   if ( CompareFunction == CompareStoredStringsNumericalW || CompareFunction == CompareStoredStringsNumericalRevW ) LoadShlwapiDll () ;

   if ( StoredStrings->szSortStart ) {
      szStart = StoredStrings->szSortStart ;
      nItem = StoredStrings->nCount - StoredStrings->nSortStartCount ;
   }
   else {
      szStart = StoredStrings->szStart ;
      nItem = StoredStrings->nCount ;

⌨️ 快捷键说明

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