main.c
判断输入参数及调用draw.c函数
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <config.h>
#include <draw.h>
#include <encoding_manager.h>
#include <fonts_manager.h>
#include <disp_manager.h>
#include <string.h>
/* ./show_file [-s Size] [-f freetype_font_file] [-h HZK] <text_file> */
int main(int argc, char **argv)
{
int iError;
unsigned int dwFontSize = 16;
char acHzkFile[128];
char acFreetypeFile[128];
char acTextFile[128];
char acDisplay[128];
char cOpr;
int bList = 0;
acHzkFile[0] = '\0';
acFreetypeFile[0] = '\0';
acTextFile[0] = '\0';
strcpy(acDisplay, "fb");
//1、使用getopt解析参数里面的选项,当遇到第一个选项字符时,并将全局变量optarg指向该选项的参数,这里使用while,找到了optstring中的所有选项
while ((iError = getopt(argc, argv, "ls:f:h:d:")) != -1)
{
switch(iError)
{
case 'l':
{
bList = 1;
break;
}
case 's':
{
dwFontSize = strtoul(optarg, NULL, 0);
break;
}
case 'f':
{
//2、指定长度的字符串拷贝
strncpy(acFreetypeFile, optarg, 128);
acFreetypeFile[127] = '\0';
break;
}
case 'h':
{
strncpy(acHzkFile, optarg, 128);
acHzkFile[127] = '\0';
break;
}
case 'd':
{
strncpy(acDisplay, optarg, 128);
acDisplay[127] = '\0';
break;
}
default:
{
printf("Usage: %s [-s Size] [-d display] [-f font_file] [-h HZK] <text_file>\n", argv[0]);
printf("Usage: %s -l\n", argv[0]);
return -1;
break;
}
}
}
//3、optind代表optarg下一个要处理的命令行参数的索引,这里optind >= argc的话,代表没有对应的参数
if (!bList && (optind >= argc))
{
printf("Usage: %s [-s Size] [-d display] [-f font_file] [-h HZK] <text_file>\n", argv[0]);
printf("Usage: %s -l\n", argv[0]);
return -1;
}
//4、disp显示结构体注册
iError = DisplayInit();
if (iError)
{
printf("DisplayInit error!\n");
return -1;
}
//5、glyph结构体注册
iError = FontsInit();
if (iError)
{
printf("FontsInit error!\n");
return -1;
}
//6、编码结构体注册
iError = EncodingInit();
if (iError)
{
printf("EncodingInit error!\n");
return -1;
}
if (bList)
{
//7、打印所有disp的结构体name(所有支持的disp方式:fb)
printf("supported display:\n");
ShowDispOpr();
//8、打印所有glyph的结构体name(所有获取glyph的方式:ascii,freetype,gbk)
printf("supported font:\n");
ShowFontOpr();
//9、打印所有支持编码的结构体name(所有支持的编码:ascii,utf-8,utf-16)
printf("supported encoding:\n");
ShowEncodingOpr();
return 0;
}
//10、optarg解析完参数后,未解析到的参数就是texstfile(novel_file)
strncpy(acTextFile, arg