음....
하드 구석에 있던 넘인데, 최종 수정일이 99년으로 나와있는데...
학교 수업할때만든건지, 어디서 퍼온건지, 심심해서 만든건지 기억도 않남... --;
-----------------------------------------------------------
#include "stdio.h"
#include "conio.h"
#include "stdafx.h"
#include "stdlib.h"
#include "windows.h"
void gotoxy(int x, int y)
{
COORD Pos = {x - 1, y - 1};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);
}
char *data=NULL;
char *filename=NULL;
long int filesize;
int page_now, page_tot;
int edit_mode;
int cur_x, cur_y;
FILE *fp=NULL;
void Init(void);
void Proc_End(void);
void Data_Read(int page);
void line_clear(int l);
void Print_HexaCode(int line);
void scr_clear(void);
char char_sel(char k, int a);
void Edit_Proc(void);
void view_cur(int x, int y, int e);
char atoc(char *p);
void Save_Func(void);
void ch_memset(char *p,char num,int size)
{
int i;
for(i=0;i < size;i++) p[i]=num;
}
void main(void)
{
int key;
int loop=1;
Init();
scr_clear();
while(loop)
{
if (kbhit())
{
switch(key=getch())
{
case 'o': case 'O': /* 영문자 'o' */
Init();
scr_clear();
gotoxy(1,2);
printf("Filne Name : ");
scanf("%s",filename);
fp=fopen(filename,"r+b");
if (fp==NULL)
{
line_clear(24);
gotoxy(1,24);
printf("\a에러 : %sNo Files",filename);
ch_memset(filename,0,sizeof(char)*128);
}else{
line_clear(2);
gotoxy(1,2);
printf("File Name : %s",filename);
fseek(fp,0,SEEK_END);
filesize=ftell(fp);
fseek(fp,0,SEEK_SET);
Data_Read(page_now);
Print_HexaCode(-1);
}
break;
case 'e': case 'E':
if (fp!=NULL)
{
Edit_Proc();
}else{
line_clear(24);
gotoxy(1,24);
printf("\a에러 : No Opened File",filename);
}
break;
case 73: /* page_up */
if (fp!=NULL)
{
Data_Read(page_now-1);
Print_HexaCode(-1);
}else{
line_clear(24);
gotoxy(1,24);
printf("\aError : No Opened File!!!",filename);
}
break;
case 81: /* page_down */
if (fp!=NULL)
{
Data_Read(page_now+1);
Print_HexaCode(-1);
}else{
line_clear(24);
gotoxy(1,24);
printf("\aError : No Opened File!!!",filename);
}
break;
case 27: /* Esc */
loop=0;
break;
}
gotoxy(70,1);
printf("%3d",key);
}
}
Proc_End();
system("cls");
}
void Init(void)
{
if (fp!=NULL)
{
fclose(fp);
fp=NULL;
}
if (data==NULL)
{
data=(char *)malloc(sizeof(char)*320);
}
if (filename==NULL)
{
filename=(char *)malloc(sizeof(char)*128);
}
ch_memset(data,0,sizeof(char)*320);
ch_memset(filename,0,sizeof(char)*128);
filesize=0;
page_now=0;
edit_mode=0;
page_tot=320;
}
void Proc_End(void)
{
if (fp!=NULL)
{
fclose(fp);
}
if (data!=NULL)
{
free(data);
}
if (filename!=NULL)
{
free(filename);
}
}
void Data_Read(int page)
{
long int k;
k=(long)page * 320L;
if (k > filesize)
{
line_clear(24);
gotoxy(1,24);
printf("Notice : End of File. ");
return ;
}else if (page < 0)
{
line_clear(24);
gotoxy(1,24);
printf("Notice : Top of File. ");
return ;
}else{
line_clear(24);
}
page_now=page;
fflush(fp);
fseek(fp,k,SEEK_SET);
ch_memset(data,0,sizeof(char)*320);
if (filesize-k >= 320) page_tot=320;
else page_tot=(int)(filesize-k);
fread(data,page_tot,1,fp);
}
void line_clear(int l)
{
int i;
gotoxy(1,l);
for(i=0;i < 80;i++) printf(" ");
}
void Print_HexaCode(int line)
{
int i, j, h=0, s=0;
long int k;
unsigned char c;
k=(long)page_now * 20L;
if (line < 0)
{
line=20;
}else{
s=line;
line++;
}
for(i=0;i < 20;i++)
{
gotoxy(1,3+i);
printf("%0000006lx ",k+i);
for(j=0;j < 16;j++,h++)
{
if (h < page_tot)
{
printf("%02x ",(unsigned char)data[(i*16)+j]);
}else{
printf(" ");
}
}
h-=16;
printf(" ");
for(j=0;j < 16;j++,h++)
{
if (h < page_tot)
{
c=(unsigned char)data[(i*16)+j];
if (c >= 0x20)
printf("%c",c);
else
printf(".");
}else{
printf(" ");
}
}
}
}
void scr_clear(void)
{
system("cls");
gotoxy(1,1);
printf("[o]Open [Page Up]Up [Page Down]Down [e]Edit [Esc]End");
Print_HexaCode(-1);
}
void view_cur(int x, int y, int e)
{
int i, j, k, l;
if (e==0)
{
for(i=0; i < 20; i++)
{
l=0;
for(j=0; j < 16; j++)
{
for(k=0; k < 2; k++,l++)
if (((j*2)+k)==x && i==y)
{
gotoxy(9+l,3+i);
return;
}
l++;
}
}
}else{
for(i=0; i < 20; i++)
{
for(j=0; j < 16; j++)
{
if (j==x && i==y)
{
gotoxy(61+j,3+i);
return;
}
}
}
}
}
void Edit_Proc(void)
{
int loop=1;
int t=0;
int tot_max;
int line_max;
int data_cur;
char key=0, d_key=0, buf[5]="\0";
char mode_name[2][6]={"digit","char"};
edit_mode=0;
cur_x=0;
cur_y=0;
if (edit_mode==0)
{
t=(cur_y*32)+cur_x;
tot_max=page_tot*2;
line_max=32;
}else{
t=(cur_y*16)+cur_x;
tot_max=page_tot;
line_max=16;
}
gotoxy(1,23);
printf("mode : %6s , cur_x : %2d, cur_y : %2d",mode_name[edit_mode],cur_x, cur_y);
line_clear(1);
gotoxy(1,1);
printf("[↑↓←→]Move [tab] Switch [Esc]End");
view_cur(cur_x, cur_y, edit_mode);
while(loop)
{
if (kbhit())
{
d_key=0;
key=getch();
switch(key)
{
case 9: /* tab */
if (edit_mode==0)
{
edit_mode=1;
cur_x/=2;
t=(cur_y*16)+cur_x;
line_max=16;
tot_max=page_tot;
}else{
edit_mode=0;
cur_x*=2;
t=(cur_y*32)+cur_x;
line_max=32;
tot_max=page_tot*2;
}
view_cur(cur_x, cur_y, edit_mode);
break;
case 75: /* left */
if (t > 0) t--;
break;
case 77: /* right */
if (t < tot_max) t++;
break;
case 72: /* up */
if (t-line_max > 0) t-=line_max;
break;
case 80: /* down */
if (t+line_max < tot_max) t+=line_max;
break;
case 27:
line_clear(1);
Save_Func();
gotoxy(1,1);
printf("[o]Open [Page Up]Up [Page Down]Down [e]Edit [Esc]End");
return;
default:
d_key=char_sel(key, edit_mode);
}
if (edit_mode==0)
{
data_cur=t/2;
}else{
data_cur=t;
}
if (d_key!=0 && data_cur < page_tot)
{
if (edit_mode==0) /* digit edit */
{
sprintf(buf,"%02x",(unsigned char)data[data_cur]);
buf[cur_x%2]=d_key;
data[data_cur]=atoc(buf);
t++;
}else{ /* char edit */
data[data_cur]=d_key;
t++;
}
Print_HexaCode(cur_y);
}
if (edit_mode==0)
{
cur_x=t%32;
cur_y=t/32;
}else{
cur_x=t%16;
cur_y=t/16;
}
gotoxy(1,23);
printf("mode : %6s , cur_x : %2d, cur_y : %2d",mode_name[edit_mode],cur_x, cur_y);
view_cur(cur_x, cur_y, edit_mode);
}
}
}
char char_sel(char k, int a)
{
unsigned char key=(unsigned char)k;
if (a==0)
{
if (key >= '0' && key <='9')
{
return k;
}else if(key >= 'a' && key <='f')
{
return k;
}else if(key >= 'A' && key <='F')
{
return (char)('a'+(key-'A'));
}
}else{
if (key >= 0x20 && key <=0x7f)
{
return k;
}
}
return 0;
}
char atoc(char *p)
{
unsigned int l=0, k;
int i;
for(i=0;p[i]!='\0';i++)
{
l*=16;
k=(unsigned int)p[i];
if (k >= '0' && k <='9')
{
l+=k-'0';
}else if(k >= 'a' && k <='f')
{
l+=10+(k-'a');
}else if(k >= 'A' && k <='F')
{
l+=10+(k-'A');
}
}
return (char)l;
}
void Save_Func(void)
{
char c=0;
line_clear(23);
while(c!='y' && c!='Y' && c!='n' && c!='N')
{
gotoxy(1,23);
printf("Save changes? (y/n) \a");
scanf("%c",&c);
}
if (c=='y' || c=='Y')
{
fflush(fp);
fseek(fp,(long)page_now*320L,SEEK_SET);
fwrite(data,page_tot,1,fp);
fflush(fp);
}
line_clear(23);
}