frustreated 发表于 2013-5-14 22:00:26

阅读器(只能读英文)

花了整整两天,自己写了个阅读器(匆匆忙完,不免有bug--)真机测试可行(cas + click版),文件越大,打开速度越慢。打开后,阅读流畅。
按键(上 )   向前翻一页
按键(下 )   向后翻一页
按键(左 )   向前翻20页
按键(右 )   向后翻20页
esc退出
不能改变字体大小和颜色
只能读英文,而且,最好目录不含有中文
现把代码和程序发布,等有空再改进吧。里面有一个函数剽窃的作者
作者发布的帖子地址为:http://www.omnimaga.org/index.php?topic=7155.0
希望原作者能原谅本人的不端行为。{:011:}

本人邮箱:[email protected]
有在这方面交流,或其他方面(微分,变分,勒贝格积分,fourier变换或者希尔伯特等之类)交流的同学都欢迎~~

代码:
#include <os.h>
#include <nspireio2.h>
#include <stdio.h>      /* printf */
#include <stdlib.h>   /* strtol */
char* numToStr(int num) {
    char* str;
    char digit[]="0123456789ABCDEF";
    int numcopy=num;
    int numdigits=0;
    while(numcopy>0) {
      numcopy/=10;
      numdigits++;
    }
    int i=0;
    str=malloc(numdigits+1);
    while(num>0) {
      str=digit;
      num/=10;
      i++;
    }
    str=0;
    return str;
}
int GetDirNum(char *path_name)
{
        DIR* dir;
        struct dirent *ptr;
        int dir_num=0;//当前目录下总文件数目(包含文件夹)//类型为整形
        //char *dir_char=(char*)malloc(8*sizeof(char));//当前目录下总文件数目(包含文件夹)//类型为字符串
        dir=opendir(path_name);//"/documents"
        while((ptr=readdir(dir))!= NULL)
        {dir_num++;}
        closedir(dir);
        return (dir_num);
}
void ShowDirectory(int file_choose,char* path_name,char* file_name,nio_console *csl)//返回值为file_%name
{       
        file_choose=file_choose%(GetDirNum(path_name));
        if(file_choose<0)
        {file_choose=file_choose+GetDirNum(path_name);}
        nio_Clear(csl);
        nio_InitConsole(csl, 52, 29, 0, 0, 15,0 );
        nio_DrawConsole(csl);
        DIR* dir;struct dirent *ptr;
        int size_file_name=-1;//文件名长度
        dir = opendir(path_name);
        int dir_num_show=0;//第num个文件//要选择的
        while((ptr = readdir(dir))!= NULL)
        {
        if(dir_num_show==(file_choose))
                {
                        size_file_name=strlen(ptr->d_name);//获取文件名长度
                        memcpy(file_name,ptr->d_name,size_file_name);
                        file_name='\0';
                        nio_drawstr(0,0,0,dir_num_show,ptr->d_name,0,15);
                        nio_printf(csl,"\n");
                }
        else
                {
                        nio_printf(csl,ptr->d_name);
                        nio_printf(csl,"\n");
                }
        dir_num_show++;
        }
        closedir(dir);
}
void GetParrentPath(char* path_name)
{
        int i=strlen(path_name);//逆序寻找字符"/",并设置成'/0'
        for(;i>1;i--)
        {
                if(path_name=='/')
                {path_name=='\0';break;}
        }
}

typedef struct St_Line
{
        char *linestring;
        struct St_Line *pNext;
        struct St_Line *pPre;
        struct St_Line *pCurrent;
}StLine;

void Initialize(StLine * pLine)
{
        pLine->linestring=NULL;pLine->pCurrent=NULL;pLine->pNext=NULL;pLine->pPre=NULL;
}
void Show29Line(nio_console *csl,StLine *pLine)
{
        if((pLine->pCurrent)!=NULL)
        {
                //~ nio_Clear(csl);
        clrscr();
        nio_InitConsole(csl, 52, 29, 0, 0, 15,0 );
        nio_DrawConsole(csl);
        int i=0;
        for (i=0;i<29;i++)
        {
                //pLine->pCurrent->linestring='\n';pLine->pCurrent->linestring='\0';
                nio_PrintStr(csl,pLine->pCurrent->linestring);
                //nio_PrintStr(csl,numToStr(strlen(pLine->pCurrent->linestring)));//i++;
                //~ if(strlen(pLine->pCurrent->linestring)==52)
                //~ {nio_PrintStr(csl,"\n");}
                if((pLine->pCurrent->pNext)!=NULL)
                {
                        pLine->pCurrent=pLine->pCurrent->pNext;
                }
                else
                {return;}
        }}
}
int main(void)
{
        nio_console csl;
        lcd_ingray();
        clrscr();
        // 52 columns, 29 rows. 0px offset for x/y.
        // Background color 15 (white), foreground color 0 (black)
        int file_choose=1;
        char *path_name=(char *)malloc(128*sizeof(char));//
        char *file_name=(char *)malloc(128*sizeof(char));
        ShowDirectory(file_choose,path_name,file_name,&csl);
        StLine *pLine=(StLine *)malloc(sizeof(StLine));
        StLine *pEnd_pre_29;
        Initialize(pLine);
        while(1)
        {
                wait_key_pressed();
                if(isKeyPressed(KEY_NSPIRE_8)||isKeyPressed(KEY_NSPIRE_UP))//Modify!!!!
                        {file_choose--;ShowDirectory(file_choose,path_name,file_name,&csl);}
                if(isKeyPressed(KEY_NSPIRE_2)||isKeyPressed(KEY_NSPIRE_DOWN))//Modify!!!!
                        {file_choose++;ShowDirectory(file_choose,path_name,file_name,&csl);}
                if(isKeyPressed(KEY_NSPIRE_ENTER))//Modify!!!!KEY_NSPIRE_ENTER
                {
                        if(strstr(file_name,".")==NULL)
                        {
                                strcat(path_name,"/");
                                strcat(path_name,file_name);
                                ShowDirectory(file_choose,path_name,file_name,&csl);
                        }
                        else
                        {
                                if((strlen(path_name)>1)&&(strstr(file_name,"..")!=NULL))//如果路径名长度大于1(考虑路径名为"/"的情况)
                                {
                                        char* separator;
                                        separator=strrchr(path_name,'/');
                                        nio_printf(&csl,path_name);
                                        separator='\0';
                                        ShowDirectory(file_choose,path_name,file_name,&csl);
                                }
                                else
                                {
                                        if(strstr(file_name,".tns")!=NULL)
                                        {
                                                strcat(path_name,"/");
                                                strcat(path_name,file_name);
                                                FILE *pFile;
                                                pFile=fopen(file_name,"r");
                                                int Line_Num=0;
                                                while (!feof(pFile))
                                                {
                                                        Line_Num++;
                                                        if (Line_Num==1)
                                                        {
                                                                pLine->linestring=(char*)malloc(53*sizeof(char));
                                                                pLine->linestring=fgets(pLine->linestring,52*sizeof(char),pFile);
                                                                pLine->pCurrent=pLine;
                                                        }
                                                        else
                                                        {
                                                                pLine->pCurrent->pNext=(StLine *)malloc(sizeof(StLine));
                                                                Initialize(pLine->pCurrent->pNext);
                                                                pLine->pCurrent->pNext->pPre=pLine->pCurrent;
                                                                pLine->pCurrent=pLine->pCurrent->pNext;
                                                                pLine->pCurrent->pNext=NULL;
                                                                pLine->pCurrent->linestring=(char*)malloc(53*sizeof(char));
                                                                fgets(pLine->pCurrent->linestring,52*sizeof(char),pFile);
                                                        }
                                                }
                                                fclose(pFile);
                                                pEnd_pre_29=pLine->pCurrent;
                                               
                                                int i=0;
                                                for(i=0;i<29;i++)
                                                {
                                                        if(pEnd_pre_29->pPre!=NULL)
                                                        {pEnd_pre_29=pEnd_pre_29->pPre;}
                                                        else
                                                        {
                                                                pEnd_pre_29=pLine;
                                                                break;
                                                        }
                                                }
                                                pLine->pCurrent=pLine;
                                                Show29Line(&csl,pLine);
                                                while(1)
                                                {
                                                        wait_key_pressed();
                                                        if(isKeyPressed(KEY_NSPIRE_DOWN))
                                                        {
                                                                Show29Line(&csl,pLine);
                                                        }
                                                        if(isKeyPressed(KEY_NSPIRE_UP))//&&(pLine->pCurrent->pNext!=NULL))
                                                        {
                                                                if((pLine->pCurrent)!=NULL)
                                                                {for(i=0;i<58;i++)//
                                                                {
                                                                        if((pLine->pCurrent->pPre)!=NULL)
                                                                        {pLine->pCurrent=pLine->pCurrent->pPre;}
                                                                        else{pLine->pCurrent=pLine;break;}
                                                                }}
                                                                Show29Line(&csl,pLine);
                                                        }
                                                        if(isKeyPressed(KEY_NSPIRE_RIGHT))
                                                        {
                                                                for (i=0;i<(29*20);i++)
                                                                {
                                                                        if((pLine->pCurrent->pNext)==NULL)
                                                                        {pLine->pCurrent=pEnd_pre_29;break;}
                                                                        else
                                                                        {pLine->pCurrent=pLine->pCurrent->pNext;}
                                                                }
                                                                Show29Line(&csl,pLine);
                                                        }
                                                        if(isKeyPressed(KEY_NSPIRE_LEFT))
                                                        {
                                                                for (i=0;i<(29*20);i++)
                                                                {
                                                                        if((pLine->pCurrent->pPre)==NULL)
                                                                        {pLine->pCurrent=pLine;break;}
                                                                        else
                                                                        {pLine->pCurrent=pLine->pCurrent->pPre;}                                                                       
                                                                }
                                                                Show29Line(&csl,pLine);
                                                        }
                                                        if(isKeyPressed(KEY_NSPIRE_ESC))
                                                        {
                                                                pLine->pCurrent=pLine;
                                                                while((pLine->pCurrent->pNext)!=NULL)
                                                                {
                                                                        free(pLine->pCurrent->linestring);///释放字符串内存
                                                                        pLine->pCurrent->linestring=NULL;
                                                                        pLine->pCurrent=pLine->pCurrent->pNext;
                                                                }
                                                                free(pLine->pCurrent->linestring);
                                                                while((pLine->pCurrent->pPre)!=NULL)
                                                                {
                                                                        pLine->pCurrent=pLine->pCurrent->pPre;
                                                                        free(pLine->pCurrent->pNext);//释放字符串地址的内存
                                                                        pLine->pCurrent->pNext=NULL;
                                                                }
                                                                free(pLine->pCurrent->pNext);
                                                                free(pLine);
                                                                return;
                                                        }
                                                }
                                        }
                                }
                        }
                }
                if(isKeyPressed(KEY_NSPIRE_ESC))
                {break;}               
        };
       
                free(file_name);
                free(path_name);
                free(pLine);
                free(pEnd_pre_29);
      nio_CleanUp(&csl);
      return 0;
}

frustreated 发表于 2013-5-14 22:04:10

附件在此~

DAS 发表于 2013-5-14 23:26:30

鼓励一下。

frustreated 发表于 2013-5-15 08:54:58

稍稍对程序做了修正,去掉了右侧边缘刷新不掉的痕迹。

白菜白 发表于 2013-5-15 12:20:47

好动西 学习了
页: [1]
查看完整版本: 阅读器(只能读英文)