int prep_line(char *buffer, char **token, int maxtoken, char comment, char *delimiters) { int i; /* Remove everything after comment delimiter */ if (comment != (char) 0) { i = 0; while (i < strlen(buffer) && buffer[i++] != comment); buffer[--i] = 0; } /* Remove trailing blanks and newline */ i = strlen(buffer) - 1; while (i >= 0 && (buffer[i] == ' ' || buffer[i] == '\n')) --i; buffer[++i] = 0; /* Parse the line into individual tokens and return number of tokens */ return utilParse(buffer, token, delimiters, maxtoken, 0); }
int util_parse( char *input, char **argv, char *delimiters, int max_tokens, char quote) { return utilParse(input, argv, delimiters, max_tokens, quote); }
static BOOL OpenMySQL(DBIO *db) { #define NUMTOKEN 5 /* host:user:passwd:dbname:port */ char *token[NUMTOKEN]; int ntoken; char copy[MAXPATHLEN+1]; MYSQL *unused; static char *fid = "dbioOpen:OpenMySQL"; strlcpy(copy, db->dbid, MAXPATHLEN+1); if ((ntoken = utilParse(copy, token, ":", NUMTOKEN, 0)) != NUMTOKEN) { logioMsg(db->lp, LOG_DEBUG, "%s: dbid parse error (got %d tokens, expected %d)", fid, ntoken, NUMTOKEN); logioMsg(db->lp, LOG_DEBUG, "%s: dbid = '%s'", fid, db->dbid); errno = EINVAL; return FALSE; } db->dbname = db->host = db->user = db->passwd = NULL; if (mysql_init(&db->mysql) == NULL) { logioMsg(db->lp, LOG_ERR, "%s: mysql_init: %s", fid, strerror(errno)); free(db); return FALSE; } if (strcasecmp(token[0], "NULL") == 0) { db->host = NULL; } else if ((db->host = strdup(token[0])) == NULL) { logioMsg(db->lp, LOG_ERR, "%s: strdup: %s", fid, strerror(errno)); dbioClose(db); return FALSE; } if (strcasecmp(token[1], "NULL") == 0) { db->user = NULL; } else if ((db->user = strdup(token[1])) == NULL) { logioMsg(db->lp, LOG_ERR, "%s: strdup: %s", fid, strerror(errno)); dbioClose(db); return FALSE; } if (strcasecmp(token[2], "NULL") == 0) { db->passwd = NULL; } else if ((db->passwd = strdup(token[2])) == NULL) { logioMsg(db->lp, LOG_ERR, "%s: strdup: %s", fid, strerror(errno)); dbioClose(db); return FALSE; } if (strcasecmp(token[3], "NULL") == 0) { db->dbname = NULL; } else if ((db->dbname = strdup(token[3])) == NULL) { logioMsg(db->lp, LOG_ERR, "%s: strdup: %s", fid, strerror(errno)); dbioClose(db); return FALSE; } db->port = (unsigned int) atoi(token[4]); if ( (unused=mysql_real_connect( &db->mysql, db->host, db->user, db->passwd, db->dbname, db->port, NULL, 0 ))!=NULL) return TRUE; logioMsg(db->lp, LOG_ERR, "%s: mysql_real_connect: error %d: %s", fid, mysql_errno(&db->mysql), mysql_error(&db->mysql)); dbioClose(db); return FALSE; }
Q330_CFG *q330ReadCfg(char *name) { int i, status, ntoken; FILE *fp = NULL; Q330_CFG *cfg = NULL; QDP_TYPE_C1_QCAL DefaultCalib = Q330_DEFAULT_CALIB; char *token[MAX_TOKEN]; char input[MAXLINELEN]; LNKLST *addr = NULL, *detector = NULL; static char *DefaultName = Q330_DEFAULT_CFG_PATH; /* Locate and open the file */ if (name == NULL) name = getenv(Q330_CFG_ENV_STRING); if (name == NULL) name = DefaultName; if (!utilFileExists(name)) return NULL; if ((fp = fopen(name, "r")) == NULL) return ReadCfgCleanReturn(fp, cfg, addr, NULL); /* Create the config structure and save the name */ if ((cfg = CreateCfg(name)) == NULL) return ReadCfgCleanReturn(fp, cfg, addr, NULL); cfg->calib = DefaultCalib; /* Initialize the temp lists */ if ((addr = listCreate()) == NULL) return ReadCfgCleanReturn(fp, cfg, addr, NULL); if ((detector = listCreate()) == NULL) return ReadCfgCleanReturn(fp, cfg, addr, NULL); /* Read the contents of the file into the list */ while ((status = utilGetLine(fp, input, MAXLINELEN, COMMENT, NULL)) == 0) { ntoken = utilParse(input, token, " \t", MAX_TOKEN, QUOTE); /* "q330" entries define Q330_ADDR structures */ if (strcasecmp(token[0], "q330") == 0) { if (!AddAddr(addr, token, ntoken)) return ReadCfgCleanReturn(fp, cfg, addr, NULL); /* "detector" entries define Q330_ADDR structures */ } else if (strcasecmp(token[0], "detector") == 0) { if (!AddDetector(detector, token, ntoken)) return ReadCfgCleanReturn(fp, cfg, addr, NULL); /* "calib" entries define QDP_TYPE_C1_QCAL structures */ } else if (strcasecmp(token[0], "calib") == 0) { if (!AddCalib(&cfg->calib, token, ntoken)) return ReadCfgCleanReturn(fp, cfg, addr, NULL); /* anything else is not supported */ } else { errno = EINVAL; return ReadCfgCleanReturn(fp, cfg, addr, NULL); } } if (status != 1) return ReadCfgCleanReturn(fp, cfg, addr, NULL); /* Copy the addresses into the output structure */ if (!listSetArrayView(addr)) return ReadCfgCleanReturn(fp, cfg, addr, NULL); cfg->naddr = addr->count; if ((cfg->addr = (Q330_ADDR *) malloc(sizeof(Q330_ADDR) * cfg->naddr)) == NULL) return ReadCfgCleanReturn(fp, cfg, addr, NULL); for (i = 0; i < addr->count; i++) cfg->addr[i] = *((Q330_ADDR *) addr->array[i]); /* Copy the detectors into the output structure */ if (!listSetArrayView(detector)) return ReadCfgCleanReturn(fp, cfg, addr, NULL); cfg->ndetector = detector->count; if ((cfg->detector = (Q330_DETECTOR *) malloc(sizeof(Q330_DETECTOR) * cfg->ndetector)) == NULL) return ReadCfgCleanReturn(fp, cfg, detector, NULL); for (i = 0; i < detector->count; i++) cfg->detector[i] = *((Q330_DETECTOR *) detector->array[i]); /* All done, return the newly created structure */ return ReadCfgCleanReturn(fp, NULL, addr, cfg); }