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

📄 gb_unicode.c

📁 gb2312转换成unicode的c程序源代码
💻 C
字号:
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void y_error(char *str)
 {
  printf("\nError: %s\n\7", str);
  getch();
  exit(1);
 }

void save_title(FILE *fpw, char mode)
 {
  if (!mode)
   fprintf(fpw,
	"<html xmlns:o=\"urn:schemas-microsoft-com:office:office\"\n"
	"xmlns:x=\"urn:schemas-microsoft-com:office:excel\"\n"
	"xmlns=\"http://www.w3.org/TR/REC-html40\">\n"
	"<head>\n"
	"<meta http-equiv=Content-Type content=\"text/html; charset=GB2312\">\n"
	"<meta name=ProgId content=Excel.Sheet>\n"
	"<meta name=Generator content=\"Microsoft Excel 9\">\n"
	"<link rel=File-List href=\"./h.files/filelist.xml\">\n"
	"<link rel=Edit-Time-Data href=\"./h.files/editdata.mso\">\n"
	"<link rel=OLE-Object-Data href=\"./h.files/oledata.mso\">\n"
	"<!--[if gte mso 9]><xml>\n"
	" <o:DocumentProperties>\n"
	"  <o:Author>于一丁</o:Author>\n"
	"  <o:LastAuthor>于一丁</o:LastAuthor>\n"
	"  <o:Created>2006-10-05T02:37:02Z</o:Created>\n"
	"  <o:LastSaved>2006-10-05T03:32:01Z</o:LastSaved>\n"
	"  <o:Version>9.3821</o:Version>\n"
	" </o:DocumentProperties>\n"
	" <o:OfficeDocumentSettings>\n"
	"  <o:DownloadComponents/>\n"
	"  <o:LocationOfComponents HRef=\"file:///G:/msowc.cab\"/>\n"
	" </o:OfficeDocumentSettings>\n"
	"</xml><![endif]-->\n"
	"<style>\n"
	"<!--table\n"
	"	{mso-displayed-decimal-separator:\"\.\";\n"
	"	mso-displayed-thousand-separator:\"\,\";}\n"
	"@page\n"
	"	{margin:1.0in .75in 1.0in .75in;\n"
	"	mso-header-margin:.5in;\n"
	"	mso-footer-margin:.5in;}\n"
	"tr\n"
	"	{mso-height-source:auto;\n"
	"	mso-ruby-visibility:none;}\n"
	"col\n"
	"	{mso-width-source:auto;\n"
	"	mso-ruby-visibility:none;}\n"
	"br\n"
	"	{mso-data-placement:same-cell;}\n"
	".style0\n"
	"	{mso-number-format:General;\n"
	"	text-align:general;\n"
	"	vertical-align:bottom;\n"
	"	white-space:nowrap;\n"
	"	mso-rotate:0;\n"
	"	mso-background-source:auto;\n"
	"	mso-pattern:auto;\n"
	"	color:windowtext;\n"
	"	font-size:12.0pt;\n"
	"	font-weight:400;\n"
	"	font-style:normal;\n"
	"	text-decoration:none;\n"
	"	font-family:宋体;\n"
	"	mso-generic-font-family:auto;\n"
	"	mso-font-charset:134;\n"
	"	border:none;\n"
	"	mso-protection:locked visible;\n"
	"	mso-style-name:常规;\n"
	"	mso-style-id:0;}\n"
	"td\n"
	"	{mso-style-parent:style0;\n"
	"	padding-top:1px;\n"
	"	padding-right:1px;\n"
	"	padding-left:1px;\n"
	"	mso-ignore:padding;\n"
	"	color:windowtext;\n"
	"	font-size:12.0pt;\n"
	"	font-weight:400;\n"
	"	font-style:normal;\n"
	"	text-decoration:none;\n"
	"	font-family:宋体;\n"
	"	mso-generic-font-family:auto;\n"
	"	mso-font-charset:134;\n"
	"	mso-number-format:General;\n"
	"	text-align:general;\n"
	"	vertical-align:bottom;\n"
	"	border:none;\n"
	"	mso-background-source:auto;\n"
	"	mso-pattern:auto;\n"
	"	mso-protection:locked visible;\n"
	"	white-space:nowrap;\n"
	"	mso-rotate:0;}\n"
	".xl24\n"
	"	{mso-style-parent:style0;\n"
	"	font-size:10.5pt;\n"
	"	mso-number-format:\"\@\";\n"
	"	vertical-align:middle;\n"
	"	white-space:normal;}\n"
	".xl25\n"
	"	{mso-style-parent:style0;\n"
	"	font-size:10.5pt;\n"
	"	font-family:\"Times New Roman\", serif;\n"
	"	mso-font-charset:0;\n"
	"	mso-number-format:\"\@\";\n"
	"	vertical-align:middle;\n"
	"	white-space:normal;}\n"
	"ruby\n"
	"	{ruby-align:left;}\n"
	"rt\n"
	"	{color:windowtext;\n"
	"	font-size:9.0pt;\n"
	"	font-weight:400;\n"
	"	font-style:normal;\n"
	"	text-decoration:none;\n"
	"	font-family:宋体;\n"
	"	mso-generic-font-family:auto;\n"
	"	mso-font-charset:134;\n"
	"	mso-char-type:none;\n"
	"	display:none;}\n"
	"-->\n"
	"</style>\n"
	"<!--[if gte mso 9]><xml>\n"
	" <x:ExcelWorkbook>\n"
	"  <x:ExcelWorksheets>\n"
	"   <x:ExcelWorksheet>\n"
	"    <x:Name>Sheet1</x:Name>\n"
	"    <x:WorksheetOptions>\n"
	"     <x:DefaultRowHeight>270</x:DefaultRowHeight>\n"
	"     <x:Selected/>\n"
	"     <x:FreezePanes/>\n"
	"     <x:FrozenNoSplit/>\n"
	"     <x:SplitHorizontal>1</x:SplitHorizontal>\n"
	"     <x:TopRowBottomPane>1</x:TopRowBottomPane>\n"
	"     <x:SplitVertical>4</x:SplitVertical>\n"
	"     <x:LeftColumnRightPane>4</x:LeftColumnRightPane>\n"
	"     <x:ActivePane>0</x:ActivePane>\n"
	"     <x:Panes>\n"
	"      <x:Pane>\n"
	"       <x:Number>3</x:Number>\n"
	"      </x:Pane>\n"
	"      <x:Pane>\n"
	"       <x:Number>1</x:Number>\n"
	"       <x:ActiveCol>3</x:ActiveCol>\n"
	"      </x:Pane>\n"
	"      <x:Pane>\n"
	"       <x:Number>2</x:Number>\n"
	"      </x:Pane>\n"
	"      <x:Pane>\n"
	"       <x:Number>0</x:Number>\n"
	"       <x:ActiveRow>3</x:ActiveRow>\n"
	"      </x:Pane>\n"
	"     </x:Panes>\n"
	"     <x:ProtectContents>False</x:ProtectContents>\n"
	"     <x:ProtectObjects>False</x:ProtectObjects>\n"
	"     <x:ProtectScenarios>False</x:ProtectScenarios>\n"
	"    </x:WorksheetOptions>\n"
	"   </x:ExcelWorksheet>\n"
	"   <x:ExcelWorksheet>\n"
	"    <x:Name>Sheet2</x:Name>\n"
	"    <x:WorksheetOptions>\n"
	"     <x:DefaultRowHeight>285</x:DefaultRowHeight>\n"
	"     <x:ProtectContents>False</x:ProtectContents>\n"
	"     <x:ProtectObjects>False</x:ProtectObjects>\n"
	"     <x:ProtectScenarios>False</x:ProtectScenarios>\n"
	"    </x:WorksheetOptions>\n"
	"   </x:ExcelWorksheet>\n"
	"   <x:ExcelWorksheet>\n"
	"    <x:Name>Sheet3</x:Name>\n"
	"    <x:WorksheetOptions>\n"
	"     <x:DefaultRowHeight>285</x:DefaultRowHeight>\n"
	"     <x:ProtectContents>False</x:ProtectContents>\n"
	"     <x:ProtectObjects>False</x:ProtectObjects>\n"
	"     <x:ProtectScenarios>False</x:ProtectScenarios>\n"
	"    </x:WorksheetOptions>\n"
	"   </x:ExcelWorksheet>\n"
	"  </x:ExcelWorksheets>\n"
	"  <x:WindowHeight>12720</x:WindowHeight>\n"
	"  <x:WindowWidth>18195</x:WindowWidth>\n"
	"  <x:WindowTopX>480</x:WindowTopX>\n"
	"  <x:WindowTopY>30</x:WindowTopY>\n"
	"  <x:ProtectStructure>False</x:ProtectStructure>\n"
	"  <x:ProtectWindows>False</x:ProtectWindows>\n"
	" </x:ExcelWorkbook>\n"
	"</xml><![endif]-->\n"
	"</head>\n"
	"\n"
	"<body link=blue vlink=purple class=xl24>\n"
	"\n"
	"<table x:str border=0 cellpadding=0 cellspacing=0 width=973 style='border-collapse:\n"
	" collapse;table-layout:fixed;width:731pt'>\n"
	" <col class=xl24 width=33 style='mso-width-source:userset;mso-width-alt:1056;\n"
	" width:25pt'>\n"
	" <col class=xl24 width=72 style='width:54pt'>\n"
	" <col class=xl24 width=38 style='mso-width-source:userset;mso-width-alt:1216;\n"
	" width:29pt'>\n"
	" <col class=xl25 width=150 style='mso-width-source:userset;mso-width-alt:4800;\n"
	" width:113pt'>\n"
	" <col class=xl24 width=680 style='mso-width-source:userset;mso-width-alt:21760;\n"
	" width:510pt'>\n"
	"\n"
	"\n"
	"\n"
	" <tr height=17 style='height:12.75pt'>\n"
	"  <td height=17 class=xl24 width=33 style='height:12.75pt;width:25pt'>序号</td>\n"
	"  <td class=xl24 width=72 style='width:54pt'>日期</td>\n"
	"  <td class=xl24 width=38 style='width:29pt'>时间</td>\n"
	"  <td class=xl24 width=150 style='width:113pt'>来源</td>\n"
	"  <td class=xl24 width=680 style='width:510pt'>内容</td>\n"
	" </tr>\n"
	"\n"
	"\n"
	"\n"
	"\n"
	"\n");
  else
   fprintf(fpw,
	"\n"
	"\n"
	"\n"
	"\n"
	"\n"
	"<![if supportMisalignedColumns]>\n"
	" <tr height=0 style='display:none'>\n"
	"  <td width=33 style='width:25pt'></td>\n"
	"  <td width=72 style='width:54pt'></td>\n"
	"  <td width=38 style='width:29pt'></td>\n"
	"  <td width=150 style='width:113pt'></td>\n"
	"  <td width=680 style='width:510pt'></td>\n"
	" </tr>\n"
	" <![endif]>\n"
	"</table>\n"
	"\n"
	"</body>\n"
	"\n"
	"</html>\n");
 }

long buf_value(unsigned char *str, char l, char mode)
// 由字符串返回实际数值, mode: 0-从后往前(一般), 1-从前往后
 {
  long v;
  char i;

  for (v = 0, i = 0;i < l;i ++ )
   {
    v <<= 8;
    v += *(str + (mode ? i : l - 1 - i));
   }
  return(v);
 }

void value_buf(unsigned char *str, char l, unsigned long v)
// 将数值写入字符串
 {
  char i;

  for (i = 0;i < l;i ++ , v >>= 8)
   *(str + i) = v;
 }

FILE *uni_fp = NULL;

void unicode_gb(char *str, int *len)
 {
  unsigned int i, code;

  for (i = 0;i < *len;i += 2)
   {
    code = buf_value(str + i, 2, 0);
    if (code < 0x100 || code == 0xF8F5)	// 单字节, 0xFF
     {
      if (code == 0xF8F5)
       *(str + i) = 0xFF;
      movmem(str + i + 2, str + i + 1, (*len) - (i + 2) + 1);
      i -- ;
      (*len) -- ;
     }
    else	// can't be here in v3.c
     {
      fseek(uni_fp, 0x20 + (long)code * 2, 0);
      fread(&code, sizeof(int), 1, uni_fp);
      *(str + i) = code / 0x100;
      *(str + i + 1) = code % 0x100;
     }
   }
  *(str + *len) = 0;
 }

void gb_unicode(unsigned char *str, int *len)
 {
  unsigned int i, code;

  for (i = 0;i < *len;i += 2)
   if (*(str + i) >= 0x81 && *(str + i) <= 0xFE &&
       *(str + i + 1) >= 0x40 && *(str + i + 1) <= 0xFE)
		       // 双字节
    {
     code = ((*(str + i)) - 0x81) * 191 + ((*(str + i + 1)) - 0x40);
     fseek(uni_fp, 0x20020 + (long)code * 2, 0);
     fread(&code, sizeof(int), 1, uni_fp);
     value_buf(str + i, 2, code);
    }
   else
    {
     memmove(str + i + 2, str + i + 1, (*len) - (i + 1) + 1);
     *(str + i + 1) = 0;
     (*len) ++ ;
    }
 }

int check_str(char *str, char *fmt, int len)
 {
  int l = fmt[0];

  if (len < l || memicmp(&str[0], &fmt[1], l))
   y_error("format");
  return(l);
 }

int find_str(char *str, char *fmt, int len)
 {
  int i, l = fmt[0];

  for (i = 0;i < len - l;i ++ )
   if (!memicmp(&str[i], &fmt[1], l))
    return(i);
  y_error("format");
  return(-1);
 }

void done_code(char *str, int len)
 {
  int i;

  unicode_gb(str, &len);
  for (i = 0;i < len;i ++ )
   if (!(*(str + i) >= '0' && *(str + i) <= '9' || *(str + i) == '+' && !i))
    y_error("Code Format");
 }

void done_name(char *code, char *name)
 {
  static FILE *fp = NULL;
  char str[100], ok, len = strlen(code);

  strcpy(name, "");
  if (fp == NULL)
   {
    fp = fopen("V3.Txt", "r");
    if (fp == NULL)
     y_error("Can't open file V3.Txt");
   }

  rewind(fp);
  for ( ;; )
   {
    fscanf(fp, "%s", &str[0]);
    if (!memicmp(str, "★", 2))
     continue;
    if (!strcmp(str, "[End]"))
     break;
    ok = !strcmp(str, code + len - strlen(str));
    fscanf(fp, "%s", &str[0]);
    if (ok)
     {
      strcpy(name, str);
      break;
     }
    if (feof(fp))
     break;
   }
 }

void done_time(char *str, char *ti_str, int len)
 {
  int i, v[5] = { 0, 0, 0, 0, 0, };
  char mode;

  unicode_gb(str, &len);	// 2004-1-1 1:55
  for (i = mode = 0;i < len;i ++ )
   {
    if (*(str + i) >= '0' && *(str + i) <= '9')
     {
      v[mode] *= 10;
      v[mode] += (*(str + i)) - '0';
      continue;
     }
    if (*(str + i) == '-' && mode < 2 ||
	*(str + i) == ' ' && mode == 2 ||
	*(str + i) == ':' && mode == 3)
     {
      mode ++ ;
      continue;
     }
    y_error("Time Format");
   }

  sprintf(str, "%d.%02d.%02d", v[0], v[1], v[2]);
  sprintf(ti_str, "%02d:%02d", v[3], v[4]);
 }

void done_content(FILE *fpw, char *str, int len)
// en: <font class=font6>.</font>
// ch: <font class=font7>&#19886;</font>
 {
  unsigned int i, code;
//  char font = -1;	// font: 0-en, 1-ch

  for (i = 0;i < len;i += 2)
   {
    code = buf_value(str + i, 2, 0);
    if (code == 0x0D || code == 0x0A)
     {
      fprintf(fpw, "<br>");
      code = buf_value(str + i + 2, 2, 0);
      if (code == 0x0D || code == 0x0A)
       i += 2;
      continue;
     }                              /*
    if (code < 0x100 || code == 0xF8F5)	// 单字节, 0xFF
     {
      if (font != 0)
       {
	if (font != -1)
	 fprintf(fpw, "</font>");
	font = 0;
	fprintf(fpw, "<font class=font6>");
       }
     }
    else
     {
      if (font != 1)
       {
	if (font != -1)
	 fprintf(fpw, "</font>");
	font = 1;
	fprintf(fpw, "<font class=font7>");
       }
     }                                */
    fprintf(fpw, "&#%u;", code);
   }
//  fprintf(fpw, "</font>");
 }

void done(FILE *fp, FILE *fpw)
 {
  unsigned char str[9999];
  unsigned char *fmt[4] =
   {
    "\x0A\x28\x75\x37\x62\x0D\x54\x3A\x00\x20\x00",
    "\x04\x0D\x00\x0A\x00",
    "\x08\xE5\x65\x1F\x67\x3A\x00\x20\x00",
    "\x08\x0D\x00\x0A\x00\x0D\x00\x0A\x00",
   };
  char code[100], code_l, da[100], da_l, ti[100], name[100];

  int len, i, p1, p2;

  len = fread(&str[0], sizeof(char), 9999, fp);
  if (memicmp(&str[0], "\xFF\xFE", 2))	// gb
   {
    gb_unicode(&str[0], &len);
    memmove(str + 2, str, len - 1);
    len += 4;
    memset(str + len - 2, 0, 2);
   }

  if (!memicmp(&str[len - 2], "\x00\x00", 2))
   len -= 2;
//   y_error("End Format");

  p1 = 2 + check_str(&str[2], fmt[0], len - 2);
  p2 = find_str(&str[p1], fmt[1], len - p1);
  memcpy(code, &str[p1], p2);
  code_l = p2;
  p1 = p1 + p2 + 4;
  p1 = p1 + check_str(&str[p1], fmt[2], len - p1);
  p2 = find_str(&str[p1], fmt[3], len - p1);
  memcpy(da, &str[p1], p2);
  da_l = p2;
  p1 = p1 + p2 + 8;
  p2 = len - p1;

  done_code(&code[0], code_l);
  done_name(&code[0], &name[0]);
  done_time(&da[0], &ti[0], da_l);
  fprintf(fpw,
	"  <td class=xl25 width=72 style='width:54pt'>%s</td>\n"
	"  <td class=xl25 width=38 style='width:29pt'>%s</td>\n"
	"  <td class=xl25 width=150 style='width:113pt'>%s%s</td>\n",
	da, ti, code, name);
  fprintf(fpw,
	"  <td class=xl25 width=680 style='width:510pt'>");
  done_content(fpw, &str[p1], p2);
  fprintf(fpw,
	"</td>\n");
 }

void done_s(FILE *fpw, char mode)
 {
  char fname[80];
  int i;
  FILE *fp;

  for (i = 1;i < 1000;i ++ )
   {
    sprintf(fname, mode ? "f%03d.txt" : "s%03d.txt", i);
    fp = fopen(fname, "rb");
    if (fp == NULL)
     break;

    printf("%d\r", i);
    fprintf(fpw,
	" <tr height=18 style='height:13.5pt'>\n"
	"  <td height=18 class=xl25 width=33 style='height:13.5pt;width:25pt'>%d</td>\n",
	i);
    done(fp, fpw);
    fprintf(fpw,
	" </tr>\n");

    fclose(fp);
   }
  printf("\n");
 }

void main(void)
 {
  FILE *fpw;

  printf("\n\nV3 Program. 2006.10.04\n\n");
  uni_fp = fopen("C:\\Com\\Disk.Uni", "rb");
  if (uni_fp == NULL)
   y_error("Can't open Disk.Uni");

  fpw = fopen("V3.Htm", "w");
  if (fpw == NULL)
   y_error("Can't open V3.Htm");
  save_title(fpw, 0);

  printf("Changing ...\n");
  done_s(fpw, 0);
  fprintf(fpw,
	"\n"
	"  <td class=xl25 width=680 style='width:510pt'>"
	"</td>\n"
	"\n");
  done_s(fpw, 1);

  save_title(fpw, 1);

  printf("End!\n");
  fclose(fpw);
 }

⌨️ 快捷键说明

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