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

📄 cmdline.c

📁 JPEG-MPEG編解碼技術書集的代碼
💻 C
📖 第 1 页 / 共 2 页
字号:
  char *token;
  int maxSwitchLen,tmp;
  CmdLinePara *paraPtr;
  CmdLineSwitch *switchPtr;

  /* set minusFlag if switches are preceded by '-' */
  minusFlag = (progName!=NULL || paraList!=NULL);
  minusChar = (minusFlag) ? "-" : "";
    
  fprintf(outStream,"\n");

  maxSwitchLen = 0;
  switchPtr = switchList;
  while (switchPtr != NULL && switchPtr->switchName != NULL) {
    if ((tmp=strlen(switchPtr->switchName)+
	 ((switchPtr->argument!=NULL && switchPtr->format!=NULL)?4:0))
	> maxSwitchLen)
      maxSwitchLen = tmp;
    switchPtr++;
  }
  
  if (progName != NULL)
    fprintf(outStream,"usage: %s",progName);
  else
    fprintf(outStream,"token list format:");
  fprintf(outStream," %sswitches",minusChar);
  paraPtr = paraList;
  while (paraPtr != NULL && paraPtr->argument != NULL) {
    fprintf(outStream," %s",paraPtr->help);
    if (paraPtr->format==NULL)
      /* remaining args are not processed !!! */
      break;	/* while (paraPtr ...) */
    paraPtr++;
  }
  fprintf(outStream,"\n");
  
  /* display help for every switch  */
  switchPtr=switchList;
  while (switchPtr != NULL && switchPtr->switchName != NULL) {
    fprintf(outStream,"%9s %s%s%-*s   ",
	    (switchPtr==switchList)?"switches:":"",
	    minusChar,switchPtr->switchName,
	    maxSwitchLen-strlen(switchPtr->switchName),
	    (switchPtr->format==NULL)?"":" <x>");
    if (strchr(switchPtr->help,'\n')==NULL)
      fprintf(outStream,"%s",switchPtr->help);
    else {
      /* multiple lines separated by '\n' */
      strncpy(help,switchPtr->help,MAX_HELP_SIZE-1);
      help[MAX_HELP_SIZE-1] = '\0';
      token = strtok(help,"\n");
      fprintf(outStream,"%s",token);
      while (token!=NULL) {
	token = strtok(NULL,"\n");
	if (token!=NULL) {
	  fprintf(outStream,"\n");
	  if (strcmp(token,"\b")==0)
	    fprintf(outStream,"%9s %*s  ",
		    "",maxSwitchLen+minusFlag,"");
	  else
	    fprintf(outStream,"%9s %*s   %s",
		    "",maxSwitchLen+minusFlag,"",token);
	}
      }
    }
    if (switchPtr->argument!=NULL && switchPtr->defaultValue!=NULL)
      fprintf(outStream," (dflt: %s)",switchPtr->defaultValue);
    fprintf(outStream,"\n");
    switchPtr++;
  }
  fprintf(outStream,"\n");
}


/* CmdLineParseString() */
/* Parse a copy of string into tokens separated by sepaChar. */
/* Resulting token list can be evaluated by CmdLineEval(). */

char **CmdLineParseString (
  char *string,			/* in: string to be parsed */
				/*     NOTE: string is not modified */
  char *sepaChar,		/* in: token separator characters */
  int *count)			/* out: number of tokens generated by parser */
				/*      (corresponds to argc) */
				/* returns: */
				/*  list of tokens generated by parser */
				/*  (corresponds to argv[]) */
{
  char *tmpTokenList[MAX_TOKEN_NUM];
  char *firstChar;
  char *stringBuf;
  char **tokenList;
  int  i,size;

  if (CLdebugLevel >= 1)
    printf("CmdLineParseString: sepa=\"%s\"\n",sepaChar);

  if (string == NULL)
    stringBuf = NULL;
  else {
    /* find first non-sepaChar */
    firstChar = string;
    while (*firstChar!='\0' && strchr(sepaChar,*firstChar)!=NULL)
      firstChar++;
    size = strlen(firstChar);
    
    /* copy string content */ 
    if ((stringBuf = (char*)malloc((size+1)*sizeof(char))) == NULL)
      CommonExit(1,"CmdLineParseString: memory allocation error (stringBuf)");
    strcpy(stringBuf,firstChar);
  }

  /* parse string */
  i = 0;
  tmpTokenList[i] = (stringBuf==NULL)?(char*)NULL:strtok(stringBuf,sepaChar);
  if (tmpTokenList[0]!=NULL && tmpTokenList[0]!=stringBuf)
    CommonExit(1,"CmdLineParseString: internal error");
  while (tmpTokenList[i]!=NULL) {
    if (CLdebugLevel >= 2)
      printf("%4d: \"%s\"\n",i,tmpTokenList[i]);
    if (++i >= MAX_TOKEN_NUM)
      CommonExit(1,"CmdLineParseString: too many tokens");
    tmpTokenList[i] = strtok(NULL,sepaChar);
  }
  *count = i;

  /* copy token list */
  if ((tokenList = (char**)malloc((*count+1)*sizeof(char*))) == NULL)
    CommonExit(1,"CmdLineParseString: memory allocation error (tokenList)");
  for (i=0; i<*count; i++)
    tokenList[i] = tmpTokenList[i];
  tokenList[*count] = NULL;
  
  if (CLdebugLevel >= 1)
    printf("CmdLineParseString: tokenCount=%d\n",*count);

  return tokenList;
}


/* CmdLineParseFile() */
/* Parse init file into tokens separated by sepaChar. */
/* Comments preceded by a commentSepaChar are ingnored. */
/* Resulting token list can be evaluated by CmdLineEval(). */

char **CmdLineParseFile (
  char *fileName,		/* in: file name of init file */
  char *sepaChar,		/* in: token separator characters */
  char *commentSepaChar,	/* in: comment separator characters */
  int *count)			/* out: number of tokens generated by parser */
				/*      (corresponds to argc) */
				/* returns: */
				/*  list of tokens generated by parser */
				/*  (corresponds to argv[]) */
				/*  or NULL if file error */
{
  FILE *initFile;
  char lineBuf[MAX_LINE_SIZE];
  char *comment;
  char tmpFileBuf[MAX_FILE_SIZE];
  char *firstChar;
  char *fileBuf;
  char *tmpTokenList[MAX_TOKEN_NUM];
  char **tokenList;
  int  i,size,line;

  if (CLdebugLevel >= 1)
    printf("CmdLineParseFile: file=\"%s\"  sepa=\"%s\"  com=\"%s\"\n",
	   fileName,sepaChar,commentSepaChar);

  /* open init file */
  if ((initFile = fopen(fileName,"r")) == NULL) {
    CommonWarning("CmdLineParseFile: error opening init file %s",fileName);
    return NULL;
  }
  
  /* read init file */
  line = 0;
  size = 0;
  tmpFileBuf[0] = '\0';
  while (fgets(lineBuf,MAX_LINE_SIZE,initFile) != NULL) {
    line++;
    comment = strpbrk(lineBuf,commentSepaChar);
    if (comment != NULL)
      i = comment-lineBuf;
    else {
      i = strlen(lineBuf)-1;
      if (lineBuf[i] != '\n')
	CommonExit(1,"CmdLineParseFile: line %d too long",line);
    }
    if (size+i+1 >= MAX_FILE_SIZE)
      CommonExit(1,"CmdLineParseFile: file too long");
    strncat(tmpFileBuf+size,lineBuf,i);
    strncat(tmpFileBuf+size+i,sepaChar,1);
    size += i+1;
  }
  
  /* close init file */
  if (fclose(initFile)) {
    CommonWarning("CmdLineParseFile: error closing init file");
    return NULL;
  }
  
  if (CLdebugLevel >= 1)
    printf("CmdLineParseFile: initFileLineNum=%d\n",line);

  /* find first non-sepaChar */
  firstChar = tmpFileBuf;
  while (*firstChar!='\0' && strchr(sepaChar,*firstChar)!=NULL)
    firstChar++;
  size -= firstChar-tmpFileBuf;

  /* copy file content */ 
  if ((fileBuf = (char*)malloc((size+1)*sizeof(char))) == NULL)
    CommonExit(1,"CmdLineParseFile: memory allocation error (fileBuf)");
  strcpy(fileBuf,firstChar);

  /* parse string */
  i = 0;
  tmpTokenList[i] = strtok(fileBuf,sepaChar);
  if (tmpTokenList[0]!=NULL && tmpTokenList[0]!=fileBuf)
    CommonExit(1,"CmdLineParseFile: internal error");
  while (tmpTokenList[i]!=NULL) {
    if (CLdebugLevel >= 2)
      printf("%4d: \"%s\"\n",i,tmpTokenList[i]);
    if (++i >= MAX_TOKEN_NUM)
      CommonExit(1,"CmdLineParseFile: too many tokens");
    tmpTokenList[i] = strtok(NULL,sepaChar);
  }
  *count = i;

  /* copy token list */
  if ((tokenList = (char**)malloc((*count+1)*sizeof(char*))) == NULL)
    CommonExit(1,"CmdLineParseFile: memory allocation error (tokenList)");
  for (i=0; i<*count; i++)
    tokenList[i] = tmpTokenList[i];
  tokenList[*count] = NULL;
  
  if (CLdebugLevel >= 1)
    printf("CmdLineParseFile: tokenCount=%d\n",*count);

  return tokenList;
}


/* CmdLineParseFree() */
/* Free memory allocated by CmdLineParseString() or CmdLineParseFile(). */

void CmdLineParseFree (
  char **tokenList)		/* in: token list returned by */
				/*     CmdLineParseString() or */
				/*     CmdLineParseFile() */
{
  if (tokenList != NULL) {
    if (*tokenList != NULL)
      free(*tokenList);
    free(tokenList);
  }

  if (CLdebugLevel >= 1)
    printf("CmdLineParseFree: %s\n",
	   (tokenList) ? "free tokenList" : "no tokenList");
}


/* ComposeFileName() */
/* Compose filename using default path and extension if required. */
/* Handles Unix & DOS paths. "-" is passed through directly. */

int ComposeFileName (
  char *inName,			/* in: input filename */
  int forceDefault,		/* in: 0=keep input path and/or extension if */
				/*       available, otherwise use default(s) */
				/*     1=force usage of default */
				/*       path and extension */
  char *defaultPath,		/* in: default path */
				/*     or NULL */
  char *defaultExt,		/* in: default extension */
				/*     or NULL */
  char *fileName,		/* out: composed filename */
  unsigned int fileNameMaxLen)	/* in: fileName max length */
				/* returns: */
				/*  0=OK  1=result too long */
{
  char *name,*dot,*tmp;
  char pathChar;

  if (CLdebugLevel >= 1)
    printf("ComposeFileName: in=\"%s\"  forceDef=%d  path=\"%s\"  ext=\"%s\""
	   "  len=%d\n",
	   inName,forceDefault,
	   (defaultPath!=NULL)?defaultPath:"(NULL)",
	   (defaultExt!=NULL)?defaultExt:"(NULL)",
	   fileNameMaxLen);

  if (strcmp(inName,"-")==0) {
    if (fileNameMaxLen<2)
      return 1;
    strcpy(fileName,inName);
    return 0;
  }

  /* compose path */
  name = StripPath(inName);
  if (name==inName || forceDefault) {
    /* use default path */
    if (defaultPath==NULL || *defaultPath=='\0')
      *fileName = '\0';
    else
      if (strlen(defaultPath)+1 >= fileNameMaxLen)
	return 1;
      else {
	strcpy(fileName,defaultPath);
	tmp = fileName+strlen(fileName)-1;
	if (strchr(fileName,'/')!=NULL || strchr(inName,'/')!=NULL)
	  pathChar = '/';
	else
	  if (strchr(fileName,'\\')!=NULL || strchr(inName,'\\')!=NULL)
	    pathChar = '\\';
	  else
	    pathChar = '/';
	if (*tmp!=pathChar) {
	  /* append pathChar to default path */
	  *(++tmp) = pathChar;
	  *(++tmp) = '\0';
	}
      }
    if (strlen(fileName)+strlen(name) >= fileNameMaxLen)
      return 1;
    else
      strcat(fileName,name);
  }
  else {
    /* use input path */
    if (strlen(inName) >= fileNameMaxLen)
      return 1;
    else
      strcpy(fileName,inName);
  }

  /* compose extension */
  dot = strchr(StripPath(fileName),'.');
  if (dot!=NULL && forceDefault) {
    /* remove input extension */
    *dot = '\0';
    dot = NULL;
  }
  if (dot==NULL && defaultExt!=NULL && *defaultExt!='\0') {
    /* use default extension */
    if (strlen(fileName)+strlen(defaultExt)+1 >= fileNameMaxLen)
      return 1;
    else {
      if (strchr(defaultExt,'.') == NULL)
	/* insert '.' before extension */
	strcat(fileName,".");
      strcat(fileName,defaultExt);
    }
  }
  
  if (CLdebugLevel >= 1)
    printf("ComposeFileName: fileName=\"%s\"\n",fileName);

  return 0;
}


/* end of cmdline.c */

⌨️ 快捷键说明

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