int CConfigHandler::parseFile(FILE *pFile, int (*handler)(void *, const char *, const char *, const char *), void *object) { char line[MAX_LINE]; char section[MAX_SECTION]; char *start; char *end; char *name; char *value; char *chr; int lineno = 0; memset(line, 0, sizeof(line)); while (fgets(line, MAX_LINE, pFile) != NULL) { start = line; start = lstrip(rstrip(start)); if (*start == ';' || *start == '#' || 0 >= strlen(start)) { memset(line, 0, sizeof(line)); continue; } /** * [section] line */ chr = strchr(line, '['); if (chr) { end = strchr(chr + 1, ']'); if (end) { *end = '\0'; memset(section, 0, sizeof(section)); strcpy(section, chr + 1); } continue; } /** * name = value */ end = strchr(line, '='); if (end && strlen(section)) { *end = '\0'; name = lstrip(rstrip(start)); value = lstrip(end + 1); rstrip(value); handler(object, section, name, value); ++lineno; } } return lineno; }
void strip( char *s, const char *delimiters) { lstrip(s, delimiters); rstrip(s, delimiters); }
static char *read_uint(ALuint *num, const char *line, int base) { char *end; *num = strtoul(line, &end, base); if(end && *end != '\0') end = lstrip(end); return end; }
bool initLemmaLexicon ( const std::string sInputFile ) { std::ifstream *is; is = new std::ifstream(sInputFile.c_str()); if ( is->fail() ) return false; bool bReadSuccessful; std::string line; std::string curToken; getline(*is, line); while(is && !lstrip(line).empty()) { std::string form; std::string lemma; std::string tag; std::istringstream iss(rstrip(line)); getline(iss, curToken, '\t'); ASSERT(is && !curToken.empty(), "Not well formatted lexicon data (form not found)"); form = curToken; //iss = std::istringstream(rstrip(line)); getline(iss, curToken, '\t'); ASSERT(is && !curToken.empty(), "Not well formatted lexicon data (lemma not found)"); lemma = curToken; if ( lemma == "=" ) lemma = form; //lexicon uses = to represent that lemma equals form //iss = std::istringstream(rstrip(line)); getline(iss, curToken, '\t'); ASSERT(is && !curToken.empty(), "Not well formatted lexicon data (tag not found)"); tag = curToken; //add to the word to lemma map //wordToLemma.insert(form,lemma); wordToLemma[form]=lemma; CMorphTag morphTag = lexiconTagToMorphTag(tag); std::pair<std::string,CMorphTag> wordMorphPair = std::pair<std::string,CMorphTag>(form,morphTag); wordAndTagToLemma[wordMorphPair] = lemma; getline(*is, line); } is->close(); delete is; //And we are done. return true; }
static PyObject * mvw(PyObject *self, PyObject *args) { int y, x, width, rep_len, end_len, ret; char *message, *rep, *end; const char *m_enc, *r_enc, *e_enc; PyObject *window; WINDOW *win; /* We use the 'et' format because we don't want Python to touch the encoding and generate Unicode Exceptions */ if(!PyArg_ParseTuple(args, "Oiiietetet", &window, &y, &x, &width, &m_enc, &message, &r_enc, &rep, &e_enc, &end)) return NULL; if (window != Py_None) win = ((PyCursesWindowObject *)window)->win; else win = NULL; rep_len = strlen(rep); end_len = theme_strlen(end, 0); /* Make width relative to current x */ width += x; int i = 0; for (i = 0; ((x < width - end_len) || (message[i] == '%')); i++) { ret = do_char(win, width - end_len, &i, &y, &x, message); if (ret) break; } int j = 0; for(j = 0; x < (width - end_len); j = (j + 1) % rep_len) do_char(win, width - end_len, &j, &y, &x, rep); for(j = 0; end[j]; j++) do_char(win, width, &j, &y, &x, end); PyMem_Free(rep); PyMem_Free(end); if (ret == -1) { PyMem_Free(message); return Py_BuildValue("s", NULL); } else { PyObject *r = Py_BuildValue("s", lstrip(&message[i])); PyMem_Free(message); return r; } }
static char *read_float(ALfloat *num, const char *line) { char *end; #ifdef HAVE_STRTOF *num = strtof(line, &end); #else *num = (ALfloat)strtod(line, &end); #endif if(end && *end != '\0') end = lstrip(end); return end; }
void LoadExcludes (char ***ex,int *nex,char *filename) { FILE *fp; int x; char **test; static char s[CCHMAXPATHCOMP + 4]; BOOL temp = fSuspend; if(!ex || !nex || !filename) return; fSuspend = TRUE; if(*ex) FreeExcludes(ex, nex); x = 0; sprintf(s, "%s%s", mydir, filename); fp = fopen(s,"r"); if(fp) { while(!feof(fp)) { if(!fgets(s, sizeof(s), fp)) break; s[sizeof(s) - 1] = 0; stripcr(s); lstrip(rstrip(s)); if(*s) { if(x >= *nex - 1) { test = realloc(*ex, sizeof(char *) * (*nex + 2)); if(test) *ex = test; else break; } (*ex)[*nex] = strdup(s); if((*ex)[*nex]) { (*nex)++; (*ex)[*nex] = NULL; } else break; } } fclose(fp); } fSuspend = temp; }
static char * polezero_comment_token(char *p) { if(!p) { return p; } p = strchr(p, ':'); if(p) { p = p + 1; p = lstrip(p); p = rstrip(p); } return p; }
/**@brief Called at the end of set up to look for any invalid options, ignoring dashes * */ void lookForInvalidOptionsDashInsens() { auto flagStrs = flags_.getFlags(); strVecToLower(flagStrs); for(auto & f : flagStrs){ lstrip(f, '-'); } for (const auto &com : commands_.arguments_) { if (!contains(flagStrs, lstripRet(com.first, '-'))) { warnings_.emplace_back( bashCT::bold + bashCT::red + "Unrecognized option, " + com.first + " not using" + bashCT::reset); } } }
char *read_clipped_line(FILE *f, char **buffer, size_t *maxlen) { while(readline(f, buffer, maxlen)) { char *line, *comment; line = lstrip(*buffer); comment = strchr(line, '#'); if(comment) *(comment++) = 0; line = rstrip(line); if(line[0]) return line; } return NULL; }
void Lpatch::clear_subdiv_strips(int /* level */) { // XXX - being phased out... (not called) // // the problem is that when subdivision meshes get deleted, we also // have to delete the sub-strips that reference Lfaces in // them. this first implementation ignores the given level at which // subdivision faces were deleted, and just blows away subdiv // strips at all levels below the top. if (!_tri_strips_dirty) { for (vector<TriStrip*>::size_type i=0; i<_tri_strips.size(); i++) lstrip(i)->delete_substrips(); } }
void joinall(char *baseuri, char **uris, int size){ int i; char *parsed = NULL; for (i = 0; i < size; i++){ lstrip(uris[i]); rstrip(uris[i]); parsed = join(baseuri,uris[i]); if (parsed == NULL) { continue; } free(uris[i]); uris[i] = NULL; uris[i] = parsed; parsed = NULL; } }
int main(void) { const char* t1 = "This is a test sentence to see if\nwhitespace removal is working"; const char* t2 = "gggThis tests if lstrip is working"; const char* t3 = "This tests if rstrip is workinghhh"; const char* t4 = "000 this tests is strip is working 000"; const char* t5 = "this tEsts if UpPer is WorkINg"; const char* t6 = "THIS TESTS if LOwEr is WORKInG"; printf("%s\n %s\n\n", t1, removeWhiteSpace(t1)); printf("%s\n %s\n\n", t2, lstrip(t2, 'g')); printf("%s\n %s\n\n", t3, rstrip(t3, 'h')); printf("%s\n %s\n\n", t4, strip(t4, '0')); printf("%s\n %s\n\n", t5, upper(t5)); printf("%s\n %s\n\n", t6, lower(t6)); return 0; }
static void LoadConfigFromFile(FILE *f) { char curSection[128] = ""; char *buffer = NULL; size_t maxlen = 0; ConfigEntry *ent; while(readline(f, &buffer, &maxlen)) { char *line, *comment; char key[256] = ""; char value[256] = ""; line = rstrip(lstrip(buffer)); if(!line[0]) continue; if(line[0] == '[') { char *section = line+1; char *endsection; endsection = strchr(section, ']'); if(!endsection || section == endsection) { ERR("config parse error: bad line \"%s\"\n", line); continue; } if(endsection[1] != 0) { char *end = endsection+1; while(isspace(*end)) ++end; if(*end != 0 && *end != '#') { ERR("config parse error: bad line \"%s\"\n", line); continue; } } *endsection = 0; if(strcasecmp(section, "general") == 0) curSection[0] = 0; else { size_t len, p = 0; do { char *nextp = strchr(section, '%'); if(!nextp) { strncpy(curSection+p, section, sizeof(curSection)-1-p); break; } len = nextp - section; if(len > sizeof(curSection)-1-p) len = sizeof(curSection)-1-p; strncpy(curSection+p, section, len); p += len; section = nextp; if(((section[1] >= '0' && section[1] <= '9') || (section[1] >= 'a' && section[1] <= 'f') || (section[1] >= 'A' && section[1] <= 'F')) && ((section[2] >= '0' && section[2] <= '9') || (section[2] >= 'a' && section[2] <= 'f') || (section[2] >= 'A' && section[2] <= 'F'))) { unsigned char b = 0; if(section[1] >= '0' && section[1] <= '9') b = (section[1]-'0') << 4; else if(section[1] >= 'a' && section[1] <= 'f') b = (section[1]-'a'+0xa) << 4; else if(section[1] >= 'A' && section[1] <= 'F') b = (section[1]-'A'+0x0a) << 4; if(section[2] >= '0' && section[2] <= '9') b |= (section[2]-'0'); else if(section[2] >= 'a' && section[2] <= 'f') b |= (section[2]-'a'+0xa); else if(section[2] >= 'A' && section[2] <= 'F') b |= (section[2]-'A'+0x0a); if(p < sizeof(curSection)-1) curSection[p++] = b; section += 3; } else if(section[1] == '%') { if(p < sizeof(curSection)-1) curSection[p++] = '%'; section += 2; } else { if(p < sizeof(curSection)-1) curSection[p++] = '%'; section += 1; } if(p < sizeof(curSection)-1) curSection[p] = 0; } while(p < sizeof(curSection)-1 && *section != 0); curSection[sizeof(curSection)-1] = 0; } continue; } comment = strchr(line, '#'); if(comment) *(comment++) = 0; if(!line[0]) continue; if(sscanf(line, "%255[^=] = \"%255[^\"]\"", key, value) == 2 || sscanf(line, "%255[^=] = '%255[^\']'", key, value) == 2 || sscanf(line, "%255[^=] = %255[^\n]", key, value) == 2) { /* sscanf doesn't handle '' or "" as empty values, so clip it * manually. */ if(strcmp(value, "\"\"") == 0 || strcmp(value, "''") == 0) value[0] = 0; } else if(sscanf(line, "%255[^=] %255[=]", key, value) == 2) { /* Special case for 'key =' */ value[0] = 0; } else { ERR("config parse error: malformed option line: \"%s\"\n\n", line); continue; } rstrip(key); if(curSection[0] != 0) { size_t len = strlen(curSection); memmove(&key[len+1], key, sizeof(key)-1-len); key[len] = '/'; memcpy(key, curSection, len); } /* Check if we already have this option set */ ent = cfgBlock.entries; while((unsigned int)(ent-cfgBlock.entries) < cfgBlock.entryCount) { if(strcasecmp(ent->key, key) == 0) break; ent++; } if((unsigned int)(ent-cfgBlock.entries) >= cfgBlock.entryCount) { /* Allocate a new option entry */ ent = realloc(cfgBlock.entries, (cfgBlock.entryCount+1)*sizeof(ConfigEntry)); if(!ent) { ERR("config parse error: error reallocating config entries\n"); continue; } cfgBlock.entries = ent; ent = cfgBlock.entries + cfgBlock.entryCount; cfgBlock.entryCount++; ent->key = strdup(key); ent->value = NULL; } free(ent->value); ent->value = expdup(value); TRACE("found '%s' = '%s'\n", ent->key, ent->value); } free(buffer); }
// we need to find out where the tag ends in the raw q3string playername. // then we can convert it to html void create_clantag_html( int pl, int reverse ) { char rawtag[ 50 ]; int icol; int itag = 0; // pointer in tag string int itag_end = strlen( TEAMS[ PLAYERS[ pl ].team ].clantag ) - 1; char x,y; int iraw = 0; // pointer in playername string int iraw_end = strlen( PLAYERS[ pl ].name ) - 1; if ( reverse ) { // ^7 s^3i^7gge ^5plan^7-^3B , match with plan-b // this seems to be working, tested in qconsole.log.Hardcore3 itag = itag_end; // start at the end ( bla plan-B ) iraw = iraw_end; strcpy( TEAMS[ PLAYERS[ pl ].team ].clantag_html, TEAMS[ PLAYERS[ pl ].team ].clantag ); while ( itag >= 0 && iraw >= 0 ) { x = TEAMS[ PLAYERS[ pl ].team ].clantag[ itag ]; y = PLAYERS[ pl ].name[ iraw ]; if ( tolower( x ) == tolower( y ) ) { // x: the current character in the plaintext clantag itag--; } if ( itag >= 0 ) { // so it won't be done the last time iraw--; } } // ok now we got " ^5plan^7-^3B" strcpy( rawtag, PLAYERS[ pl ].name + iraw ); lstrip( rawtag ); // now we got "^5plan^7-^3B" // test if rawtag starts with a colorcode, if not then // look for previous colorcode so we know what color the tag starts with if ( rawtag[ 0 ] == '^' ) { // ok } else { icol = iraw; while ( icol > 0 && PLAYERS[ pl ].name[ icol - 1 ] != '^' ) { icol--; } if ( PLAYERS[ pl ].name[ icol - 1 ] == '^' ) { strncopy( rawtag, &(PLAYERS[ pl ].name[ icol - 1 ]), 2 ); } else { // use default colorcode ^7 (white) strcpy( rawtag, "^7" ); } strcat( rawtag, PLAYERS[ pl ].name + iraw ); } build_clantag( TEAMS[ PLAYERS[ pl ].team ].clantag_html, rawtag ); //strcpy( TEAMS[ PLAYERS[ pl ].team ].clantag_html, et_2_html( rawtag ) ); return; } else { while ( ( ( x = TEAMS[ PLAYERS[ pl ].team ].clantag[ itag ] ) != '\0' ) && ( itag <= itag_end ) && ( iraw <= iraw_end ) ) { y = PLAYERS[ pl ].name[ iraw ]; // y: the current character in the raw quake3 name // compare the lowercase characters because the NAMECLEAN is converted to lowercase if ( tolower( x ) == tolower( y ) ) { // x: the current character in the plaintext clantag itag++; iraw++; } else { if ( y == '^' ) { iraw = iraw + 2; } else { // actually this should not happen, but just in case, prevent infinite loop iraw = iraw + 1; } } } // iraw gives the length of the raw string strncopy( rawtag, PLAYERS[ pl ].name, iraw ); // todo: add google search link build_clantag( TEAMS[ PLAYERS[ pl ].team ].clantag_html, rawtag ); //strcpy( TEAMS[ PLAYERS[ pl ].team ].clantag_html, et_2_html( rawtag ) ); } }
const char* strip(char* src){ rstrip(src); lstrip(src); return src; }
int ambdec_load(AmbDecConf *conf, const char *fname) { char *buffer = NULL; size_t maxlen = 0; char *line; FILE *f; f = al_fopen(fname, "r"); if(!f) { ERR("Failed to open: %s\n", fname); return 0; } while((line=read_clipped_line(f, &buffer, &maxlen)) != NULL) { char *saveptr; char *command; command = my_strtok_r(line, "/ \t", &saveptr); if(!command) { ERR("Malformed line: %s\n", line); goto fail; } if(strcmp(command, "description") == 0) { char *value = my_strtok_r(NULL, "", &saveptr); al_string_copy_cstr(&conf->Description, lstrip(value)); } else if(strcmp(command, "version") == 0) { line = my_strtok_r(NULL, "", &saveptr); line = read_uint(&conf->Version, line, 10); if(line && *line != '\0') { ERR("Extra junk after version: %s\n", line); goto fail; } if(conf->Version != 3) { ERR("Unsupported version: %u\n", conf->Version); goto fail; } } else if(strcmp(command, "dec") == 0) { const char *dec = my_strtok_r(NULL, "/ \t", &saveptr); if(strcmp(dec, "chan_mask") == 0) { line = my_strtok_r(NULL, "", &saveptr); line = read_uint(&conf->ChanMask, line, 16); if(line && *line != '\0') { ERR("Extra junk after mask: %s\n", line); goto fail; } } else if(strcmp(dec, "freq_bands") == 0) { line = my_strtok_r(NULL, "", &saveptr); line = read_uint(&conf->FreqBands, line, 10); if(line && *line != '\0') { ERR("Extra junk after freq_bands: %s\n", line); goto fail; } if(conf->FreqBands != 1 && conf->FreqBands != 2) { ERR("Invalid freq_bands value: %u\n", conf->FreqBands); goto fail; } } else if(strcmp(dec, "speakers") == 0) { line = my_strtok_r(NULL, "", &saveptr); line = read_uint(&conf->NumSpeakers, line, 10); if(line && *line != '\0') { ERR("Extra junk after speakers: %s\n", line); goto fail; } if(conf->NumSpeakers > MAX_OUTPUT_CHANNELS) { ERR("Unsupported speaker count: %u\n", conf->NumSpeakers); goto fail; } } else if(strcmp(dec, "coeff_scale") == 0) { line = my_strtok_r(NULL, " \t", &saveptr); if(strcmp(line, "n3d") == 0) conf->CoeffScale = ADS_N3D; else if(strcmp(line, "sn3d") == 0) conf->CoeffScale = ADS_SN3D; else if(strcmp(line, "fuma") == 0) conf->CoeffScale = ADS_FuMa; else { ERR("Unsupported coeff scale: %s\n", line); goto fail; } } else { ERR("Unexpected /dec option: %s\n", dec); goto fail; } } else if(strcmp(command, "opt") == 0) { const char *opt = my_strtok_r(NULL, "/ \t", &saveptr); if(strcmp(opt, "xover_freq") == 0) { line = my_strtok_r(NULL, "", &saveptr); line = read_float(&conf->XOverFreq, line); if(line && *line != '\0') { ERR("Extra junk after xover_freq: %s\n", line); goto fail; } } else if(strcmp(opt, "xover_ratio") == 0) { line = my_strtok_r(NULL, "", &saveptr); line = read_float(&conf->XOverRatio, line); if(line && *line != '\0') { ERR("Extra junk after xover_ratio: %s\n", line); goto fail; } } else if(strcmp(opt, "input_scale") == 0 || strcmp(opt, "nfeff_comp") == 0 || strcmp(opt, "delay_comp") == 0 || strcmp(opt, "level_comp") == 0) { /* Unused */ my_strtok_r(NULL, " \t", &saveptr); } else { ERR("Unexpected /opt option: %s\n", opt); goto fail; } } else if(strcmp(command, "speakers") == 0) { const char *value = my_strtok_r(NULL, "/ \t", &saveptr); if(strcmp(value, "{") != 0) { ERR("Expected { after speakers command, got %s\n", value); goto fail; } if(!load_ambdec_speakers(conf, f, &buffer, &maxlen, &saveptr)) goto fail; value = my_strtok_r(NULL, "/ \t", &saveptr); if(!value) { line = read_clipped_line(f, &buffer, &maxlen); if(!line) { ERR("Unexpected end of file\n"); goto fail; } value = my_strtok_r(line, "/ \t", &saveptr); } if(strcmp(value, "}") != 0) { ERR("Expected } after speaker definitions, got %s\n", value); goto fail; } } else if(strcmp(command, "lfmatrix") == 0 || strcmp(command, "hfmatrix") == 0 || strcmp(command, "matrix") == 0) { const char *value = my_strtok_r(NULL, "/ \t", &saveptr); if(strcmp(value, "{") != 0) { ERR("Expected { after speakers command, got %s\n", value); goto fail; } if(conf->FreqBands == 1) { if(strcmp(command, "matrix") != 0) { ERR("Unexpected \"%s\" type for a single-band decoder\n", command); goto fail; } if(!load_ambdec_matrix(conf->HFOrderGain, conf->HFMatrix, conf->NumSpeakers, f, &buffer, &maxlen, &saveptr)) goto fail; } else { if(strcmp(command, "lfmatrix") == 0) { if(!load_ambdec_matrix(conf->LFOrderGain, conf->LFMatrix, conf->NumSpeakers, f, &buffer, &maxlen, &saveptr)) goto fail; } else if(strcmp(command, "hfmatrix") == 0) { if(!load_ambdec_matrix(conf->HFOrderGain, conf->HFMatrix, conf->NumSpeakers, f, &buffer, &maxlen, &saveptr)) goto fail; } else { ERR("Unexpected \"%s\" type for a dual-band decoder\n", command); goto fail; } } value = my_strtok_r(NULL, "/ \t", &saveptr); if(!value) { line = read_clipped_line(f, &buffer, &maxlen); if(!line) { ERR("Unexpected end of file\n"); goto fail; } value = my_strtok_r(line, "/ \t", &saveptr); } if(strcmp(value, "}") != 0) { ERR("Expected } after matrix definitions, got %s\n", value); goto fail; } } else if(strcmp(command, "end") == 0) { line = my_strtok_r(NULL, "/ \t", &saveptr); if(line) { ERR("Unexpected junk on end: %s\n", line); goto fail; } fclose(f); free(buffer); return 1; } else { ERR("Unexpected command: %s\n", command); goto fail; } line = my_strtok_r(NULL, "/ \t", &saveptr); if(line) { ERR("Unexpected junk on line: %s\n", line); goto fail; } } ERR("Unexpected end of file\n"); fail: fclose(f); free(buffer); return 0; }
/* trim leading and trailing spaces from string */ char *strip(char *str) { return lstrip(rstrip(str)); }
str *str::strip(str *chars) { return lstrip(chars)->rstrip(chars); }
/* * strip: Return a copy of s with leading and trailing whitespace * removed */ inline std::string strip(const std::string& s) { return rstrip(lstrip(s)); }
int EnterLine (char *buf,ULONG len,USHORT y,char *filename, char **choices,int numc,int start) { ULONG pos = 0,c,nm,numchars = 0,tpos,wl; APIRET rc; int key,attr = ((7 << 4) << 8) | ' ',ret = 0; BOOL okay = TRUE,insert = TRUE,lasttab = FALSE; char *sav,*k = NULL,*p; static char keybuf[1026]; USHORT t; FILEFINDBUF3 fb3; HDIR hdir = HDIR_CREATE; wl = 0; sav = malloc(vio.col * 2); if(!sav) { DosBeep(50,100); *buf = 0; return -1; } ThreadMsg(" "); t = vio.col * 2; VioReadCellStr(sav,&t,y,0,0); VioWrtNCell((char *)&attr,vio.col,y,0,0); ShowCursor(FALSE); VioSetCurPos(y,0,0); for(c = 0;c < len - 1;c++) { /* find end of default string */ if(!buf[c]) break; } pos = c; for(;c < len - 1;c++) /* space-pad remainder of buf */ buf[c] = ' '; while(okay) { /* assure buffer is null terminated (cluck, cluck) */ buf[len - 1] = 0; /* assure pos hasn't gone past our limit */ if(pos > len - 1) pos = len - 1; /* set left side of entry field */ if(pos < wl) wl = pos; if(pos >= wl + vio.col) wl = (pos - vio.col) + 1; /* set cursor position */ VioSetCurPos(y,pos - wl,0); /* display buf */ tpos = min(vio.col,len - wl); /* max length of displayable text */ VioWrtCharStr(buf + wl,tpos,y,0,0); /* show text */ if(tpos < vio.col) /* space-pad? */ VioWrtNChar(" ",vio.col - tpos,y,tpos,0); if(k && *k) /* "macros" waiting to be entered? */ key = *k++; /* yep, skip keyboard input */ else { /* nope, go to the keyboard */ k = NULL; /* clear macro pointer */ key = get_ch(-1); } switch(key) { case 256: /* shiftstate changed -- ignore */ break; case '\r': /* accept input */ okay = FALSE; break; case 45 | 256: case 61 | 256: case '\x1b': /* Escape -- exit editor */ memset(buf,' ',len - 1); buf[len - 1] = 0; okay = FALSE; ret = -1; break; case '\b': if(pos) { pos--; memmove(buf + pos,buf + (pos + 1),len - (pos + 1)); buf[len - 2] = ' '; } lasttab = FALSE; break; case 25: /* ctrl+y -- clear input */ VioWrtNCell((char *)&attr,vio.col,y,0,0); memset(buf,' ',len - 1); buf[len - 1] = 0; wl = pos = 0; lasttab = FALSE; break; case 59 | 256: /* F1 -- insert directory */ k = directory; lasttab = FALSE; break; case 60 | 256: /* F2 -- insert filename */ if(filename) k = filename; lasttab = FALSE; break; case 62 | 256: /* F4 -- insert target */ k = target; lasttab = FALSE; break; case 15: /* shift+tab */ if(hdir != (HDIR)HDIR_CREATE) { DosFindClose(hdir); hdir = HDIR_CREATE; lasttab = FALSE; } /* intentional fallthru */ case 9: /* tab -- auto-complete */ if(!pos || pos >= len - 1) break; if(lasttab && numchars) { lasttab = FALSE; for(tpos = 0;tpos < numchars;tpos++) keybuf[tpos] = '\b'; keybuf[tpos++] = '\01'; keybuf[tpos] = 0; numchars = 0; k = keybuf; break; } else { if(hdir != (HDIR)HDIR_CREATE) DosFindClose(hdir); hdir = HDIR_CREATE; } /* intentional fallthru */ case 1: /* cheat! */ k = NULL; if(!pos || pos >= len - 1) break; tpos = pos - 1; while(tpos && buf[tpos] != ' ') tpos--; if(buf[tpos] == ' ') tpos++; strcpy(keybuf,buf + tpos); tpos = 0; while(keybuf[tpos] && keybuf[tpos] != ' ') tpos++; keybuf[tpos] = 0; lstrip(rstrip(keybuf)); if(*keybuf) { strcat(keybuf,"*"); nm = 1; if(hdir == (HDIR)HDIR_CREATE) rc = DosFindFirst(keybuf, &hdir, FILE_NORMAL | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY | FILE_ARCHIVED | FILE_READONLY, &fb3, sizeof(fb3), &nm, FIL_STANDARD); else rc = DosFindNext(hdir,&fb3,sizeof(fb3),&nm); if(!rc) { while((fb3.attrFile & FILE_DIRECTORY) && (*fb3.achName == '.' && (!fb3.achName[1] || (fb3.achName[1] == '.' && !fb3.achName[2])))) { nm = 1; if(DosFindNext(hdir,&fb3,sizeof(fb3),&nm)) { DosFindClose(hdir); hdir = HDIR_CREATE; *fb3.achName = 0; break; } } if(*fb3.achName) { keybuf[strlen(keybuf) - 1] = 0; p = strchr(keybuf,'\\'); if(p) p++; else p = keybuf; tpos = 0; while(*p && fb3.achName[tpos] && toupper(*p) == toupper(fb3.achName[tpos])) { p++; tpos++; } if(fb3.achName[tpos]) { strcpy(keybuf,fb3.achName + tpos); numchars = strlen(keybuf); lasttab = TRUE; k = keybuf; } else if(hdir != (HDIR)HDIR_CREATE) { DosFindClose(hdir); hdir = HDIR_CREATE; } } } else if(hdir != (HDIR)HDIR_CREATE) { DosBeep(50,50); DosFindClose(hdir); hdir = HDIR_CREATE; } } break; case 83 | 256: /* delete */ memmove(buf + pos,buf + (pos + 1),len - (pos + 1)); buf[len - 2] = ' '; lasttab = FALSE; break; case 82 | 256: /* insert */ insert = (insert) ? FALSE : TRUE; break; case 71 | 256: /* home */ wl = pos = 0; lasttab = FALSE; break; case 79 | 256: /* end */ pos = len - 2; while(pos && buf[pos] == ' ') pos--; if(pos && buf[pos] != ' ') pos++; lasttab = FALSE; break; case 75 | 256: /* left */ if(pos) pos--; lasttab = FALSE; break; case 77 | 256: /* right */ if(pos < len - 1) pos++; lasttab = FALSE; break; case 72 | 256: /* up */ lasttab = FALSE; if(choices) { if(choices[start]) { VioWrtNCell((char *)&attr,vio.col,y,0,0); memset(buf,' ',len - 1); buf[len - 1] = 0; wl = pos = 0; k = choices[start]; start++; while(start < numc && !choices[start]) start++; if(start > (numc - 1)) start = 0; if(!choices[start]) { while(start < numc && !choices[start]) start++; } if(start > (numc - 1)) start = 0; } } break; case 80 | 256: /* down */ lasttab = FALSE; if(choices) { if(choices[start]) { VioWrtNCell((char *)&attr,vio.col,y,0,0); memset(buf,' ',len - 1); buf[len - 1] = 0; wl = pos = 0; k = choices[start]; start--; while(start >= 0 && !choices[start]) start--; if(start < 0) start = numc - 1; if(!choices[start]) { while(start >= 0 && !choices[start]) start--; } if(start < 0) start = numc - 1; } } break; case 115 | 256: /* ctrl+left */ while(pos && buf[pos] == ' ') pos--; while(pos && buf[pos] != ' ') pos--; lasttab = FALSE; break; case 116 | 256: /* ctrl + right */ while(pos < len - 1 && buf[pos] == ' ') pos++; while(pos < len - 1 && buf[pos] != ' ') pos++; lasttab = FALSE; break; default: if(pos < len - 1 && !(key & 256) && !iscntrl(key)) { if(insert) { if(pos < len - 2) { memmove(buf + (pos + 1),buf + pos,len - (pos + 2)); buf[len - 2] = ' '; } buf[pos] = (char)key; } else buf[pos] = (char)key; pos++; } else if(pos >= len - 1) DosBeep(250,25); break; } } if(hdir != (HDIR)HDIR_CREATE) DosFindClose(hdir); ShowCursor(TRUE); VioWrtCellStr(sav,vio.col * 2,y,0,0); free(sav); SetupConsole(); ThreadMsg(NULL); buf[len - 1] = 0; lstrip(rstrip(buf)); return (ret) ? ret : strlen(buf); }
static void FindSwapperDat (char *SwapperDat) { char *filename = "C:\\CONFIG.SYS",*p,*pp; char *input = NULL; FILE *fp; FILEFINDBUF3 ffb; ULONG nm = 1L,size = sizeof(SwapperDat); HDIR hdir = HDIR_CREATE; APIRET rc = 1L; input = malloc(8192); if(input) { *input = 0; *SwapperDat = 0; PrfQueryProfileData(prof, appname, "SwapperDat", (PVOID)SwapperDat, &size); if(*SwapperDat) { rc = DosFindFirst(SwapperDat, &hdir, FILE_NORMAL | FILE_ARCHIVED | FILE_HIDDEN | FILE_SYSTEM | FILE_READONLY, &ffb, sizeof(ffb), &nm, FIL_STANDARD); if(!rc) { DosFindClose(hdir); fp = fopen(SwapperDat,"r"); if(fp) { fclose(fp); *SwapperDat = 0; rc = 1L; } } else *SwapperDat = 0; } if(rc) { if(DosQuerySysInfo(QSV_BOOT_DRIVE, QSV_BOOT_DRIVE, (PVOID)&nm, (ULONG)sizeof(ULONG))) nm = 3L; *filename = (char)nm + '@'; fp = fopen(filename,"r"); if(fp) { while(!feof(fp)) { if(!fgets(input,8192,fp)) break; input[8191] = 0; lstrip(input); if(!strnicmp(input,"SWAPPATH",8)) { p = input + 8; while(*p == ' ') p++; if(*p == '=') { p++; stripcr(p); rstrip(p); while(*p == ' ') p++; if(*p == '\"') { p++; pp = p; while(*pp && *pp != '\"') *pp++; if(*pp) *pp = 0; } else { pp = strchr(p,' '); if(pp) *pp = 0; } if(*p) { strncpy(SwapperDat,p,CCHMAXPATH); SwapperDat[CCHMAXPATH - 1] = 0; if(SwapperDat[strlen(SwapperDat) - 1] != '\\') strcat(SwapperDat,"\\"); strcat(SwapperDat,"SWAPPER.DAT"); hdir = HDIR_CREATE; nm = 1L; if(!DosFindFirst(SwapperDat, &hdir, FILE_NORMAL | FILE_ARCHIVED | FILE_HIDDEN | FILE_SYSTEM | FILE_READONLY, &ffb, sizeof(ffb), &nm, FIL_STANDARD)) { DosFindClose(hdir); PrfWriteProfileString(prof, appname, "SwapperDat", SwapperDat); } else *SwapperDat = 0; break; } } } } fclose(fp); } } if(!*SwapperDat) { if(DosQuerySysInfo(QSV_BOOT_DRIVE, QSV_BOOT_DRIVE, (PVOID)&nm, (ULONG)sizeof(ULONG))) nm = 3L; sprintf(SwapperDat, "%c:\\OS2\\SYSTEM\\SWAPPER.DAT", (char)nm + '@'); hdir = HDIR_CREATE; nm = 1L; if(!DosFindFirst(SwapperDat, &hdir, FILE_NORMAL | FILE_ARCHIVED | FILE_HIDDEN | FILE_SYSTEM | FILE_READONLY, &ffb, sizeof(ffb), &nm, FIL_STANDARD)) { DosFindClose(hdir); PrfWriteProfileString(prof, appname, "SwapperDat", SwapperDat); } else *SwapperDat = 0; } free(input); } }
/* Removes the leading and trailing whitespace from string s. String s is modified by this function. Input: C string - The string to remove the leading and trailing whitespace from Output: None */ void strip(char *s) { lstrip(s); rstrip(s); }
int main(int argc, char **argv) { if(argc < 4) printUsage(argv[0]); p2p_server = id_bound = id_connect = NULL; #ifdef __LINUX__ int opt; while((opt = getopt(argc, argv, "s:u:c:")) != -1) { switch(opt) { case 's': p2p_server = optarg; break; case 'u': id_bound = optarg; break; case 'c': id_connect = optarg; break; case 'n': break; default: printUsage(argv[0]); } } #elif defined(WIN32) int i; for(i=1; i<argc; i++) { if(strcmp(argv[i], "-s") == 0) p2p_server = argv[++i]; else if(strcmp(argv[i], "-u") == 0) id_bound = argv[++i]; else if(strcmp(argv[i], "-c") == 0) id_connect = argv[++i]; else printUsage(argv[0]); } #endif if(!p2p_server || (!id_bound && !id_connect)) printUsage(argv[0]); const char *svrs[] = { p2p_server }; PA_NetLibInit(); P2pCoreInitialize(svrs, 1, id_bound, &cbs); printf("Waiting for initialization....\n"); PA_Sleep(2000); if(id_connect) connectTo(p2p_server, id_connect); char line[512]; int err; printHelp(); while(1) { err = 0; printf("Select: "); fflush(stdout); if(fgets(line, sizeof(line), stdin) == NULL) { printf("EOF of stdin\n"); break; } if(line[0] == 'q') break; else switch(line[0]) { case 'h': printHelp(); break; case 'c': err = connectTo(p2p_server, sstrip(line+1)); break; case 'r': err = relayTo(p2p_server, sstrip(line+1)); break; case 's': printConnectionsState(); break; case 'g': err = getFile(g_hconn, sstrip(line+1)); break; case 'p': err = putFile(g_hconn, sstrip(line+1)); break; case 'e': echo(g_hconn, lstrip(line+1)); break; case 'f': flood(g_hconn, lstrip(line+1)); break; case 'F': flood2(g_hconn, lstrip(line+1)); break; default: if(!isspace(line[0])) printHelp(); break; } if(err) { printErr(err); } } if(g_hconn) { void *ptr; P2pConnGetUserData(g_hconn, &ptr); free(ptr); P2pConnClose(g_hconn); } P2pCoreTerminate(); P2pCoreCleanup(); return 0; }
/* * Parse the configuration file and do initial data structure setup. */ int event_config(const char *fname) { FILE *configFile; event_logfile_t *logfile; int sts = 0; size_t len; char line[MAXPATHLEN * 2]; char *ptr, *name, *noaccess; configFile = fopen(fname, "r"); if (configFile == NULL) { __pmNotifyErr(LOG_ERR, "event_config: %s: %s", fname, strerror(errno)); return -1; } while (!feof(configFile)) { if (fgets(line, sizeof(line), configFile) == NULL) { if (feof(configFile)) break; __pmNotifyErr(LOG_ERR, "event_config: fgets: %s", strerror(errno)); sts = -1; break; } /* * fgets() puts the '\n' at the end of the buffer. Remove * it. If it isn't there, that must mean that the line is * longer than our buffer. */ len = strlen(line); if (len == 0) /* Ignore empty strings. */ continue; if (line[len - 1] != '\n') { /* String must be too long */ __pmNotifyErr(LOG_ERR, "event_config: config line too long: '%s'", line); sts = -1; break; } line[len - 1] = '\0'; /* Remove the '\n'. */ /* Strip all trailing whitespace. */ rstrip(line); /* If the string is now empty or a comment, just ignore the line. */ len = strlen(line); if (len == 0) continue; if (line[0] == '#') continue; /* Skip past all leading whitespace to find the start of * NAME. */ ptr = name = lstrip(line); /* Now we need to split the line into 3 parts: NAME, ACCESS * and PATHNAME. NAME can't have whitespace in it, so look * for the first non-whitespace. */ while (*ptr != '\0' && ! isspace((int)*ptr)) { ptr++; } /* If we're at the end, we didn't find any whitespace, so * we've only got a NAME, with no ACCESS/PATHNAME. */ if (*ptr == '\0') { __pmNotifyErr(LOG_ERR, "event_config: badly formatted " " configuration file line: '%s'", line); sts = -1; break; } /* Terminate NAME at the 1st whitespace. */ *ptr++ = '\0'; /* Make sure NAME isn't too long. */ if (strlen(name) > MAXPATHLEN) { __pmNotifyErr(LOG_ERR, "event_config: name too long: '%s'", name); sts = -1; break; } /* Make sure NAME is valid. */ if (valid_pmns_name(name) == 0) { __pmNotifyErr(LOG_ERR, "event_config: invalid name: '%s'", name); sts = -1; break; } /* Skip past any extra whitespace between NAME and ACCESS */ ptr = noaccess = lstrip(ptr); /* Look for the next whitespace, and that terminate ACCESS */ while (*ptr != '\0' && ! isspace((int)*ptr)) { ptr++; } /* If we're at the end, we didn't find any whitespace, so * we've only got NAME and ACCESS with no/PATHNAME. */ if (*ptr == '\0') { __pmNotifyErr(LOG_ERR, "event_config: badly formatted " " configuration file line: '%s'", line); sts = -1; break; } /* Terminate ACCESS at the 1st whitespace. */ *ptr++ = '\0'; /* Skip past any extra whitespace between ACCESS and PATHNAME */ ptr = lstrip(ptr); /* Make sure PATHNAME (the rest of the line) isn't too long. */ if (strlen(ptr) > MAXPATHLEN) { __pmNotifyErr(LOG_ERR, "event_config: path is too long: '%s'", ptr); sts = -1; break; } /* Now we've got a reasonable NAME/ACCESS/PATHNAME. Save them. */ len = (numlogfiles + 1) * sizeof(event_logfile_t); logfiles = realloc(logfiles, len); if (logfiles == NULL) { __pmNoMem("event_config", len, PM_FATAL_ERR); sts = -1; break; } logfile = &logfiles[numlogfiles]; memset(logfile, 0, sizeof(*logfile)); logfile->noaccess = (noaccess[0] == 'y' || noaccess[0] == 'Y'); strncpy(logfile->pmnsname, name, sizeof(logfile->pmnsname)); logfile->pmnsname[sizeof(logfile->pmnsname)-1] = '\0'; strncpy(logfile->pathname, ptr, sizeof(logfile->pathname)); logfile->pathname[sizeof(logfile->pathname)-1] = '\0'; /* remaining fields filled in after pmdaInit() is called. */ numlogfiles++; if (pmDebug & DBG_TRACE_APPL0) __pmNotifyErr(LOG_INFO, "event_config: new logfile %s (%s)", logfile->pathname, logfile->pmnsname); } fclose(configFile); if (sts < 0) { free(logfiles); return sts; } if (numlogfiles == 0) { __pmNotifyErr(LOG_ERR, "event_config: no valid log files found"); return -1; } return numlogfiles; }
static void LoadConfigFromFile(FILE *f) { char curSection[128] = ""; char *buffer = NULL; size_t maxlen = 0; ConfigEntry *ent; while(readline(f, &buffer, &maxlen)) { char *line, *comment; char key[256] = ""; char value[256] = ""; comment = strchr(buffer, '#'); if(comment) *(comment++) = 0; line = rstrip(lstrip(buffer)); if(!line[0]) continue; if(line[0] == '[') { char *section = line+1; char *endsection; endsection = strchr(section, ']'); if(!endsection || section == endsection || endsection[1] != 0) { ERR("config parse error: bad line \"%s\"\n", line); continue; } *endsection = 0; if(strcasecmp(section, "general") == 0) curSection[0] = 0; else { strncpy(curSection, section, sizeof(curSection)-1); curSection[sizeof(curSection)-1] = 0; } continue; } if(sscanf(line, "%255[^=] = \"%255[^\"]\"", key, value) == 2 || sscanf(line, "%255[^=] = '%255[^\']'", key, value) == 2 || sscanf(line, "%255[^=] = %255[^\n]", key, value) == 2) { /* sscanf doesn't handle '' or "" as empty values, so clip it * manually. */ if(strcmp(value, "\"\"") == 0 || strcmp(value, "''") == 0) value[0] = 0; } else if(sscanf(line, "%255[^=] %255[=]", key, value) == 2) { /* Special case for 'key =' */ value[0] = 0; } else { ERR("config parse error: malformed option line: \"%s\"\n\n", line); continue; } rstrip(key); if(curSection[0] != 0) { size_t len = strlen(curSection); memmove(&key[len+1], key, sizeof(key)-1-len); key[len] = '/'; memcpy(key, curSection, len); } /* Check if we already have this option set */ ent = cfgBlock.entries; while((unsigned int)(ent-cfgBlock.entries) < cfgBlock.entryCount) { if(strcasecmp(ent->key, key) == 0) break; ent++; } if((unsigned int)(ent-cfgBlock.entries) >= cfgBlock.entryCount) { /* Allocate a new option entry */ ent = realloc(cfgBlock.entries, (cfgBlock.entryCount+1)*sizeof(ConfigEntry)); if(!ent) { ERR("config parse error: error reallocating config entries\n"); continue; } cfgBlock.entries = ent; ent = cfgBlock.entries + cfgBlock.entryCount; cfgBlock.entryCount++; ent->key = strdup(key); ent->value = NULL; } free(ent->value); ent->value = expdup(value); TRACE("found '%s' = '%s'\n", ent->key, ent->value); } free(buffer); }
// strip from both ends inline std::string &strip(std::string &s) { return lstrip(rstrip(s)); }