computerwhiz
Programmer
Why is it that I can load whatever size file I want into the edit dialog when I am on "Windows 2000 Professional" but as soon as I run this program on windows 98 I can load the file text into memory but I cannot set the edit dialog text if the amount of text is over 64K? I am truly confused. am I going about this the wrong way?
Code:
#include <windows.h>
#include <commctrl.h>
#include "Main.h"
static char g_szClassName[] = "MyWindowClass";
static HINSTANCE g_hInst = NULL;
static HWND g_hStatusBar;
static BOOL ReadyToParse = FALSE;
static BOOL Neg=FALSE;
static BOOL EnableFlag=FALSE;
static LPSTR pszText;
static LPSTR Quote_A="\"";
static LPSTR Quote_B="\",\"";
static LPSTR Quote_C="\",";
static LPSTR Quote_D=",";
static LPSTR Return="\r\n";
static LPSTR Header="\"Number\",\"Date\",\"Remark\",\"Gross\",\"Deductions\",\"Amount Paid\"";
static int txtpos=0, count=0;
#define ID_STATUSBAR 4997
#define IDC_MAIN_TEXT 1001
void Insert(int Pos, LPSTR String)
{
if (Pos<0 || Pos>strlen(pszText) || String == 0X00)
{
return;
}
LPSTR NewText = (LPSTR)GlobalAlloc(GPTR, strlen(String)+strlen(pszText) + 2);
if (Pos > 0)
{
memcpy(NewText, pszText, Pos);
}
memcpy(NewText+Pos, String, strlen(String));
if (strlen(pszText)-Pos > 0)
{
memcpy(NewText+Pos+strlen(String), pszText+Pos, strlen(pszText)-Pos);
}
GlobalFree(pszText);
pszText = (LPSTR)GlobalAlloc(GPTR, strlen(NewText) + 2);
strcpy(pszText, NewText);
pszText[strlen(NewText)+1] = 0x00;
GlobalFree(NewText);
}
void Delete(int Pos, int Count)
{
int Len = strlen(pszText);
LPSTR NewText = (LPSTR)GlobalAlloc(GPTR, Len+2);
memmove(pszText+Pos, pszText+Pos+Count, Len-Pos-Count);
pszText[Len-Count+1] = 0x00;
strcpy(NewText, pszText);
GlobalFree(pszText);
pszText = (LPSTR)GlobalAlloc(GPTR, strlen(NewText) + 2);
strcpy(pszText, NewText);
pszText[strlen(NewText)+1] = 0x00;
GlobalFree(NewText);
}
void ProcessHeader(int ChkType)
{
if(ChkType == 1)
{
Delete(0, 191), Insert(0, Quote_A), Insert(10, Quote_A);
Delete(11, 74), Insert(11, Return), Insert(13, Quote_A);
Insert(22, Quote_A), Insert(25, Quote_A), Insert(84, Quote_A);
Insert(85, Return), Delete(87, 205), Insert(87, Header);
}
else if(ChkType == 2)
{
Delete(0, 159), Insert(0, Quote_A), Insert(36, Quote_A);
Insert(37, Return), Delete(39, 13), Insert(39, Quote_A);
Insert(49, Quote_A), Delete(52,79), Insert(52, Quote_A);
Insert(61, Quote_A), Delete(62, 100), Insert(64, Header);
}
}
void ProcessBody(int BdyType, int NumbPgs, LPSTR Pages)
{
int adj=0, adj2=0, adj3=0, Loop=0, NextBit=0, brk=0;
LPSTR TempStr = (LPSTR)GlobalAlloc(GPTR, 12);
LPSTR pszStatusBarTxt = (LPSTR)GlobalAlloc(GPTR, 70);
LPSTR pszStatusBarTxt2 = (LPSTR)GlobalAlloc(GPTR, 30);
strcpy(pszStatusBarTxt2, "Pages to process: ");
strcpy(pszStatusBarTxt2+18, Pages);
SendMessage(g_hStatusBar, SB_SETTEXT, 1, (LPARAM)pszStatusBarTxt2);
if(BdyType == 1)
{
txtpos=148;
for(Loop=1; Loop <= NumbPgs; Loop++)
{
count = 1;
memset(pszStatusBarTxt, 0x00, sizeof(pszStatusBarTxt));
strcpy(pszStatusBarTxt, "parsing.");
SendMessage(g_hStatusBar, SB_SETTEXT, 0, (LPARAM)pszStatusBarTxt);
while(count <= 50)
{
adj = 0, adj2 = 0;
strcat(pszStatusBarTxt, ".");
SendMessage(g_hStatusBar, SB_SETTEXT, 0, (LPARAM)pszStatusBarTxt);
if(brk == 1) brk++;
strncpy(TempStr, pszText+txtpos+36, 11);
TempStr[11] = 0x00;
Insert(txtpos, Quote_A);
Delete(txtpos+11, 1), Insert(txtpos+11, Quote_B);//number
Delete(txtpos+19,1), Insert(txtpos+19, Quote_B);//date
Insert(txtpos+48, Quote_C);//info
Delete(txtpos+52, 1);//remove comma in gross
Delete(txtpos+55, 1);//remove comma in gross
Insert(txtpos+62, Quote_D);//gross
Delete(txtpos+64, 1);//remove comma in disc
Delete(txtpos+67, 1);//remove comma in disc
Insert(txtpos+74, Quote_D);//discount
Delete(txtpos+77, 1);//remove comma in amount
Delete(txtpos+80, 1);//remove comma in amount
if(pszText[txtpos+86] == 0x02D) adj2++;
if(pszText[txtpos+61] == 0x02D)
{
Delete(txtpos+61, 1), Insert(txtpos+50, "-");
}
else
{
Delete(txtpos+61, 1), adj--;
}
if(pszText[txtpos+70+adj] != 0x02E & pszText[txtpos+70+adj] != 0x02D)
{
Delete(txtpos+72+adj, 1);//add a zero to blank disc
Insert(txtpos+72+adj, "0");
}
if(pszText[txtpos+73+adj] == 0x02D)
{
Delete(txtpos+73+adj, 1), Insert(txtpos+63+adj, "-");
}
else
{
Delete(txtpos+73+adj, 1), adj--;
}
if(pszText[txtpos+86+adj] == 0x02D)
{
Delete(txtpos+86+adj, 1), Insert(txtpos+75+adj, "-");
}
NextBit = 0;
while(pszText[txtpos+48+NextBit] != 0x00D)
{
while(isspace(pszText[txtpos+48+NextBit]))
{
Delete(txtpos+48+NextBit, 1);
adj--;
}
NextBit++;
}
if(strstr(TempStr, " ----"))
{
adj3 = txtpos;
brk++;
}
count++;
txtpos += 88 + adj + adj2;
if(brk == 2) break;
}
if(brk != 2 & Loop < NumbPgs)
{
Delete(txtpos, 450);
}
else if(brk == 2)
{
Delete(adj3, 86);
txtpos -= 87;
}
}
Delete(txtpos, strlen(pszText)-txtpos);//remove remaining of file
}
if(BdyType == 2)
{
txtpos=125, count=1;
strcpy(pszStatusBarTxt, "parsing.");
while(count <=27)
{
adj = 0, adj2 = 0;
SendMessage(g_hStatusBar, SB_SETTEXT, 0, (LPARAM)pszStatusBarTxt);
strcat(pszStatusBarTxt, ".");
if(brk == 1) brk++;
strncpy(TempStr, pszText+txtpos+58, 11);
TempStr[11] = 0x00;
Delete(txtpos, 9), Insert(txtpos, Quote_A);
Delete(txtpos+11, 1), Insert(txtpos+11, Quote_B);//number
Delete(txtpos+20,1), Insert(txtpos+20, Quote_B);//date
Insert(txtpos+54, Quote_C);//info
Delete(txtpos+60, 1);//remove comma in gross
Insert(txtpos+67, Quote_D);
Delete(txtpos+72, 1);//remove comma in disc
Insert(txtpos+79, Quote_D);
Delete(txtpos+84, 1);//remove comma in amount
if(pszText[txtpos+90] == 0x02D) adj2++;
if(pszText[txtpos+66] == 0x02D)
{
Delete(txtpos+66, 1), Insert(txtpos+56, "-");
}
else
{
Delete(txtpos+66, 1), adj--;
}
if(pszText[txtpos+75+adj] != 0x02E & pszText[txtpos+75+adj] != 0x02D)
{
Delete(txtpos+77+adj, 1);//add a zero to blank disc
Insert(txtpos+77+adj, "0");
}
if(pszText[txtpos+78+adj] == 0x02D)
{
Delete(txtpos+78+adj, 1), Insert(txtpos+68+adj, "-");
}
else
{
Delete(txtpos+78+adj, 1), adj--;
}
if(pszText[txtpos+90+adj] == 0x02D)
{
Delete(txtpos+90+adj, 1), Insert(txtpos+80+adj, "-");
}
NextBit = 0;
while(pszText[txtpos+56+NextBit] != 0x00D)
{
while(isspace(pszText[txtpos+56+NextBit]))
{
Delete(txtpos+56+NextBit, 1);
adj--;
}
NextBit++;
}
if(strstr(TempStr, "-----------"))
{
adj3 = txtpos;
brk++;
}
count++;
txtpos += 92 + adj + adj2;
if(brk == 2) break;
}
if(brk == 2)
{
Delete(adj3, 90);
txtpos -= 91;
}
Delete(txtpos, strlen(pszText)-txtpos);//remove remaining of file
}
GlobalFree(pszStatusBarTxt);
GlobalFree(pszStatusBarTxt2);
GlobalFree(TempStr);
SendMessage(g_hStatusBar, SB_SETTEXT, 1, (LPARAM)"");
}
void ProcessFile(HWND hEdit)
{
if (ReadyToParse)
{
DWORD dwTextLngth;
char Pages[5];
int PagesInt = 0;
dwTextLngth = GetWindowTextLength(GetDlgItem(hEdit, IDC_MAIN_TEXT));
if(dwTextLngth > 400)
{
pszText = (LPSTR)GlobalAlloc(GPTR, dwTextLngth + 1);
if(pszText != NULL)
{
if(GetWindowText(GetDlgItem(hEdit, IDC_MAIN_TEXT), pszText, dwTextLngth + 1))
{
if(strstr(pszText, "Payment Attachment"))
{
SendMessage(g_hStatusBar, SB_SETTEXT, 0, (LPARAM)"Parsing.");
strncpy(Pages, pszText+356, 4);
Pages[4] = 0x00;
PagesInt = atoi(Pages);
ProcessHeader(1);
ProcessBody(1, PagesInt, Pages);
SetWindowText(GetDlgItem(hEdit, IDC_MAIN_TEXT), pszText);
SendMessage(g_hStatusBar, SB_SETTEXT, 0,
(LPARAM)"Parsing is finished, please save your work.");
}
else if(strstr(pszText, "Payee:"))
{
SendMessage(g_hStatusBar, SB_SETTEXT, 0, (LPARAM)"Parsing.");
ProcessHeader(2);
ProcessBody(2, 0, NULL);
SetWindowText(GetDlgItem(hEdit, IDC_MAIN_TEXT), pszText);
SendMessage(g_hStatusBar, SB_SETTEXT, 0,
(LPARAM)"Parsing is finished, please save your work.");
}
}
}
GlobalFree(pszText);
}
else
{
MessageBox(hEdit, "No text to parse.", "FAILURE",
MB_OK | MB_ICONEXCLAMATION);
}
ReadyToParse = FALSE;
HMENU hMenu;
hMenu = GetMenu(hEdit);
EnableFlag = FALSE;
EnableMenuItem(hMenu, 1, MF_BYPOSITION | (EnableFlag ? MF_ENABLED : MF_GRAYED));
DrawMenuBar(hEdit);
}
else
{
HMENU hMenu;
hMenu = GetMenu(hEdit);
EnableFlag = FALSE;
EnableMenuItem(hMenu, 1, MF_BYPOSITION | (EnableFlag ? MF_ENABLED : MF_GRAYED));
DrawMenuBar(hEdit);
}
}
void SetWndTitle(HWND hMain, LPSTR pszTitle)
{
char NewWindowTitle[50] = "Check Parser -- ";
LPSTR Temp = strrchr(pszTitle, 0x05C)+1;
memcpy(NewWindowTitle+16, Temp, strlen(Temp)+1);
SetWindowText(hMain, NewWindowTitle);
}
BOOL LoadFile(HWND hMain, HWND hEdit, LPSTR pszFileName)
{
HANDLE hFile;
BOOL bSuccess = FALSE;
hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, 0, 0);
if(hFile != INVALID_HANDLE_VALUE)
{
DWORD dwFileSize;
dwFileSize = GetFileSize(hFile, NULL);
if(dwFileSize != 0xFFFFFFFF)
{
LPSTR pszFileText;
pszFileText = (LPSTR)GlobalAlloc(GPTR, dwFileSize + 1);
if(pszFileText != NULL)
{
DWORD dwRead;
if(ReadFile(hFile, pszFileText, dwFileSize, &dwRead, NULL))
{
pszFileText[dwFileSize] = 0;
if(SetWindowText(hEdit, pszFileText))
{
DWORD dwTextLength;
dwTextLength = GetWindowTextLength(hEdit);
if(dwTextLength > 1)
{
bSuccess = TRUE;
ReadyToParse = TRUE;
SetWndTitle(hMain, pszFileName);
SendMessage(g_hStatusBar, SB_SETTEXT, 0, (LPARAM)"Ready to parse file.");
}
}
}
GlobalFree(pszFileText);
}
}
CloseHandle(hFile);
}
return bSuccess;
}
BOOL SaveFile(HWND hEdit, LPSTR pszFileName)
{
HANDLE hFile;
BOOL bSuccess = FALSE;
hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, 0,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if(hFile != INVALID_HANDLE_VALUE)
{
DWORD dwTextLength;
dwTextLength = GetWindowTextLength(hEdit);
if(dwTextLength > 0)
{
LPSTR pszText;
pszText = (LPSTR)GlobalAlloc(GPTR, dwTextLength + 1);
if(pszText != NULL)
{
if(GetWindowText(hEdit, pszText, dwTextLength + 1))
{
DWORD dwWritten;
if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL)) bSuccess = TRUE;
SendMessage(g_hStatusBar, SB_SETTEXT, 0,
(LPARAM)"File was saved successfully.");
}
GlobalFree(pszText);
}
}
CloseHandle(hFile);
}
return bSuccess;
}
BOOL DoFileOpenSave(HWND hwnd, BOOL bSave)
{
OPENFILENAME ofn;
char szFileName[MAX_PATH];
ZeroMemory(&ofn, sizeof(ofn));
szFileName[0] = 0;
ofn.lStructSize = sizeof(OPENFILENAME);//sizeof(ofn) msdn says win98/NT4 should use this.
ofn.hwndOwner = hwnd;
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrDefExt = "htm";
if(bSave)
{
ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY |
OFN_OVERWRITEPROMPT;
ofn.lpstrFilter = "CSV Files (*.csv)\0*.csv\0\0";
if(GetSaveFileName(&ofn))
{
if(!SaveFile(GetDlgItem(hwnd, IDC_MAIN_TEXT), szFileName))
{
MessageBox(hwnd, "Save file failed.", "Error",
MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
}
}
else
{
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrFilter = "Html Files (*.htm)\0*.htm\0All Files (*.*)\0*.*\0\0";
if(GetOpenFileName(&ofn))
{
if(!LoadFile(hwnd, GetDlgItem(hwnd, IDC_MAIN_TEXT), szFileName))
{
MessageBox(hwnd, "Load of file failed.", "Error",
MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
}
}
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch(Message)
{
case WM_CREATE:
{
int iStatusWidths[] = {250, -1};
CreateWindow("EDIT", "",
WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | ES_MULTILINE |
ES_WANTRETURN,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
hwnd, (HMENU)IDC_MAIN_TEXT, g_hInst, NULL);
g_hStatusBar = CreateWindowEx(0, STATUSCLASSNAME, NULL,
WS_CHILD | WS_VISIBLE | SBARS_SIZEGRIP, 0, 0, 0, 0,
hwnd, (HMENU)ID_STATUSBAR, g_hInst, NULL);
SendMessage(g_hStatusBar, SB_SETPARTS, 2, (LPARAM)iStatusWidths);
SendMessage(g_hStatusBar, SB_SETTEXT, 0, (LPARAM)"Ready to open a file.");
SendDlgItemMessage(hwnd, IDC_MAIN_TEXT, WM_SETFONT,
(WPARAM)GetStockObject(DEFAULT_GUI_FONT), MAKELPARAM(TRUE, 0));
break;
}
case WM_SIZE:
{
RECT rectClient, rectStatus;
UINT uToolHeight, uStatusHeight, uClientAlreaHeight;
SendMessage(g_hStatusBar, WM_SIZE, 0, 0);
GetClientRect(hwnd, &rectClient);
GetWindowRect(g_hStatusBar, &rectStatus);
uStatusHeight = rectStatus.bottom - rectStatus.top;
uClientAlreaHeight = rectClient.bottom;
if(wParam != SIZE_MINIMIZED)
MoveWindow(GetDlgItem(hwnd, IDC_MAIN_TEXT), 0, 0, rectClient.right,
uClientAlreaHeight-uStatusHeight, TRUE);
break;
}
case WM_SETFOCUS:
SetFocus(GetDlgItem(hwnd, IDC_MAIN_TEXT));
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case CM_FILE_OPEN:
if(DoFileOpenSave(hwnd, FALSE))
{
HMENU hMenu, hFileMenu;
hMenu = GetMenu(hwnd);
EnableFlag = TRUE;
EnableMenuItem(hMenu, 1, MF_BYPOSITION | (EnableFlag ? MF_ENABLED : MF_GRAYED));
hFileMenu = GetSubMenu(hMenu, 0);
EnableMenuItem(hFileMenu, CM_FILE_SAVEAS, MF_BYCOMMAND | (EnableFlag ? MF_ENABLED : MF_GRAYED));
DrawMenuBar(hwnd);
}
break;
case CM_FILE_SAVEAS:
if(DoFileOpenSave(hwnd, TRUE))
{
HMENU hMenu, hFileMenu;
hMenu = GetMenu(hwnd);
EnableFlag = FALSE;
EnableMenuItem(hMenu, 1, MF_BYPOSITION | (EnableFlag ? MF_ENABLED : MF_GRAYED));
hFileMenu = GetSubMenu(hMenu, 0);
EnableMenuItem(hFileMenu, CM_FILE_SAVEAS, MF_BYCOMMAND | (EnableFlag ? MF_ENABLED : MF_GRAYED));
DrawMenuBar(hwnd);
}
break;
case CM_FILE_EXIT:
PostMessage(hwnd, WM_CLOSE, 0, 0);
break;
case CM_PARSE_FILE:
ProcessFile(hwnd);
break;
case CM_ABOUT:
MessageBox (NULL,
"MBS TEXTBOOK EXCHANGE, INC.\n"
"Copyright 2003\n\n"
"Check parser was designed for the easy\n"
"conversion of MBS Accounts Payable check\n"
"remittances into .CSV files. Making it easier to\n"
"load the data into a spreadsheet.\n\nDesigned by Dave Dartt" ,
"About...", 0);
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX WndClass;
HWND hwnd;
MSG Msg;
g_hInst = hInstance;
WndClass.cbSize = sizeof(WNDCLASSEX);
WndClass.style = 0;
WndClass.lpfnWndProc = WndProc;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hInstance = g_hInst;
WndClass.hIcon = LoadImage(NULL, "chkparser.ico", IMAGE_ICON, 32, 32, LR_LOADFROMFILE);
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
WndClass.lpszMenuName = "MAINMENU";
WndClass.lpszClassName = g_szClassName;
WndClass.hIconSm = LoadImage(NULL, "chkparser.ico", IMAGE_ICON, 16, 16, LR_LOADFROMFILE);
if(!RegisterClassEx(&WndClass))
{
MessageBox(0, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
return 0;
}
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName, "Check Parser",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 550, 400,
NULL, NULL, g_hInst, NULL);
if(hwnd == NULL)
{
MessageBox(0, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&Msg, NULL, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}