⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dic.c

📁 redboy for gba 是BPNS为GBA编写的一个小软件。软件的邹形是BASIC高级语言解释执行器。几经修改和扩展
💻 C
字号:
#include <CsAgb.h>
#include <rbasic.h>
#include <rb_stdio.h>
#include <Qgraph.h>
#include <graph.h>
#include <sys.h>
#include <string.h>
#include <rb_string.h>
#define dic_key_c RGB(30,0,30)
#define dic_key_bg RGB(30,30,0)
#define dic_bord_c RGB(25,25,25)
#define dic_fc RGB(2,13,5)
#define dic_bg RGB(7,13,20)
#define dic_bord_xp 5
#define dic_bord_yp 95
#define max_len 25
#define dic_quit 7
#define dic_del 8
#define dic_ok 9
#define dic_cap 10
#define dic_sel 11
#define dic_indx *(u32 *)0x06012C00
extern void dic_set(char *M,u8 act);//字库设置
extern char _folder[40];
extern char _file[40];
extern u32 _file_loc;
extern u8 is_basic_run;
extern u8 arlt(char *tit,char *mes);
u32 dic_get_u32 (u32 loc);
void dic_capLock();
extern void wait(u32 count);
const u8 dic_dre[]={
0x00,0x00,0x0c,0x00,0x1c,0x00,0x3c,0x00,0x7c,0x00,0x7c,0x00,0x3c,0x00,0x1c,0x00,0x0c,0x00,0x00,0x00,//UP
0x00,0x00,0x18,0x00,0x1c,0x00,0x1e,0x00,0x1f,0x00,0x1f,0x00,0x1e,0x00,0x1c,0x00,0x18,0x00,0x00,0x00,//downd
0x00,0x00,0x00,0x00,0x00,0x00,0x1c,0x00,0x3e,0x00,0x7f,0x00,0xff,0x80,0x00,0x00,0x00,0x00,0x00,0x00,//left
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0x80,0x7f,0x00,0x3e,0x00,0x1c,0x00,0x00,0x00,0x00,0x00,//right
0x00,0x00,0x19,0x80,0x3b,0x80,0x7f,0x80,0xff,0x80,0xff,0x80,0x7f,0x80,0x3b,0x80,0x19,0x80,0x00,0x00,//L
0x00,0x00,0x66,0x00,0x77,0x00,0x7f,0x80,0x7f,0xc0,0x7f,0xc0,0x7f,0x80,0x77,0x00,0x66,0x00,0x00,0x00,//R
};
const u8 dic_yb[12*10]={
   36,24,36,28,34,34,34,34,28,0,
   0,0,0,118,9,63,72,73,54,0,
   0,0,0,26,38,34,34,38,26,0,
   0,0,0,28,34,34,2,34,28,0,
   0,0,0,28,34,3,62,34,28,0,
   6,8,8,8,8,8,8,8,8,48,
   0,0,0,44,58,34,34,34,34,6,
   28,54,34,34,62,34,34,34,28,0,
   0,0,0,8,20,20,34,34,34,0,
   0,0,0,30,2,12,3,1,1,62,
   0,0,0,28,34,2,12,34,28,0,
   0,0,0,30,32,32,24,34,60,0
};
const char ch_hi[26]={'Q','W','E','R','T',
	'Y','U','I','O','P',
	'A','S','D','F','G',
	'H','J','K','L','N',
	'Z','X','C','V','B',
	'M'};
   
typedef struct
{
	char hmes[30];//文件信息
	u8 rsv[20];//保留区
	u8 mask;//加密码
	u32 count;//记录个数
	u32 loc;//首个记录位置
	u32 index[26];//A-Z记录索引
} dichead;
extern u8 font_mask[8];
extern void (*sys_deamon)();
extern u32 TM_CN;
extern void title(char *mes);
extern u32 list_fsys(u32 loc);
extern u32 myfopen(char *fname,u32 *size);
u8 get_dic_mes(dichead *head,u32 loc)
{
   int i;
   char *temp=(char *)head;
   for (i=0;i<sizeof(dichead);i++) temp[i]=*(char *)(loc+i);
   if (head->hmes[0]=='d' && head->hmes[1]=='i' && head->hmes[2]=='c') return 1;
   else return 0;//非dic文件
}
void dic_yinb(u8 xp,u8 yp,u8 id)
{
   u8 dot,i,j;
   u32 lc=240*yp+xp;
   u32 loc=id*10;
   for (i=0;i<10;i++)
   {
      dot=dic_yb[loc+i];
      for (j=0;j<7;j++)
      {
         if (dot&font_mask[j+1]) *(mVRAM+lc+j)=dic_fc;
      }
      lc+=240;
   }
}
void paint_yb(u8 xp,u8 yp,char *str)
{
   u8 i=0;
   while (str[i]!='\0')
   {
      if (str[i]>='0' && str[i]<='9') dic_yinb(xp,yp,str[i]-'0');
      else if(str[i]=='\\') dic_yinb(xp,yp,10);
      else if(str[i]=='A') dic_yinb(xp,yp,11);
      else paint_eng(xp,yp,dic_fc,str[i]);
      i++;
      xp+=7;
   }
}
void get_word(char *word,u32 loc)
{
   u32 i=0;
   while (1)
   {
      word[i]=*(char *)(loc+i);
      if (word[i]<128) word[i]+=128;
		else word[i]-=128;
		if (i>40 || word[i]=='#') break;
		i++;
   }
   word[i]='\0';
}
void get_word_mes(char *word,u32 loc)
{
   u32 i=0;
   while (1)
   {
      word[i]=*(char *)(loc+i);
      if (word[i]>=128) word[i]-=128;
		else word[i]+=128;
		if (i>400 || word[i]=='\n') break;
		i++;
   }
   word[i]='\0';
}
void dic_txt(char *mes)
{
   u8 xp=8,yp=63;
   u32 sp=0;
   while (mes[sp]!='\0' && mes[sp]!='#')
   {
      if (mes[sp]>127)
      {
         if (xp>218) {xp=8;yp+=14;}
         paint_ch(xp,yp,dic_fc,mes+sp);
         sp+=2;
         xp+=12;
      }
      else if (mes[sp]>=32)
      {
         if (xp>228) {xp=8;yp+=14;}
         paint_eng(xp,yp+1,dic_fc,mes[sp]);
         if (mes[sp]==';' && mes[sp+1]>='a' && mes[sp+1]<='z') {xp=8;yp+=14;}
         else xp+=7;
         sp++;
      }
      else sp++;
   }
}
void show_word_mes(char *word)
{
   u32 i=0;
   u32 sp=0;
   char mes[400];
   while (word[i]!='#' && word[i]!='\0')
   {
      mes[sp]=word[i];
      i++;
      sp++;
   }
   mes[sp]='\0';
   cwrite(8,28,dic_fc,mes);
   i++;
   if (word[i]=='[')
   {
      sp=0;
      while (word[i]!=']')
      {
         mes[sp]=word[i];
         i++;
         sp++;
      }
      mes[sp]=']';
      mes[sp+1]='\0';
      i++;
      paint_yb(8,45,mes);
   }
   sp=0;
   dic_txt(word+i);
}

u32 dic_search(char *word,dichead *head,u32 loc)
{
   u32 low,hi,mid,get;
   char str[50];
   int cmp;
   u8 ch=word[0]<'a'?(word[0]-'A'):(word[0]-'a');
   if (!is_letter(word[0])) return 0;
   low=head->index[ch];
   if (word[0]!='Z' && word[0]!='z') hi=head->index[ch+1]-1;
   else hi=head->count-1;
   while (hi>low+1)
   {
      mid=low+(hi-low)/2;
      get=dic_get_u32(loc+sizeof(dichead)+sizeof(u32)*mid);
      get_word(str,loc+head->loc+get);
      cmp=strcmp(word,str);
      if (cmp==0) {dic_indx=mid;return loc+get+head->loc;}
      else if(cmp>0) low=mid;
      else hi=mid;
   }
   get=dic_get_u32(loc+sizeof(dichead)+sizeof(u32)*low);
   get_word(str,loc+head->loc+get);
   if (strcmp(word,str)<=0)
   {
      dic_indx=low;
      return loc+get+head->loc;
   }
   dic_indx=hi;
   get=dic_get_u32(loc+sizeof(dichead)+sizeof(u32)*hi);
   return loc+get+head->loc;
}
u32 dic_get_u32(u32 loc)
{
   int i;
   u32 re=0;
   for (i=3;i>=0;i--) re=re*256+*(u8 *)(loc+i);
   return re;
}
      
void dic_record(char *word,dichead *head,u32 loc,u32 n)
{
   u32 get;
   get=dic_get_u32(loc+sizeof(dichead)+sizeof(u32)*n);
   get_word(word,loc+get+head->loc);
}
void dic_deamon()
{
   if (TM_CN%2) Q_box(9+rb_xp*7,8,14+rb_xp*7,18,dic_fc);
   else Q_box(9+rb_xp*7,8,14+rb_xp*7,18,RGB(30,30,30));
}

void paint_dre(u8 xp,u8 yp,u16 color,u8 ch)//汉字显示
{
   u32 i,j;
   u8 dot;
   u32 lc=yp*240+xp;
   u32 loc=ch*20;
   for (i=0;i<10;i++)
   {
      dot=dic_dre[loc+i*2];
      for (j=0;j<8;j++)//左八位點陣信息
      {
         if (dot&font_mask[j]) *(mVRAM+lc+j)=color;
      }
      dot=dic_dre[loc+i*2+1];
      for (j=0;j<2;j++)//後两位點陣信息
      {
         if (dot&font_mask[j]) *(mVRAM+lc+j+8)=color;
      }
      lc+=240;
   }
}
void n_box(u8 xp,u8 yp,u8 xsp,u8 ysp,u16 co,u16 bg)
{
   r_box(xp,yp,xsp,ysp,co,0);
   r_draw(xp,yp,bg,0);
   r_draw(xp,ysp,bg,0);
   r_draw(xsp,yp,bg,0);
   r_draw(xsp,ysp,bg,0);
}
void dic_key(u8 xp,u8 yp,u8 ch)
{
   n_box(xp,yp,xp+14,yp+10,dic_key_c,dic_bord_c);
   Q_box(xp+1,yp+1,xp+13,yp+9,dic_key_bg);
   paint_en(xp+4,yp+2,dic_key_c,ch);
}
void dic_key_l(u8 xp,u8 yp,u8 *ch)
{
   n_box(xp,yp,xp+14+18,yp+10,dic_key_c,dic_bord_c);
   Q_box(xp+1,yp+1,xp+13+18,yp+9,dic_key_bg);
   write(xp+5,yp-1,dic_key_c,ch);
}
void dic_board()
{
   u8 xp=dic_bord_xp,yp=dic_bord_yp;
   u8 i,j,k=0;
   //char ch='A';
   n_box(xp,yp,xp+230,yp+60,dic_bord_c,dic_bg);
   Q_box(xp+1,yp+1,xp+229,yp+59,dic_bord_c);
   dic_capLock();
   for (i=0;i<2;i++)
   for (j=0;j<10;j++)
   {
      dic_key(xp+(j+1+(j>4?1:0))*18+4,yp+(i+1)*14+4,ch_hi[k]);;
      k++;
   }
   for (j=0;j<6;j++)
   {
      dic_key(xp+(j+1+(j>4?1:0))*18+4,yp+(i+1)*14+4,ch_hi[k]);
      k++;
   }
   dic_key_l(xp+(j+2)*18+4,yp+(i+1)*14+4,"BLK");
   j+=2;
   dic_key_l(xp+(j+2)*18+4,yp+(i+1)*14+4,"DEL");
   for (i=0;i<5;i++)
   {
      paint_dre(xp+(i+1)*18+6,yp+4,dic_key_c,i);
      paint_dre(xp+(i+7)*18+6,yp+4,dic_key_c,i);
   }
   for (i=0;i<3;i++)
   {
      paint_dre(xp+8,yp+5+(i+1)*14,dic_key_c,i*2);
      paint_dre(xp+8+6*18,yp+5+(i+1)*14,dic_key_c,i*2+1);
   }
   n_box(xp+8,yp+5,xp+18,yp+12,dic_key_c,dic_bord_c);
}
u8 dic_get_key()
{
   while (1)
   {
      readkey();
      if (CS_IsKeyDown(KEY_A)) return dic_ok;
      if (CS_IsKeyDown(KEY_B)) return 0;
      if (CS_IsKeyDown(KEY_UP)) return 3;
      if (CS_IsKeyDown(KEY_DOWN)) return 4;
      if (CS_IsKeyDown(KEY_LEFT)) return 1;
      if (CS_IsKeyDown(KEY_RIGHT)) return 2;
      if (CS_IsKeyHold(KEY_R) && CS_IsKeyHold(KEY_L)) return dic_quit;
      if (CS_IsKeyDown(KEY_L)) return 5;
      if (CS_IsKeyDown(KEY_R)) return 6;
      if (CS_IsKeyDown(KEY_START)) return dic_cap;
      if (CS_IsKeyDown(KEY_SELECT)) return dic_sel;
   }
}
void dic_area(u8 id)
{
   u8 xp=(id+1)%2;
   u8 yp=(id-1)/2;
   xp=6*18*xp;
   r_box(dic_bord_xp+xp+4+16,dic_bord_yp+(yp+1)*14+2,dic_bord_xp+xp+4+16+5*18,dic_bord_yp+(yp+1)*14+16,0,5);
}
void dic_capLock()
{
   u8 xp=dic_bord_xp,yp=dic_bord_yp;
   if (rb_yp) Q_box(xp+9,yp+6,xp+17,yp+11,RGB(30,30,0));
   else Q_box(xp+9,yp+6,xp+17,yp+11,dic_bord_c);
}
u32 dic_list(dichead *head,u32 loc,u32 n,u8 mod)
{
   int i;
   u32 get;
   u32 sel=0,old=0;
   u32 np=n;
   u8 done=1;
   char word[50];
   n_box(5,25,235,90,RGB(30,30,30),dic_bg);
   Q_box(6,26,234,89,RGB(30,30,30));
   for (i=0;i<5;i++)
   {
      if (n+i>=head->count) break;
      dic_record(word,head,loc,n+i);
      cwrite(8,27+12*i,dic_fc,word);
   }
   if (mod==0) return 0;
   while (1)
   {
      CS_ReadKey();
      if (CS_IsKeyDown(KEY_A)) return np+sel+1;
      if (CS_IsKeyDown(KEY_B)) return 0;
      if (CS_IsKeyDown(KEY_UP))
      {
         if (sel)
         {
            sel--;
            done=1;
         }
         else if (np)
         {
            np--;
            done=2;
            old=sel;
         }
      }
      else if (CS_IsKeyDown(KEY_DOWN))
      {
         if (sel<4) {sel++;done=1;}
         else if(np+5<head->count)
         {
            np++;
            done=2;
            old=sel;
         }
      }
      else if (CS_IsKeyDown(KEY_LEFT))
      {
         if (np>4) np=np-5;
         else np=0;
         done=2;
         old=sel;
      }
      else if(CS_IsKeyDown(KEY_RIGHT))
      {
         if (np+5<head->count)
         {
            np+=5;
            done=2;
            if (np+sel>=head->count) sel=head->count-1;
            old=sel;
         }
      }
      if (done==2)
      {
         Q_box(6,26,234,89,RGB(30,30,30));
         for (i=0;i<5;i++)
         {
            if (np+i>=head->count) break;
            dic_record(word,head,loc,np+i);
            cwrite(8,27+12*i,dic_fc,word);
         }
      }
      if (done)
      {
         r_box(8,27+sel*12,230,27+11+sel*12,0,5);
         if (old!=sel) r_box(8,27+old*12,230,27+11+old*12,0,5);
         old=sel;
         done=0;
      }
   }
}

u8 dic_input(char *str,dichead *head,u32 loc)
{
   u8 done=0;
   u8 sel=0;
   u8 key;
   char word[50];
   u32 get;
   u32 indx;
   Q_box(6,6,234,19,RGB(30,30,30));
   cwrite(8,7,dic_fc,str);
   dic_list(head,loc,dic_indx,0);
   while (1)
   {
   again1:
      key=dic_get_key();
      if (key==dic_cap)
      {
         rb_yp=!rb_yp;
         dic_capLock();
         goto again1;
      }
      if (key==dic_quit) return 0;
      if (key==dic_ok) return 1;
      if (key==0)//删除
      {
         if (rb_xp)
         {
            rb_xp--;
            str[rb_xp]='\0';
            done=1;
            goto next;
         }
         else goto again1;
      }
      if (key>0 && key<7)//是按键
      {
         sel=key;
         dic_area(sel);
      again:
         key=dic_get_key();
         if (key==dic_cap)
         {
            rb_yp=!rb_yp;
            dic_capLock();
            goto again;
         }
         //if (key==6) goto again;
         dic_area(sel);
         if (key==dic_quit) return 0;
         if (key==6) done=2;//选择
         else if (key>0 && key<6)
         {
            if (sel<6 || (sel==6 && key<=3))//增加
            {
               if (rb_xp<max_len)
               {
                  if (sel<6 || (sel==6 && key==1))
                  str[rb_xp]=(rb_yp?0:('a'-'A'))+ch_hi[(sel-1)*5+key-1];//
                  else str[rb_xp]=' ';
                  rb_xp++;
                  str[rb_xp]='\0';
                  done=1;
               }
            }
            else //删除
            {
               if (rb_xp)
               {
                  rb_xp--;
                  str[rb_xp]='\0';
                  done=1;
               }
            }
         }
      }
   next:
      if (done==2)
      {
         indx=dic_list(head,loc,dic_indx,1);
         if (indx)
         {
            dic_record(str,head,loc,indx-1);
            rb_xp=str_len(str);
         }
      }
      if (done)
      {
         Q_box(6,6,234,19,RGB(30,30,30));
         cwrite(8,7,dic_fc,str);
         done=0;
         get=dic_search(str,head,loc);
         if (get)
         {
            dic_list(head,loc,dic_indx,0);
         }
      }
   }
}
void dic(u32 loc)
{
   char word[50]="\0";
   char mes[500]="\0";
   u8 errors=0;
   u32 get;
   dichead head;
   rb_xp=0;
   //dic_indx=0;
   while (get_dic_mes(&head,fbase+loc)==0)//非法文件
   {
      if (errors>3) return;
      is_basic_run=1;
      title("请选择正确的英语词库:");
      loc=list_fsys(0);
      is_basic_run=0;
      errors++;
   }
   dic_indx=0;
   loc=fbase+loc;
   sys_deamon=dic_deamon;
   Q_box(0,0,239,159,dic_bg);
   n_box(5,5,235,20,RGB(10,10,0),dic_bg);
   Q_box(6,6,234,19,RGB(30,30,30));
   dic_board();
   while (1)
   {
      if (dic_input(word,&head,loc)==0) {sys_deamon=NULL;return;}
      if (word[0]=='\0') goto next;
      get=dic_search(word,&head,loc);
      if (get) get_word_mes(mes,get);
      n_box(5,25,235,155,RGB(30,30,30),dic_bg);
      Q_box(6,26,234,154,RGB(30,30,30));
      show_word_mes(mes);
      while (1)
      {
         CS_ReadKey();
         if (CS_IsKeyDown(KEY_A) || CS_IsKeyDown(KEY_DOWN) || CS_IsKeyDown(KEY_RIGHT)) break;
         else if(CS_IsKeyDown(KEY_B)||CS_IsKeyDown(KEY_LEFT) || CS_IsKeyDown(KEY_UP))
         {
            rb_xp=0;
            word[0]='\0';
            break;
         }
      }
      Q_box(5,25,235,155,dic_bg);
next: dic_board();
   }
}
void dic_select()//选择文件
{
   u32 old_set;
   u32 new_set;
   u32 loc;
   dic_set((char *)&old_set,2);//获得旧的设置
   is_basic_run=1;
   r_box(0,16,239,159,RGB(0,9,16),1);
   r_box(0,0,239,159,RGB(30,30,30),0);
   title("请选择dic英汉词库");
   loc=list_fsys(0);
   new_set=loc;
   if (old_set==new_set) return;
   else if (loc)
   {
      if (arlt("系统提示:","您真的要修改当前词库吗?\n\n[A:Yes]    [B:No]"))
      dic_set((char *)&new_set,1);
   }
}
void eng_dic()
{
   u32 loc;
   dic_set((char *)&loc,2);
   if (!loc)
   {
      arlt("系统提示:","您没有选定英汉词库或者词库尚未安装,请先设置词库。");
      return;
   }
   else dic(loc-fbase);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -