示例#1
0
文件: strtool.c 项目: fnawothnig/code
int main(int argc, char *argv[]) {
	int i = 0;
	char *cmd = argv[++i];
	char *str;

	if (argc < 2) {
		fprintf(stderr, "Missing function\n");
		return 2;
	}
	else if (streq(cmd, "next")) {
		if (argc < 3)
			return 2;
		str = argv[++i];
		return next_item(str, 0);
	}
	else if (streq(cmd, "nextw")) {
		if (argc < 3)
			return 2;
		str = argv[++i];
		return next_item(str, 1);
	}
	else if (streq(cmd, "prev")) {
		if (argc != 3)
			return 2;
		str = argv[++i];
		return prev_item(str, 0);
	}
	else if (streq(cmd, "prevw")) {
		if (argc != 3)
			return 2;
		str = argv[++i];
		return prev_item(str, 1);
	}
	else if (streq(cmd, "rstrip")) {
		if (argc != 3)
			return 2;
		str = argv[++i];
		return strip_tail(str);
	}
	else {
		fprintf(stderr, "Unknown function '%s'\n", cmd);
		return 2;
	}
}
示例#2
0
g_error sub_configfile_parse(const char *filename, struct cfg_section **section) {
  FILE *f;
  char line[LINESIZE];
  char *p,*q;

  f = fopen(filename,"r");
  if (!f)
    return mkerror(PG_ERRT_IO,37);      /* Can't open config file */

  while (fgets(line,LINESIZE,f)) {
    p = line;

    /* Chomp up all the leading whitespace */
    p = strip_head(p);
    
    /* Skip blank lines and comments */
    if ((!*p) || *p=='#')
      continue;

    /* Process section lines */
    if (*p=='[') {
      /* Skip the open bracket */
      p++;
      /* Stick a null in place of the close bracket */
      q = strchr(p,']');
      if (!q)
	return mkerror(PG_ERRT_BADPARAM,38); /* Missing ']' */
      *q = 0;
      *section = configfile_makesection(p);
      continue;
    }

    if (!*section)
      return mkerror(PG_ERRT_BADPARAM,39);   /* Undefined section */

    /* Want to source another file ? */
    if (p[0]=='.' && (p[1]==' '||p[1]=='\t')) {
      g_error err;
      q = p+2;
      q = strip_head(q);
      strip_tail(q);
      err = sub_configfile_parse(q, section);
      if (iserror(err))
	return err;
      else continue;
    }

    /* If we got this far, it's a key/value line to a defined section */

    /* Get a pointer to the key in p and a pointer to the value in q */
    q = strchr(p,'=');
    if (!q) {
      /* Missing "=", assume the value is boolean and it's being defined as 1 */
      q = "1";
    }
    else {
      *q = 0;
      q++;
    }

    /* Cut leading whitespace from q */
    q = strip_head(q);

    /* Chop off trailing whitespace from p and q */
    strip_tail(p);
    strip_tail(q);

    /* Set the parameter! */
    configfile_set(*section,p,q);
  }

  fclose(f);
  return success;
}