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

📄 tojava.c

📁 编程语言转换辅助工具,C语法转成JAVA语法
💻 C
字号:
#include <string.h>
#include <stdio.h>
#include <mem.h>

void main(int argc, char *argv[]){
  FILE *infp, *outfp;
  static char linebuf[512 + 1];
  static char tmpbuf[512 + 1];
  char *ptr;
  char *firstptr;
  char *secondptr;
  char *otherptr;
  int len;
  int remflag;

  if (argc < 3){
    printf("Usage:tojava cfile jfile\n");
    return;
  }

  infp = fopen(argv[1], "rb");
  if (infp == NULL)
    return;
  outfp = fopen(argv[2], "wb");
  if (outfp == NULL){
    fclose(infp);
    return;
  }

  remflag = 0;
  while (feof(infp) == 0){
    memset(linebuf, '\0', sizeof(linebuf));
    if (fgets(linebuf, sizeof(linebuf) - 1, infp) == NULL)
      break;
    if (remflag == 0){
      ptr = strstr(linebuf, "//");
      if (ptr != NULL)
	*ptr = '\0';
      ptr = strstr(linebuf, "/*");
      if (ptr != NULL){
	if (strstr(linebuf, "*/") == NULL)
	  remflag = 1;
	*ptr = '\0';
      }
    }else{
      ptr = strstr(linebuf, "*/");
      if (ptr != NULL){
	len = strlen(ptr + 2);
	memset(tmpbuf, '\0', sizeof(tmpbuf));
	if (len > 0)
	  memcpy(tmpbuf, ptr + 2, len);
	memset(linebuf, '\0', sizeof(linebuf));
	if (len > 0)
	  memcpy(linebuf, tmpbuf, len);
	remflag = 0;
      }
      if (remflag == 1)
	continue;
    }
    len = strlen(linebuf);
    if (len > 0){
      if (linebuf[len - 1] == '\n')
	linebuf[len - 1] = '\0';
      if (linebuf[len - 1] == '\r')
	linebuf[len - 1] = '\0';
    }
    if (len > 1){
      if (linebuf[len - 2] == '\n')
	linebuf[len - 2] = '\0';
      if (linebuf[len - 2] == '\r')
	linebuf[len - 2] = '\0';
    }
    len = strlen(linebuf);
    memset(tmpbuf, '\0', sizeof(tmpbuf));
    memcpy(tmpbuf, linebuf, len);
    firstptr = strtok(tmpbuf, " \t");
    secondptr = strtok(NULL, " \t");
    otherptr = strtok(NULL, "");
    if (strcmp(firstptr, "#define") == 0){
      fprintf(outfp, "private static final int ");
      if (secondptr != NULL)
	fprintf(outfp, "%s ", secondptr);
      if (otherptr != NULL)
	fprintf(outfp, "= %s", otherptr);
      fprintf(outfp, ";\r\n");
      continue;
    }
    if (strcmp(firstptr, "const") == 0){
      fprintf(outfp, "private static final ");
      if (secondptr != NULL)
	fprintf(outfp, "%s ", secondptr);
      if (otherptr != NULL)
	fprintf(outfp, "%s", otherptr);
      fprintf(outfp, "\r\n");
      continue;
    }
    if ((strcmp(firstptr, "int") == 0) || (strcmp(firstptr, "string") == 0) || (strcmp(firstptr, "image") == 0) || (strcmp(firstptr, "void") == 0)){
      fprintf(outfp, "private %s ", firstptr);
      if (secondptr != NULL)
	fprintf(outfp, "%s ", secondptr);
      if (otherptr != NULL)
	fprintf(outfp, "%s", otherptr);
      fprintf(outfp, "\r\n");
      continue;
    }
    fprintf(outfp, "%s\r\n", linebuf);
  }

  fclose(infp);
  fclose(outfp);
}

⌨️ 快捷键说明

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