|
楼主 |
发表于 2008-7-6 20:40:41
|
显示全部楼层
终于让我解决了!
终于让我解决了!原来是我从笔记本上边做这个练习结果每次分配的边界超出了笔记本特殊的屏幕范围。(25rows*80cols)
重新加上了移动坐标范围的检测代码。并且加了一个自动获取terminfo的函数来获得做大的rows和cols。
- #include <stdio.h>
- #include <errno.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <curses.h>
- #include <term.h>
- #include <string.h>
- #include <termios.h>
- #include <sys/ioctl.h>
- struct position
- {
- int x ;
- int y ;
- };
- int return_winsize(int fd,struct winsize *size)
- {
- if(ioctl(fd,TIOCGWINSZ,(char*) size) < 0)
- {
- perror("iotcl is faild\n");
- exit(0);
- }
- printf("%d rows,%d colnums\n",size->ws_row,size->ws_col);
- return 0;
- }
- char *get_filled_buff(char *file,int length)
- {
- int fd;
- ssize_t reallength;
- char *buff;
- int count;
-
- if(length > 0 &&length < 1024)
- {
- buff = (char*) malloc(length+1);
- if(buff == NULL)
- {
- perror("buff is NULL! so kill proccess.\n");
- exit(1);
- }
-
- }else
- {
- perror("length is NOT suitable! so kill proccess.\n");
- exit(1);
- }
-
- fd = open(file,O_RDONLY);
- if(fd == -1)
- {
- free(buff);
- perror("can NOT open the file\n");
- exit(1);
- }
-
- reallength = read(fd,buff,length);
- if(reallength == -1)
- {
- close(fd);
- free(buff);
- perror("can NOT read the file\n");
- exit(1);
- }
- for(count=0;count < reallength;count++)
- {
- if(buff[count] == '\n' ||buff[count] == '\r')
- buff[count] = ' ';//变成空格
- }
- buff[reallength] = '\0';
-
- close(fd);
-
- return buff;
- }
- int print_string_with_realx(char* buff,int y,int x,int realy)
- {
- int c;
- if(move(y,x) == ERR)
- {
- addstr("move ERR\n");
- }
- for(c = 0;c < realy&&buff[c] != '\0';c++)
- {
- addch(buff[c]);
- }
- refresh();
- return 0;
- }
- int print_blank_string_with_realx(char* buff,int y,int x,int realy)
- {
- int c;
- if(move(y,x) == ERR)
- {
- addstr("move blank ERR\n");
- }
- for(c = 0;c < realy&&buff[c] != '\0';c++)
- {
- addch(' ');
- }
- refresh();
- return 0;
- }
- int Theater_Marquee(char *file,int length,int y,int x,int speed)
- {
- int c;
- int realy;
- int max_x;
- int max_y;
-
- char *buff;
- char *buffpointer;
- buff = get_filled_buff(file,length);
- buffpointer = buff;
-
- struct winsize size;
- return_winsize(STDIN_FILENO,&size);
- max_x = size.ws_col-10;//80-10
- max_y = size.ws_row-5;//25-5
- if(y > max_y)
- {
- perror("y > max_y\n");
- exit(1);
- }
- sleep(3);
- //printf("%s\n",buff);
- initscr() ;
-
- clear();
- //attron(A_BLINK);
-
- for(c = 0;c < strlen(buff) ;c++)
- {
- if(c/max_x<1)
- {
- realy = c+1;
- x = max_x - realy;
- }
- else
- {
- realy = max_x;
- x = 0;
- buff++;
- }
- if(realy > strlen(buff))
- break;
-
- print_string_with_realx(buff,y,x,realy);
- usleep(speed);
-
- print_blank_string_with_realx(buff,y,x,realy);
- }
-
- //attroff(A_BLINK);
- endwin();
- free(buffpointer);
- return 0;
- }
- int usage()
- {
- printf("USAGE(i.e) : ./TheaterMarquee2 -f ./1txt -l 50 -y 20 -s 70000\n(-x is already disabled!)\n");
- exit(0);
- }
- int main(int ac,char **av)
- {
- char *filepath;
- int length;
- struct position position1;
- int speed;
- int c;
-
- if(ac != 9)//这里只是简单的凑服了一下其实不能排除所有的异常情况
- {
- usage();
- }
-
- while((c=getopt(ac,av,"f:l:y:s:"))!=EOF)
- {
- switch (c)
- {
- case 'f':
- filepath = optarg;
- break;
- case 'l':
- length = atoi(optarg);
- break;
- //case 'x': //其实x系数没有用,起作用的是y系数
- //position1.x = atoi(optarg);
- //break;
- case 'y':
- position1.y = atoi(optarg);
- break;
- case 's':
- speed = atoi(optarg);
- break;
- default:
- usage();
- exit(1);
- } /* case */
- }
-
- //printf("%s\n%d\n%d\n%d\n%d\n",filepath,length,position1.x,position1.y,speed);
-
- Theater_Marquee(filepath,length,position1.y,10,speed);//position1.x
- return 0;
- }
复制代码 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|