#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
#include<conio.h>

typedef struct _node
{
    char* data;
    struct _node* next;
    struct _node* back;
}node;
node *head, *tail;
int total, now;  // 전체페이지, 현재페이지 출력 변수

void init_line(void);
void load_file(void);
void show_header(void);
void show_page(node*);
void key_proc(void);
void move_line(int, node**);
void gotoxy(int, int);

void main()
{
    init_line();
    load_file();
    key_proc();
    puts("Program ends...");
}

//노드 초기화 함수
void init_line(void)
{
    head = (node*)malloc(sizeof(node));
    tail = (node*)malloc(sizeof(node));
    head->next = tail;
    head->back = head;
    tail->back = head;
    tail->next = tail;
}

//파일 로드 함수
void load_file(void)
{
    FILE *fp;
    char buf[256];
    node* temp;
    if((fp = fopen("test.txt","rt"))==NULL)
    {
        puts("Error : Can`t read that file.");
        exit(1);
    }
    while(!feof(fp))
    {
        fgets(buf, 255, fp);
        if(strlen(buf)>80)
            buf[80] = 0;  // 최대길이 80
        if((temp = (node*)malloc(sizeof(node))) == NULL)
        {
            puts("Error : Out of Memory.");
            exit(1);
        }
        if((temp->data = (char*)malloc(strlen(buf))) == NULL)
        {
            puts("Error : Out of Memory.");
            exit(1);
        }
        strcpy(temp->data, buf);
        temp->next = tail;
        temp->back = tail->back;
        tail->back->next = temp;
        tail->back = temp;
        total++;  //전체 갯수 증가
    }
    fclose(fp);
}  

//화면 출력 함수
void show_page(node* temp)
{
    int line = 0;
    system("cls");
    show_header();
    gotoxy(1, 2);
    while(line++ < 23 && temp != tail)
    {
        cprintf("%-80s\r", temp->data);
        temp = temp->next;
    }
}

//키 제어 함수
void key_proc(void)
{
    node* temp;
    now = 1;
    temp = head->next;
    show_page(temp);
    while(1)
    {
        //ESC키 눌렀을때
        if(GetAsyncKeyState(VK_ESCAPE) & 0x8000)
            break;
        //PgUp키 눌렀을때
        if(GetAsyncKeyState(VK_PRIOR) & 0x8000)
        {
            move_line(-23, &temp);
            show_page(temp);
        }
        //PgDn키 눌렀을때
        if(GetAsyncKeyState(VK_NEXT) & 0x8000)
        {
            move_line(23, &temp);
            show_page(temp);
        }
        //▲키 눌렀을때
        if(GetAsyncKeyState(VK_UP) & 0x8000)
        {
            move_line(-1, &temp);
            show_page(temp);
        }
        //▼키 눌렀을때
        if(GetAsyncKeyState(VK_DOWN) & 0x8000)
        {
            move_line(1, &temp);
            show_page(temp);
        }
    }
    system("cls");
}

//페이지 이동 함수
void move_line(int d, node** temp)
{
    if(d<0)
    {
        while(d++ < 0 && (*temp)->back != head)
        {
            *temp = (*temp)->back;
            now--;
        }
    }
    else
    {
        while(d-- > 0 && (*temp)->next != tail)
        {
            *temp = (*temp)->next;
            now++;
        }
    }
}

//출력화면의 최상단 화면
void show_header(void)
{
    HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    GetConsoleScreenBufferInfo(hout, &csbi);

    //출력색 변환
    SetConsoleTextAttribute(hout, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
    gotoxy(1, 1);
    cprintf("TVIEW : %-12s     Loc : %6d of %6d     By Jeong Jin Hwa     ", "test.txt", now, total);
    //원래색으로 변환
    SetConsoleTextAttribute(hout, csbi.wAttributes);
}

//gotoxy함수
void gotoxy(int x, int y)
{
   COORD Pos = {x - 1, y - 1};
   SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);
}

+ Recent posts