'///////////////////////////////////////////////////////////////////////////////
'
'    (C/C++)     Unicode.
'  1.5 ( 08.07.2007)
'
'///////////////////////////////////////////////////////////////////////////////

'-------------------------------------------------------------------------------
'      (  )
'---
function IsQuotes
  dim curLine, curCol, pos, buf, count

  curLine = ActiveDocument.Selection.CurrentLine
  curCol = ActiveDocument.Selection.CurrentColumn

  ActiveDocument.Selection.Cancel
  ActiveDocument.Selection.StartOfLine dsFirstText, dsExtend

  count = 0
  pos = 0
  buf = ActiveDocument.Selection

  do while true
    pos = InStr( pos + 1, buf, """", vbTextCompare )
    if not (pos > 0) then
      exit do
    end if

    if pos = 1 then
      count = count + 1
    else
      if Mid(buf, pos - 1, 1) <> "\" then
        count = count + 1
      elseif (pos > 2) and (Mid(buf, pos - 2, 1) = "\") then
        count = count + 1
      end if
    end if
  loop

  IsQuotes = ((count > 0) and ((count Mod 2) <> 0))

  ActiveDocument.Selection.MoveTo curLine, curCol

  if not IsQuotes then
    ActiveDocument.Selection.CharLeft dsExtend

    if ActiveDocument.Selection = "'" then
      ActiveDocument.Selection.CharRight
      ActiveDocument.Selection.CharRight dsExtend

      if ActiveDocument.Selection = "\" then
        ActiveDocument.Selection.CharRight
        ActiveDocument.Selection.CharRight dsExtend
      end if

      ActiveDocument.Selection.CharRight
      ActiveDocument.Selection.CharRight dsExtend

      if ActiveDocument.Selection = "'" then
        IsQuotes = true
      end if
    end if

    ActiveDocument.Selection.MoveTo curLine, curCol
  end if

  if IsQuotes then
    ActiveDocument.Selection.CharRight
  end if
end function

'-------------------------------------------------------------------------------
'      '//'
'---
function IsComment
  dim curLine, curCol

  curLine = ActiveDocument.Selection.CurrentLine
  curCol = ActiveDocument.Selection.CurrentColumn

  ActiveDocument.Selection.Cancel
  ActiveDocument.Selection.StartOfLine dsFirstText, dsExtend

  IsComment = false

  if InStr(1, ActiveDocument.Selection, "//", vbTextCompare) > 0 then
    IsComment = true
  end if

  ActiveDocument.Selection.MoveTo curLine, curCol
end function

'-------------------------------------------------------------------------------
'       
'---
function IsDirective
  dim curLine, curCol

  curLine = ActiveDocument.Selection.CurrentLine
  curCol = ActiveDocument.Selection.CurrentColumn

  ActiveDocument.Selection.Cancel
  ActiveDocument.Selection.StartOfLine dsFirstText, dsExtend

  IsDirective = false

  '   
  dim drt(13)
  drt( 0) = "#include"
  drt( 1) = "#pragma"
  drt( 2) = "DISP_FUNCTION"
  drt( 3) = "DISP_PROPERTY"
  drt( 4) = "DISP_PROPERTY_NOTIFY"
  drt( 5) = "DISP_PROPERTY_EX"
  drt( 6) = "DISP_PROPERTY_PARAM"
  drt( 7) = "DISP_FUNCTION_ID"
  drt( 8) = "DISP_PROPERTY_ID"
  drt( 9) = "DISP_PROPERTY_NOTIFY_ID"
  drt(10) = "DISP_PROPERTY_EX_ID"
  drt(11) = "DISP_PROPERTY_PARAM_ID"
  drt(12) = "DISP_DEFVALUE"
  drt(13) = "DISP_DEFVALUE_ID"

  '  
  for each i in drt
    if InStr(1, ActiveDocument.Selection, i, vbTextCompare) > 0 then
      IsDirective = true
      exit for
    end if
  next

  if not IsDirective then
    ActiveDocument.Selection.SelectLine
    IsDirective = InStr(1, ActiveDocument.Selection, "static char THIS_FILE", vbTextCompare) > 0
  end if

  ActiveDocument.Selection.MoveTo curLine, curCol
end function

'-------------------------------------------------------------------------------
'   
'---
function IgnorePos
  IgnorePos = false

'  if IsQuotes then
'    IgnorePos = true
'    exit function
'  end if

  if IsComment then
    IgnorePos = true
    exit function
  end if

  if IsDirective then
    IgnorePos = true
  end if
end function

'-------------------------------------------------------------------------------
'   
'---
function InitPos
  curLine = ActiveDocument.Selection.CurrentLine
  ActiveDocument.Selection.MoveTo curLine, 1
  InitPos = curLine & ",a," & curLine & "," & 1
end function

'-------------------------------------------------------------------------------
'     
'---
function FreshPos( lastPos )
  dim lastLine, lastCol, curLine, curCol, p, c, startLine, f

  curLine = ActiveDocument.Selection.CurrentLine
  curCol = ActiveDocument.Selection.CurrentColumn

  par = Split( lastPos, "," )
  startLine = Int(par(0))
  f         =     par(1)
  lastLine  = Int(par(2))
  lastCol   = Int(par(3))

  if curLine > lastLine then
    FreshPos = ((curLine > startLine) and (f = "a")) or ((curLine < startLine) and (f = "b"))
  elseif curLine = lastLine then
    FreshPos = ((curCol > lastCol))' and (f = "a"))
  elseif curLine < lastLine then
    FreshPos = ((curLine < startLine) and (f = "a"))
    f = "b"
  end if

  lastPos = startLine & "," & f & "," & curLine & "," & curCol
end function

'-------------------------------------------------------------------------------
'      str  close
'---
function Replace( find, str, close, flags )
  dim run, buf, r, pos, curLine, curCol, endLine, endCol

  endLine = ActiveDocument.Selection.CurrentLine
  endCol = ActiveDocument.Selection.CurrentColumn
  pos = InitPos
  Replace = vbOk

  do while true
    curLine = ActiveDocument.Selection.CurrentLine
    curCol = ActiveDocument.Selection.CurrentColumn
        
    if not ActiveDocument.Selection.FindText(find, flags) then
      exit do
    end if

    if not FreshPos(pos) then
      exit do
    end if

    if not IgnorePos then
      '  .. IgnorePos  
      ActiveDocument.Selection.MoveTo curLine, curCol
      ActiveDocument.Selection.FindText find, flags

      if Len(close) > 0 then
        buf = str + ActiveDocument.Selection + close
      else
        buf = str
      end if

'      r = MsgBox( ":" + vbCr + ActiveDocument.Selection + vbCr + "" + vbCr + buf, vbYesNoCancel + vbQuestion, "ConvertToUnicode" )
      r = MsgBox( buf, vbYesNoCancel + vbQuestion, " " )

      if r = vbYes then 
        ActiveDocument.Selection = buf
      end if

      endLine = ActiveDocument.Selection.CurrentLine
      endCol = ActiveDocument.Selection.CurrentColumn

      if r = vbCancel then
        Replace = vbCancel
        exit do
      end if
    end if
  loop

  ActiveDocument.Selection.MoveTo endLine, endCol
end function

'-------------------------------------------------------------------------------
'   
'---
function NextBox()
  NextBox = vbCancel ' MsgBox( "  ?", vbOKCancel + vbQuestion, "ConvertToUnicode" )
end function

'-------------------------------------------------------------------------------
'DESCRIPTION:    (C/C++)  ANSI  Unicode 
'---
sub ConvertToUnicode()

  if not (ActiveDocument.Type = "Text") then
    MsgBox "   .", vbExclamation, "ConvertToUnicode"
    exit sub
  end if

  if ActiveDocument.ReadOnly then
    MsgBox "    .", vbExclamation, "ConvertToUnicode"
    exit sub
  end if

  '   
  dim curLine, curCol
  startLine = ActiveDocument.Selection.CurrentLine
  startCol = ActiveDocument.Selection.CurrentColumn

  '    
  dim fpt(169)
  fpt(  0) = "main _tmain"
  fpt(  1) = "WinMain _tWinMain"
  fpt(  2) = "environ _tenviron"
  fpt(  3) = "_environ _tenviron"
  fpt(  4) = "__argv __targv"
  fpt(  5) = "printf _tprintf"
  fpt(  6) = "fprintf _ftprintf"
  fpt(  7) = "sprintf _stprintf"
  fpt(  8) = "_snprintf _sntprintf"
  fpt(  9) = "vprintf _vtprintf"
  fpt( 10) = "vfprintf _vftprintf"
  fpt( 11) = "vsprintf _vstprintf"
  fpt( 12) = "_vsnprintf _vsntprintf"
  fpt( 13) = "scanf _tscanf"
  fpt( 14) = "fscanf _ftscanf"
  fpt( 15) = "sscanf _stscanf"
  fpt( 16) = "fgetc _fgettc"
  fpt( 17) = "_fgetchar _fgettchar"
  fpt( 18) = "fgets _fgetts"
  fpt( 19) = "fputc _fputtc"
  fpt( 20) = "_fputchar _fputtchar"
  fpt( 21) = "fputs _fputts"
  fpt( 22) = "getc _gettc"
  fpt( 23) = "getchar _gettchar"
  fpt( 24) = "gets _getts"
  fpt( 25) = "putc _puttc"
  fpt( 26) = "putchar _puttchar"
  fpt( 27) = "puts _putts"
  fpt( 28) = "ungetc _ungettc"
  fpt( 29) = "strtod _tcstod"
  fpt( 30) = "strtol _tcstol"
  fpt( 31) = "strtoul _tcstoul"
  fpt( 32) = "_itoa _itot"
  fpt( 33) = "_ltoa _ltot"
  fpt( 34) = "_ultoa _ultot"
  fpt( 35) = "atoi _ttoi"
  fpt( 36) = "atol _ttol"
  fpt( 37) = "_atoi64 _ttoi64"
  fpt( 38) = "_i64toa _i64tot"
  fpt( 39) = "_ui64toa _ui64tot"
  fpt( 40) = "strcat _tcscat"
  fpt( 41) = "strcpy _tcscpy"
  fpt( 42) = "_strdup _tcsdup"
  fpt( 43) = "strlen _tcslen"
  fpt( 44) = "strxfrm _tcsxfrm"
  fpt( 45) = "strchr _tcschr"
  fpt( 46) = "strcspn _tcscspn"
  fpt( 47) = "strncat _tcsncat"
  fpt( 48) = "strncpy _tcsncpy"
  fpt( 49) = "strpbrk _tcspbrk"
  fpt( 50) = "strrchr _tcsrchr"
  fpt( 51) = "strspn _tcsspn"
  fpt( 52) = "strstr _tcsstr"
  fpt( 53) = "strtok _tcstok"
  fpt( 54) = "_strnset _tcsnset"
  fpt( 55) = "_strrev _tcsrev"
  fpt( 56) = "_strset _tcsset"
  fpt( 57) = "strcmp _tcscmp"
  fpt( 58) = "_stricmp _tcsicmp"
  fpt( 59) = "strncmp _tcsnccmp"
  fpt( 60) = "strncmp _tcsncmp"
  fpt( 61) = "_strnicmp _tcsncicmp"
  fpt( 62) = "_strnicmp _tcsnicmp"
  fpt( 63) = "strcoll _tcscoll"
  fpt( 64) = "_stricoll _tcsicoll"
  fpt( 65) = "_strncoll _tcsnccoll"
  fpt( 66) = "_strncoll _tcsncoll"
  fpt( 67) = "_strnicoll _tcsncicoll"
  fpt( 68) = "_strnicoll _tcsnicoll"
  fpt( 69) = "strlen2 _tcsclen"
  fpt( 70) = "strncat _tcsnccat"
  fpt( 71) = "strncpy _tcsnccpy"
  fpt( 72) = "_strnset _tcsncset"
  fpt( 73) = "_strdec _tcsdec"
  fpt( 74) = "_strinc _tcsinc"
  fpt( 75) = "_strncnt _tcsnbcnt"
  fpt( 76) = "_strncnt _tcsnccnt"
  fpt( 77) = "_strnextc _tcsnextc"
  fpt( 78) = "_strninc _tcsninc"
  fpt( 79) = "_strspnp _tcsspnp"
  fpt( 80) = "_strlwr _tcslwr"
  fpt( 81) = "_strupr _tcsupr"
  fpt( 82) = "strxfrm _tcsxfrm"
  fpt( 83) = "_execl _texecl"
  fpt( 84) = "_execle _texecle"
  fpt( 85) = "_execlp _texeclp"
  fpt( 86) = "_execlpe _texeclpe"
  fpt( 87) = "_execv _texecv"
  fpt( 88) = "_execve _texecve"
  fpt( 89) = "_execvp _texecvp"
  fpt( 90) = "_execvpe _texecvpe"
  fpt( 91) = "_spawnl _tspawnl"
  fpt( 92) = "_spawnle _tspawnle"
  fpt( 93) = "_spawnlp _tspawnlp"
  fpt( 94) = "_spawnlpe _tspawnlpe"
  fpt( 95) = "_spawnv _tspawnv"
  fpt( 96) = "_spawnve _tspawnve"
  fpt( 97) = "_spawnvp _tspawnvp"
  fpt( 98) = "_spawnvpe _tspawnvpe"
  fpt( 99) = "system _tsystem"
  fpt(100) = "asctime _tasctime"
  fpt(101) = "ctime _tctime"
  fpt(102) = "_strdate _tstrdate"
  fpt(103) = "_strtime _tstrtime"
  fpt(104) = "_utime _tutime"
  fpt(105) = "strftime _tcsftime"
  fpt(106) = "_chdir _tchdir"
  fpt(107) = "_getcwd _tgetcwd"
  fpt(108) = "_getdcwd _tgetdcwd"
  fpt(109) = "_mkdir _tmkdir"
  fpt(110) = "_rmdir _trmdir"
  fpt(111) = "_fullpath _tfullpath"
  fpt(112) = "getenv _tgetenv"
  fpt(113) = "_makepath _tmakepath"
  fpt(114) = "_putenv _tputenv"
  fpt(115) = "_searchenv _tsearchenv"
  fpt(116) = "_splitpath _tsplitpath"
  fpt(117) = "fdopen _tfdopen"
  fpt(118) = "_fdopen _tfdopen"
  fpt(119) = "_fsopen _tfsopen"
  fpt(120) = "fopen _tfopen"
  fpt(121) = "freopen _tfreopen"
  fpt(122) = "perror _tperror"
  fpt(123) = "_popen _tpopen"
  fpt(124) = "_tempnam _ttempnam"
  fpt(125) = "tmpnam _ttmpnam"
  fpt(126) = "_chmod _tchmod"
  fpt(127) = "_creat _tcreat"
  fpt(128) = "_findfirst _tfindfirst"
  fpt(129) = "_findfirsti64 _tfindfirsti64"
  fpt(130) = "_findnext _tfindnext"
  fpt(131) = "_findnexti64 _tfindnexti64"
  fpt(132) = "_mktemp _tmktemp"
  fpt(133) = "open _topen"
  fpt(134) = "access _taccess"
  fpt(135) = "_open _topen"
  fpt(136) = "_access _taccess"
  fpt(137) = "remove _tremove"
  fpt(138) = "rename _trename"
  fpt(139) = "_sopen _tsopen"
  fpt(140) = "_unlink _tunlink"
  fpt(141) = "_finddata_t _tfinddata_t"
  fpt(142) = "_finddatai64_t _tfinddatai64_t"
  fpt(143) = "isascii _istascii"
  fpt(144) = "iscntrl _istcntrl"
  fpt(145) = "isxdigit _istxdigit"
  fpt(146) = "_stat _tstat"
  fpt(147) = "_stati64 _tstati64"
  fpt(148) = "setlocale _tsetlocale"
  fpt(149) = "isalnum _istalnum"
  fpt(150) = "isalpha _istalpha"
  fpt(151) = "isdigit _istdigit"
  fpt(152) = "isgraph _istgraph"
  fpt(153) = "islower _istlower"
  fpt(154) = "isprint _istprint"
  fpt(155) = "ispunct _istpunct"
  fpt(156) = "isspace _istspace"
  fpt(157) = "isupper _istupper"
  fpt(158) = "toupper _totupper"
  fpt(159) = "tolower _totlower"
  fpt(160) = "itoa _itot"
  fpt(161) = "ltoa _ltot"
  fpt(162) = "strnewdup _tcsnewdup"
  fpt(163) = "unlink _tunlink"
  fpt(164) = "atof _tcstod"
  fpt(165) = "strupr _tcsupr"
  fpt(166) = "strlwr _tcslwr"
  fpt(167) = "stricmp _tcsicmp"
  fpt(168) = "strcmpi _tcsicmp"
  fpt(169) = "_strcmpi _tcsicmp"

  '  char  CHAR
  if Replace("char", "TCHAR", "", dsMatchCase + dsMatchWord) = vbCancel then
    if NextBox() = vbCancel then
      exit sub
    end if
  end if

  '    -
  for each i in fpt
'    ActiveDocument.Selection.MoveTo startLine, startCol '   
    pair = Split( i, " " )
    if Replace(pair(0), pair(1), "", dsMatchCase + dsMatchWord) = vbCancel then
      if NextBox() = vbCancel then
        exit sub
      end if
    end if
  next

'  ActiveDocument.Selection.MoveTo startLine, startCol '   

  '     _T
  if Replace( """[^""]*""", "_T(", ")", dsMatchRegExp) = vbCancel then '"\:q"
    if NextBox() = vbCancel then
      exit sub
    end if
  end if

'  ActiveDocument.Selection.MoveTo startLine, startCol '   

  '     _T
  if Replace( "'[^']*'", "_T(", ")", dsMatchRegExp) = vbCancel then '"\:q"
    if NextBox() = vbCancel then
      exit sub
    end if
  end if

'  ActiveDocument.Selection.MoveTo startLine, startCol '   

  '  unsigned char  byte
  if Replace("unsigned\:b+char", "byte", "", dsMatchCase + dsMatchRegExp) = vbCancel then
    exit sub
  end if

  'todo sizeof

'  ActiveDocument.Selection.MoveTo startLine, startCol '    
end sub
