📄 main.c
字号:
/* text compress
written by jared bruni
www.lostsidedead.com
I wrote this app cause I was bored and wanted to practice only using
C to help keep it seperated from C++ in my mind
*/
/* note the changing cursor */
#include <windows.h>
#include <stdio.h>
#include "resource.h"
/* variables */
HWND mainwindow;
HWND editbox;
/* function prototypes */
LRESULT APIENTRY WindowProccess(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
void InitApp();
BOOL openfile(char* file);
BOOL savefile(char* file);
int flen(const char *filename);
void readfile(const char *filename, char* data);
void strcomp(char* source);
void strdecomp(char* source);
int strdecomplen(char* source);
void SetSize();
/* win main, the applications entry point */
int APIENTRY WinMain(HINSTANCE hInst,HINSTANCE hPrev,LPSTR line,int CmdShow)
{
MSG msg;
InitApp();
while(GetMessage(&msg,0,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
/* *initilize this application*
define are window class
create are window
and show it
*/
void InitApp()
{
WNDCLASS wc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
wc.hCursor = LoadCursor(GetModuleHandle(NULL),MAKEINTRESOURCE(IDC_CURSOR1));
wc.hIcon = LoadIcon(GetModuleHandle(NULL),MAKEINTRESOURCE(IDI_ICON1));
wc.hInstance = GetModuleHandle(NULL);
wc.lpfnWndProc = (WNDPROC) WindowProccess;
wc.lpszClassName = "TextCompress";
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
wc.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wc);
mainwindow = CreateWindow("TextCompress"," Text Compress - ",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,640,480,0,0,GetModuleHandle(NULL),0);
ShowWindow(mainwindow,SW_SHOW);
UpdateWindow(mainwindow);
}
/* *main window callback function*,
a pointer to this function
is inside of the class structure
registered for this application's
main window
*/
LRESULT APIENTRY WindowProccess(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
switch(msg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_CREATE:
{
editbox = CreateWindowEx(WS_EX_STATICEDGE,"Edit",NULL, ES_READONLY | WS_VSCROLL | WS_HSCROLL | WS_CHILD | WS_VISIBLE | WS_BORDER | ES_MULTILINE,5,5,200,200,hwnd,0,GetModuleHandle(NULL),0);
}
break;
case WM_SETFOCUS:
{
SetCapture(editbox);
SetCursor(LoadCursor(GetModuleHandle(NULL),MAKEINTRESOURCE(IDC_CURSOR1)));
}
break;
case WM_SIZE:
{
int w = LOWORD(lParam);
int h = HIWORD(lParam);
SetWindowPos(editbox,0,0,0,w,h,0);
}
break;
case WM_COMMAND:
{
switch(wParam)
{
case ID_EXIT:
{
SendMessage(hwnd,WM_CLOSE,0,0);
}
break;
case ID_FILELOAD:// load this file
{
char filename[100];
if(openfile(filename))
{
/* open this file */
char* data = malloc ( flen(filename) + 1 );
readfile(filename,data);
SendMessage(editbox,WM_SETTEXT,strlen(data) + 1,(LPARAM)(LPCSTR)data);
free(data);
SetSize();
}
}
break;
case ID_SAVEFILE:
{
char filename[100];
if(savefile(filename))
{
FILE* sfile;
char* data = malloc ( SendMessage(editbox,WM_GETTEXTLENGTH,0,0) + 1 );
SendMessage(editbox,WM_GETTEXT,SendMessage(editbox,WM_GETTEXTLENGTH,0,0) + 1,(LPARAM)(LPCSTR) data);
sfile = fopen(filename,"w");
fprintf(sfile,data);
free(data);
fclose(sfile);
}
}
break;
case ID_ABOUT:
MessageBox(hwnd,"Simple Text Compress Algorithm GUI\n\nWritten in C by Jared Bruni for Practice\n\nwww.lostsidedead.com"," www.lostsidedead.com ",MB_OK | MB_ICONINFORMATION);
break;
case ID_COMPRESS:
{
char* cmpdat;
int len = SendMessage(editbox,WM_GETTEXTLENGTH,0,0) + 1;
cmpdat = malloc ( len );
SendMessage(editbox,WM_GETTEXT,len,(LPARAM)(LPCSTR)cmpdat);
strcomp(cmpdat);
SendMessage(editbox,WM_SETTEXT,strlen(cmpdat),(LPARAM)(LPCSTR)cmpdat);
free(cmpdat);
SetSize();
}
break;
case ID_DECOMPRESS:
{
char* pressed;
char* unpressed;
int len;
int ulen;
len = SendMessage(editbox,WM_GETTEXTLENGTH,0,0) + 1;
pressed = malloc (len);
SendMessage(editbox,WM_GETTEXT,len,(LPARAM)(LPCSTR)pressed);
ulen = strdecomplen(pressed) + 1;
unpressed = malloc( ulen );
strcpy(unpressed,pressed);
strdecomp(unpressed);
SendMessage(editbox,WM_SETTEXT,ulen,(LPARAM)(LPCSTR)unpressed);
SetSize();
free(pressed);
free(unpressed);
}
break;
}
}
break;
default: return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}
/* open file dialog box if true, the string you point to has the value */
BOOL openfile(char* cFile)
{
char szFile[200];
OPENFILENAME ofn;
strcpy(szFile, "*.*");
ZeroMemory(&ofn, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = mainwindow;
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = "ALL\0*.*\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
if (GetOpenFileName(&ofn)==TRUE)
{
strcpy(cFile, ofn.lpstrFile);
return TRUE;
}
else
{
return FALSE;
}
}
/* save file dialog box if true, cFile has the filename */
BOOL savefile(char* cFile)
{
char szFile[200];
OPENFILENAME ofn;
strcpy(szFile, "*.*");
ZeroMemory(&ofn, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = mainwindow;
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = "ALL\0*.*\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
if (GetSaveFileName(&ofn)==TRUE)
{
strcpy(cFile, ofn.lpstrFile);
return TRUE;
}
else
{
return FALSE;
}
}
/* important i/o functions */
/* return the length of this file */
int flen(const char *filename)
{
FILE* sfile;
int count = 0;
sfile = fopen(filename,"rb+");
while (1)
{
char c;
c = fgetc( sfile );
if(c == EOF)
{
break;
}
else
{
count++;
}
}
return count;
}
/* read in the file */
void readfile(const char *filename, char* data)
{
int pos;
FILE* ofile;
pos = 0;
ofile = fopen(filename,"rb+");
while (1)
{
char c;
c = fgetc( ofile );
if(c == EOF)
{
break;
}
data[pos] = c;
pos++;
}
data[pos] = 0;
fclose(ofile);
}
/* compression algorithms */
/* simple string compress algorithm */
void strcomp(char* source)
{
char* temp = malloc( strlen(source) + 1 );
int i, len = strlen(source),pos = 0;
for(i = 0; i < len; i++)
{
if( source[i+1] == source[i] && source[i+2] == source[i] && source[i+3] == source[i] )
{
int stop;
int ic = 0;
int amount = 0;
char amt[15];
int ilen;
int z;
for(ic = i; ic < len; ic++)
{
if(source[i] != source[ic])
{
stop = ic;
break;
}
else
{
amount++;
}
}
itoa(amount,amt,10);
ilen = strlen(amt);
temp[pos] = source[i];
pos++;
temp[pos] = (char) 200;
pos++;
for(z = 0; z < ilen; z++)
{
temp[pos] = amt[z];
pos++;
}
temp[pos] = (char) 200;
pos++;
i = stop-1;
}
else
{
temp[pos] = source[i];
pos++;
}
}
temp[pos] = 0;
strcpy(source,temp);
free(temp);
}
/* simple string decompression algorithm */
void strdecomp(char* source)
{
char* temp = malloc(strlen(source) + strlen(source) + strlen(source) + 1);
int i = 0,len = strlen(source),pos = 0;
for(i = 0; i < len; i++)
{
if(source[i] == (char)200)
{
int stop = 0;
int ic = 0;
int startpos = i + 1;
char xnum[15];
int xpos = 0;
int ia = 0;
int amt = 0;
for(ic = startpos; ic < len; ic++)
{
if(source[ic] == (char)200)
{
stop = ic;
break;
}
}
for(ia = i+1; ia < stop; ia++)
{
xnum[xpos] = source[ia];
xpos++;
}
xnum[xpos] = 0;
amt = atoi(xnum);
for(ia = 0; ia < amt-1; ia++)
{
temp[pos] = source[i-1];
pos++;
}
i = stop;
continue;
}
else
{
temp[pos] = source[i];
pos++;
}
}
temp[pos] = 0;
strcpy(source,temp);
free(temp);
}
/* get the decompressed length, of a compressed string */
int strdecomplen(char* source)
{
int i = 0,len = strlen(source),pos = 0;
int amt = 0;
for(i = 0; i < len; i++)
{
if(source[i] == (char)200)
{
int stop = 0;
int ic = 0;
int startpos = i + 1;
char xnum[15];
int xpos = 0;
int ia = 0;
for(ic = startpos; ic < len; ic++)
{
if(source[ic] == (char)200)
{
stop = ic;
break;
}
}
for(ia = i+1; ia < stop; ia++)
{
xnum[xpos] = source[ia];
xpos++;
}
xnum[xpos] = 0;
amt += atoi(xnum)-1;
i = stop;
continue;
}
else
{
amt++;
}
}
return amt;
}
/* set the size of the current data within the editbox */
void SetSize()
{
int size = SendMessage(editbox,WM_GETTEXTLENGTH,0,0);
char data[50];
sprintf(data,"Very Simple Text Compress - size of text ( %i ) bytes",size);
SendMessage(mainwindow,WM_SETTEXT,strlen(data),(LPARAM)(LPCSTR)data);
}
/* thats all */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -