$PELLES 'Change2Unicode(Command$ = FileIn as string) CONST CMAX = 300 CONST Versions$ = "1.05" DIM K Global str_cLine as String * 2048 Global MixedFunctions[5,2] As String * 70 'Global hFileIn 'Global hFileOut AS Dim arrUnicode[CMAX+1][2] as String * 200 Dim changeFrom as String * 200 Dim changeTo as String * 200 Dim lngLines as Long Dim iCnt as Integer Dim iStart as Integer Dim iLoop as Integer Dim Raw FileIn as String Dim Raw FileOut as String Dim Raw str_Path as String Dim RetVal% Dim Raw Start! Dim Raw Fini! Dim Lapse! Dim Already as Integer Dim WaitforPrompt as integer Dim RAW LeaveANSI as integer Dim Raw NOTKeyword[3] AS String Dim UseUnicode as Boolean Dim LeaveThis As Integer Global command2[4] as string 'Global command3 as string Global Use_tchar as BOOLEAN Global Use_lchar as BOOLEAN Use_tchar = 1 'TRUE Use_lchar = 0 LeaveThis = 0 WaitforPrompt = 0 FileIn = COMMAND$(1) command2[0] = COMMAND$(2) command2[1] = COMMAND$(3) command2[2] = COMMAND$(4) FileOut = "" ' If File is nothing then get a file to translate FileIn = Trim$(FileIn) If Len(FileIn) = 0 then WaitforPrompt = 1 str_cLine = "C++ Files(*.cpp) C Files(*.c)|*.CPP;*.c" FileIn = GETFILENAME$("OPEN", str_cLine,0,NULL,OFN_LONGNAMES | OFN_EXPLORER,APPEXEPATH$) ' defaults to OPEN Dialog Dim cmd$ Dim n as integer Dim g as integer Dim h as integer Dim x$[3] cmd$ = InputBox$ ("Command line","Other Command line switches:","") If cmd$ <> "" then n = split(x$,trim$(cmd$),chr$(32)) For g = 0 to n-1 If x$[g] <> "" then command2[h] = x$[g] Print str$(h) & " " & str$(g) & " " & command2[h] h++ End If Next g End If IF NOT EXIST(FileIn) THEN End End If ELSE ? "File to Convert - " & FileIn & " Version # " & Versions$ End if FileOut = FileIn str_cLine = MID$(FileIn,1,INSTRREV(FileIn,".")) CONCAT(str_cLine,"OLD") IF EXIST(str_cLine) THEN KILL str_cLine End If RENAME FileIn,str_cLine FileIn = str_cLine str_Path = MID$(FileIn,1,INSTRREV(FileIn,"\")) Start! = TIMER UseUnicode = Chk4Unicode(FileIn) ' Fini! = TIMER ' Lapse! = Fini! - Start! ' ? "Elapsed Time1:",Lapse!," Secs" 'Start! = TIMER 'UseUnicode = Chk4UnicodeOld(FileIn) ' Fini! = TIMER ' Lapse! = Fini! - Start! ' ? "Elapsed Time2:",Lapse!," Secs" 'Start! = TIMER ' Garvan Added this code for reading data file DIM DataFile$ DIM Buffer$[12] DIM LineIn$ DIM icoment%, Ndx%, Index% DIM NoPrint AS Long NoPrint = 0 'I Added this so multiple Keyword files If trim$(command2[0]) = "" Then DataFile$ = APPEXEPATH$ & "Basic2CKeywordsXlat.txt" ElseIf instr(trim$(command2[0]),"-g") <> 0 then DataFile$ = APPEXEPATH$ & "Basic2UserKeywordsXlat.txt" ElseIf instr(trim$(command2[0]),"-w") <> 0 then DataFile$ = APPEXEPATH$ & "WCS_Basic2CKeywordsXlat.txt" ElseIf instr(trim$(command2[0]),"-u") <> 0 then DataFile$ = APPEXEPATH$ & "Basic2CKeywordsXlat.txt" ElseIf instr(trim$(command2[0]),"-t") <> 0 then DataFile$ = APPEXEPATH$ & "Basic2CKeywordsXlat.txt" Use_tchar = 0 'False ElseIf instr(trim$(command2[0]),"-l") <> 0 then DataFile$ = APPEXEPATH$ & "Basic2CKeywordsXlat.txt" Use_tchar = 1 'False Use_lchar = 1 'False Else DataFile$ = command2[0] End If If instr(trim$(command2[1]), "-t") <> 0 Then If Use_lchar = 0 then'False Use_tchar = 0' False End If ElseIf instr(trim$(command2[1]),"-l") <> 0 then Use_lchar = 1 Use_tchar = 1' False End If Print "Using " & DataFile$ & " " & Use_tchar IF NOT EXIST(DataFile$) THEN MSGBOX "DataFile " & DataFile$ & " not found",command2[0], 0 End END IF 'FREEFILE is optional, BCX takes care of this detail 'Thanks, but - Old habits are hard to break, code isn't Call ReadDataFile() 'End Garvans code '? "Closed file" 'CLS 'Now open both Files RetVal% = EXIST(FileIn) If RetVal% = 0 then ? "ERROR - File Not Found" Else hFileIn = FREEFILE OPEN FileIn FOR INPUT AS hFileIn hFileOut = FREEFILE str_cLine = "" OPEN FileOut FOR OUTPUT AS hFileOut hTest = FREEFILE str_cLine = "" CONCAT(str_cLine,str_Path) CONCAT(str_cLine,"Report.txt") OPEN str_cLine FOR OUTPUT AS hTest str_cLine = "" 'Input and output files are already open now do the conversion LeaveANSI = 0 Call LoadNOTKeywords If UseUnicode <> 0 then ' FPRINT hFileOut, "#define _UNICODE" End If If Use_tchar = 1 then ' FPRINT hFileOut, "#include " End If Do until EOF(hFileIn) NoPrint = 0 LINE INPUT hFileIn, str_cLine lngLines = lngLines + 1 If (Instr(UCASE$(str_cLine),"$EMIT_ANSI") <> 0) then FPRINT hTest,str$(lngLines) & " $EMIT_ANSI " & str_cLine If LeaveANSI = 0 then LeaveANSI = 1 Else LeaveANSI = 0 End If End If If (Instr(UCASE$(str_cLine),"<--UNICODE AWARE") <> 0) then LeaveThis = 1 Replace "<--UNICODE AWARE" with "//<--UNICODE AWARE" In str_cLine ElseIf (Instr(UCASE$(str_cLine),">--UNICODE AWARE") <> 0) then LeaveThis = 0 Replace ">--UNICODE AWARE" with "//>--UNICODE AWARE" In str_cLine End If If LeaveThis = 1 then FPRINT hFileOut, str_cLine ITERATE End If 'Added 10/16/04 If instr(mid$(str_cLine,1,3),"//") then FPRINT hFileOut, str_cLine ITERATE End If If Instr(LCASE$(str_cLine),"#pragma comment") <> 0 Then 'str_cLine = RedefinePragma(str_cLine) removed 10/18/04 FPRINT hFileOut, str_cLine ITERATE End If If chkMixedFunctions(str_cLine) Then 'Start Check for functions that need to be output to file without XLation 10/18/04 - 12/8/04 RetVal% = chkMixedFunctions(str_cLine) If Instr(Remove$(str_cLine,chr$(32)), MixedFunctions [RetVal%-1,1]) <> 0 then If Instr(str_cLine,";") = 0 then 'its a function, so just copy to output file till end of function Do Until Left$(str_cLine,1) = "}" ' If RetVal% <> 4 then FPRINT hFileOut, str_cLine ' End If LINE INPUT hFileIn, str_cLine lngLines = lngLines + 1 If EOF(hFileIn) then Exit Do Loop $COMMENT If RetVal% = 4 then str_cLine = PrintNewBCX_COM_WS2AS() End If $COMMENT FPRINT hFileOut, str_cLine Else 'its a header FPRINT hFileOut, str_cLine End If ITERATE End If End If 'End Check for functions If LeaveANSI = 0 then If NOT chkNOTKeywords(str_cLine) Then For iLoop = 0 to iCnt iStart = instr(str_cLine,arrUnicode[iLoop][0]) 'Check that arrUnicode[iLoop][0] isn't part of a larger word 'check character before If chkAlpha(Mid$(str_cLine,iStart-1,1)) <> 0 THEN ITERATE 'check character after If chkAlpha(Mid$(str_cLine,iStart + len(arrUnicode[iLoop][0]),1)) <> 0 THEN ITERATE 'It's not part of another word - so... If iStart <> 0 Then Already = Instr(str_cLine,arrUnicode[iLoop][1],iStart-3) If Already < 1 Or Already > 5 then changeFrom = arrUnicode[iLoop][0] changeTo = arrUnicode[iLoop][1] If instr(UCASE$(changeTo),"$DELELINE") then 'delete the whole line & add to Report only FPRINT hTest,str$(lngLines) & " LINE DELETED " & str_cLine str_cLine = "//" & str_cLine NoPrint = 1 Elseif instr(UCASE$(changeTo),"$DELE") or instr(UCASE$(changeTo),"") then 'Just add white space in place of code Replace changeFrom with " " In str_cLine ConCat (str_cLine,"// Deleted ") ConCat (str_cLine,changeFrom) FPRINT hTest,str$(lngLines) & " " & str_cLine Else Replace changeFrom with changeTo In str_cLine ConCat (str_cLine,"// Changed to Unicode ") FPRINT hTest,str$(lngLines) & " " & str_cLine End If End If 'Already End If 'iStart Next iLoop Else 'There's a NOTKeyword so it's special, if it's in a compound statement then 'translate to the comma before the keyword FPRINT hTest,str$(lngLines) & " NotKeyWord" Dim strSpecial$, strRest$ Dim i as integer, j as integer , k as integer For i = 0 to 2 k = instr(UCASE$(str_cLine),Trim$(NOTKeyword[i])) If k <> 0 then j = k -1 End If Next i FPRINT hTest,str$(lngLines) & " " & str$(j) & " " & str_cLine If j <> 0 then strSpecial$ = Left$(str_cLine,j) strRest$ = Mid$(str_cLine,j) For iLoop = 0 to iCnt iStart = instr(strSpecial$,arrUnicode[iLoop][0]) If chkAlpha(Mid$(strSpecial$,iStart-1,1)) <> 0 THEN ITERATE If chkAlpha(Mid$(strSpecial$,iStart + len(arrUnicode[iLoop][0]),1)) <> 0 THEN ITERATE If iStart <> 0 Then Already = Instr(strSpecial$,arrUnicode[iLoop][1],iStart-3) If Already < 1 Or Already > 5 then changeFrom = arrUnicode[iLoop][0] changeTo = arrUnicode[iLoop][1] Replace changeFrom with changeTo In strSpecial$ If instr(UCASE$(strRest$),"WIDETOANSI") then Replace "WideToAnsi" with "WideToAnsiW" In strRest$ End If str_cLine = strSpecial$ ConCat (str_cLine,strRest$) ConCat (str_cLine,"// Changed to Unicode ") FPRINT hTest,str$(lngLines) & " " & str_cLine End If End If Next iLoop End If 'j<>0 End If 'chkNOTKeywords 'Emit str_cLine to FileOut Else NoPrint = 0 End If 'LeaveAnsi If NoPrint = 0 then If instr(str_cLine$, "pragma") = 0 then AddLtoStrLit(str_cLine$) ' 'Added by Garvan End If FPRINT hFileOut, str_cLine NoPrint = 0 Else NoPrint = 0 End If Loop Fini! = TIMER Lapse! = Fini! - Start! str_cLine = "" FPRINT hTest, str_cLine str_cLine = " Operation took " & Str$(Lapse!) & " Secs" FPRINT hTest, str_cLine Close hFileIn Close hFileOut Close hTest ? "Elapsed Time:",Lapse!," Secs" ? "Time this compile : " , TIME$ End If If WaitforPrompt = 1 then LOCATE 20,28,0 LOCATE 23,28,0 INPUT "Press Enter To End" , K End If COLOR 7,0 CLS '******************************************************************** 'Mostly Added by Garvan, changed by IDC to add _T("") and remove L("") SUB AddLtoStrLit(a$) If Use_tchar = 0 then 'False Exit Sub End If DIM i,j, onof ,n , m, already DIM b$, t$ 'Dim c as integer b$ = a$ t$ = TRIM$(a$) ' IF t[0] = 35 THEN EXIT SUB '35 = # (compiler directive) remmed out 10/16/04 'Test for and remove 'L' in front of start of Text block 'as we're going to change it to _T anyway. If Use_lchar = 0 then m=1 WHILE m <> 0 n = instr(a$,chr$(34),m) If n <> 0 then If a[n-2] = 40 then If a[n-3] = 76 then b$ = a$ a$ = Left$(b$,n - 3) & Mid$(b$,n-1) End If ElseIf a[n-2] = 76 then b$ = a$ a$ = Left$(b$,n - 2) & Mid$(b$,n) End If n = instr(a$,chr$(34),m) 'find the start of the original quotation n = instr(a$,chr$(34),n + 1) 'find the end of the quotation End If m = instr(a$,chr$(34),n + 1) 'find the next quotation WEND End If b$ = a$ WHILE a[i] <> 0 IF a[i] = 34 THEN '34 = DQ$ If (a[i-1] = 40) BAND (a[i-2] = 84) BAND (a[i-3] = 95) then onof = NOT onof already = 1 else If a[i-1] <> 92 then 'check for escape character onof = NOT onof IF onof THEN If Use_lchar = 0 then 'Need to change to _T("xxx") b[j++] = 32 '32 = space b[j++] = 95 '95 = underscore add real underscore here to test b[j++] = 84 '84 = T b[j++] = 40 '40 = ( ' Else ' 'Need to change to L("xxx") ' b[j++] = 76 End If Else 'IF onof THEN if already = 0 then i++ b[j++] = 34 If Use_lchar = 0 then b[j++] = 41 '41 = ) End If End If END IF 'IF onof THEN End If ' a[i-1] = 91 End If END IF b[j] = a[i] i++ : j++ WEND a$ = b$ END SUB '******************************************************************** SUB LoadNOTKeywords() 'Global NOTKeyword[3] AS String * 32 NOTKeyword[0] = "//EMIT_ANSI" NOTKeyword[1] = "bcx_temp_ans_string_pointer" 'NOTKeyword[1] = "ANSITOWIDE" 'NOTKeyword[2] = "WIDETOANSI" MixedFunctions[0,0] = "AnsiToWide": MixedFunctions[0,1] = "LPOLESTRAnsiToWide" MixedFunctions[1,0] = "WideToAnsi": MixedFunctions[1,1] = "char*WideToAnsi" MixedFunctions[2,0] = "BCX_COM_AS2WS": MixedFunctions[2,1] = "HRESULTBCX_COM_AS2WS" MixedFunctions[3,0] = "BCX_COM_WS2AS": MixedFunctions[3,1] = "HRESULTBCX_COM_WS2AS" MixedFunctions[4,0] = "bcx_temp_ans_string_pointer": MixedFunctions[4,1] = "char*bcx_temp_ans_string_pointer" 'char* bcx_temp_ans_string_pointer END SUB '******************************************************************** Function chkNOTKeywords(a$) AS Integer 'These are for lines where Keywords are to be left alone Dim Raw i i = 0 ' For i = 0 to 1 If Instr(UCASE$(a$),Trim$(NOTKeyword[i])) <> 0 then 'was i Function = 1 'TRUE 'Exit Function End If ' Next i Function = 0'FALSE END Function '******************************************************************** Function chkMixedFunctions(a$) AS Integer 'These are for lines where Functions are to be left alone Dim Raw i i = 0 For i = 1 to 4 If Instr(a$, MixedFunctions[i-1,0]) <> 0 then Function = i End If Next i Function = 0'FALSE END Function '******************************************************************** Function chkAlpha(a$) Dim i as Integer Dim j as Integer j = ASC(a$) If a$ = "_" then 'j = 95 then i = j ' i = 0 Else $CCODE i = isalnum(j); $CCODE End If Function = i END Function '******************************************************************** $COMMENT 'Not needed anymore as now BCX outputs properly Function RedefinePragma(a$) As string Dim c$ , d$ 'Change From #pragma lib to #pragma comment(lib,"uuid.lib") d$ = Mid$(a$, Instr(a$,chr$(60))+1,Instr(a$,chr$(62))-Instr(a$,chr$(60)) - 1) c$ = "#pragma comment" & Chr$(40) & "lib" & chr$(44) & chr$(34) & d$ & chr$(34) & chr$(41) Function = c$ End Function $COMMENT '******************************************************************** 'Function ChkForEscape(a$) Sub ReadDataFile() OPEN DataFile$ FOR INPUT AS FP1 WHILE NOT EOF(FP1) LINE INPUT FP1,LineIn$ LineIn$ = TRIM$(LineIn$) IF NOT LEN(LineIn$) THEN ITERATE 'Blank line IF LEFT$(LineIn$, 2) = "//" THEN ITERATE icoment% = INSTR(LineIn$,"//") If Instr(LineIn$,"*UNICODE ONLY") <> 0 then 'Added 10/16/04 If UseUnicode = 0 Then ? "Unicode is False - Exit Load" EXIT LOOP Else ? "Use Unicode option is True - Continue Unicode only Functions" End If End If IF icoment% THEN 'Strip comments LineIn$ = TRIM$(LEFT$(LineIn$, icoment%-1)) END IF Ndx% = SPLIT(Buffer$, LineIn$, ",") IF Ndx% < 2 THEN ITERATE 'Ignore dud entries IF Index >= CMAX THEN Msgbox "The file Basic2CKeywordsXlat.txt has exceeded the maximum count of " & str$(CMAX) EXIT LOOP 'ITERATE 'Don't allow over dimensioning. Needs Error message End If arrUnicode[Index%][0] = TRIM$(Buffer$[0]) arrUnicode[Index%][1] = TRIM$(Buffer$[1]) Index%++ WEND iCnt = Index% -1 CLOSE FP1 End Sub '******************************************************************** Function PrintNewBCX_COM_WS2AS() as string Dim retStr$ * 2048 retStr$ = "HRESULT BCX_COM_WS2AS(LPCWSTR wide_string, UINT code_page)" & crlf$ retStr$ = retStr$ & "{" & crlf$ retStr$ = retStr$ & " if(wide_string==NULL) return (HRESULT) NO_ERROR;" & crlf$ retStr$ = retStr$ & " ULONG temp_bytes_copied_len=0;" & crlf$ retStr$ = retStr$ & " ULONG temp_ansi_len = (ULONG)WideCharToMultiByte(code_page,0,wide_string,-1,(char*)bcx_temp_ans_string_pointer,0,NULL,NULL)+256;" & crlf$ '<-char* cast added retStr$ = retStr$ & " if(temp_ansi_len==0) return (HRESULT) NO_ERROR;" & crlf$ retStr$ = retStr$ & " if (bcx_temp_ans_string_buffer_size < temp_ansi_len) {" & crlf$ retStr$ = retStr$ & " if (bcx_temp_ans_string_pointer) free(bcx_temp_ans_string_pointer);" & crlf$ retStr$ = retStr$ & "#ifndef UNICODE" & crlf$ '<- If Unicode defined added retStr$ = retStr$ & " bcx_temp_ans_string_pointer = (char*)calloc(temp_ansi_len,sizeof(char));" & crlf$ retStr$ = retStr$ & "#else" & crlf$ retStr$ = retStr$ & " bcx_temp_ans_string_pointer = (USHORT*)calloc(temp_ansi_len,sizeof(char));" & crlf$ '<- In Unicode the string pointers are TChar retStr$ = retStr$ & "#endif" & crlf$ retStr$ = retStr$ & " if (NULL == bcx_temp_ans_string_pointer){" & crlf$ retStr$ = retStr$ & " bcx_temp_ans_string_buffer_size = 0;" & crlf$ retStr$ = retStr$ & " return E_OUTOFMEMORY;" & crlf$ retStr$ = retStr$ & " } // if" & crlf$ retStr$ = retStr$ & " bcx_temp_ans_string_buffer_size = temp_ansi_len;" & crlf$ retStr$ = retStr$ & " }" & crlf$ retStr$ = retStr$ & " if((temp_bytes_copied_len = WideCharToMultiByte(code_page,0,wide_string,-1,(char*)bcx_temp_ans_string_pointer,temp_ansi_len,NULL,NULL))==0)" & crlf$ '<-char* cast added retStr$ = retStr$ & " {" & crlf$ retStr$ = retStr$ & " return HRESULT_FROM_WIN32(GetLastError());" & crlf$ retStr$ = retStr$ & " } // if" & crlf$ retStr$ = retStr$ & " bcx_temp_ans_string_pointer[temp_bytes_copied_len] = '\\0';" & crlf$ retStr$ = retStr$ & " return (HRESULT)NO_ERROR;" & crlf$ Function = retStr$ End Function '******************************************************************** Function Chk4UnicodeOld(FileIn As String) RetVal% = EXIST(FileIn) If RetVal% = 0 then ? "ERROR - File Not Found" Function = False Else hFileIn = FREEFILE OPEN FileIn FOR INPUT AS hFileIn Do until EOF(hFileIn) LINE INPUT hFileIn, str_cLine lngLines = lngLines + 1 If (Instr(UCASE$(str_cLine),"_UNICODE") <> 0) then Close hFileIn Function = True End If Loop Close hFileIn Function = False End If End Function '******************************************************************** Function Chk4Unicode(FileIn As String) RetVal% = EXIST(FileIn) If RetVal% = 0 then ? "ERROR - File Not Found" Function = False Else DIM RetStr$ * LOF(FileIn) RetStr$ = LOADFILE$(FileIn) If (Instr(UCASE$(RetStr$),"_UNICODE") <> 0) then Function = True End If Function = False End If End Function