END_TEST START_TEST(test_str_ltrim) { str_t *str = str_from_cstr(" \n\r123"); str_ltrim(str); CHECK_STR(str, >= 3, == 3, "123"); str_free(str); str = str_new(0); str_ltrim(str); CHECK_STR(str, == STR_DEFAULT_CAPACITY, == 0, ""); str_free(str); }
int main(int argc, char **argv) { struct stem_cache *stem = stem_cache_new(stem_porters, NULL, 200); char buf[BUFSIZ + 1]; unsigned int i; buf[BUFSIZ] = '\0'; if (!stem) { return EXIT_FAILURE; } if (argc == 1) { while (fgets(buf, BUFSIZ, stdin)) { char *start; str_rtrim(buf); start = (char *) str_ltrim(buf); stem_cache_stem(stem, start); printf("%s\n", start); } } else { for (i = 1; i < argc; i++) { FILE *input = fopen(argv[i], "rb+"); if (!input) { fprintf(stderr, "failed to open file %s: %s\n", argv[i], strerror(errno)); stem_cache_delete(stem); return EXIT_FAILURE; } while (fgets(buf, BUFSIZ, input)) { char *start; str_rtrim(buf); start = (char *) str_ltrim(buf); stem_cache_stem(stem, start); printf("%s\n", start); } fclose(input); } } stem_cache_delete(stem); return EXIT_SUCCESS; }
static int server_try_setclientname(int client_id, const char *name) { int i; char trimmed_name[64]; /* trim the name */ str_copy(trimmed_name, str_ltrim(name), sizeof(trimmed_name)); str_rtrim(trimmed_name); dbg_msg("", "'%s' -> '%s'", name, trimmed_name); name = trimmed_name; /* check for empty names */ if(!name[0]) return -1; /* make sure that two clients doesn't have the same name */ for(i = 0; i < MAX_CLIENTS; i++) if(i != client_id && clients[i].state >= SRVCLIENT_STATE_READY) { if(strcmp(name, clients[i].name) == 0) return -1; } /* set the client name */ str_copy(clients[client_id].name, name, MAX_NAME_LENGTH); return 0; }
void getSquidConf() { FILE* confFile; FILE* squidFile; char line[BUFSIZ]; if ((confFile = fopen("../etc/monitor.conf", "r")) == NULL) { perror("monitor.conf"); exit(1); } while (fgets(line, BUFSIZ, confFile) != NULL) { str_ltrim(line, "\t "); str_rtrim(line, "\t\r\n "); if (!strncmp(conf_deviceName, line, strlen(conf_deviceName))) { int i = 0; while (conf_device[i] = line[i + 1 + strlen(conf_deviceName)]) { i++; } //printf("%s\n", conf_device); } } }
void macro_ltrim(macro **mp) { macro *m; while ((m = *mp)) { if (m->type != MACRO_STR) break; str_ltrim(&m->str); if (m->str) break; *mp = m->next; m->next = 0; macro_free(m); } }
void ConfigFile::_open(void) { std::ifstream ifs(_filename); _sections.clear(); if (not ifs.is_open()) return ; std::string section; std::string line; std::size_t idx; while (std::getline(ifs, line)) { str_trim(line); if (line.empty()) continue ; if(line.front() == '[' and line.back() == ']') { section = line.substr(1, line.size() - 2); str_trim(section); } else if ((idx = line.find("=")) != std::string::npos) { std::string key(line.substr(0, idx - 1)); std::string value(line.substr(idx + 1)); _sections[section][str_rtrim(key)] = str_ltrim(value); } else { warn(std::string("In `") + _filename + "`: badly formated: `" + line + "`"); } } ifs.close(); }
char * str_trim(char *str) { char *ptr; ptr = str_rtrim(str); str = str_ltrim(ptr); return str; }
CHAR* str_trim(register CHAR* s) { s = str_ltrim(s); return str_rtrim(s); }
/* * strim, karakter dizisinin sagindaki ve solundaki * bosluklari kaldirir. * * @param s : karakter dizisi */ char *strim(char *s){ return str_ltrim(str_rtrim(s)); }
int test_file(FILE *fp, int argc, char **argv) { char buf[65535 + 1]; char *pos; unsigned int strategy = 0; /* what bucketing strategy we're using */ void *ptr = NULL; unsigned int bucketsize = 0; struct params params = {0}; struct chash *hash = NULL; char name[256]; if (!parse_params(argc, argv, ¶ms)) { fprintf(stderr, "failed to parse params\n"); return 0; } while (fgets((char *) buf, 65535, fp)) { str_rtrim(buf); pos = (char *) str_ltrim(buf); if (!str_casecmp(pos, "new")) { /* creating a new bucket */ unsigned int size = -1; if (ptr) { chash_delete(hash); free(ptr); } /* read parameters */ if ((fscanf(fp, "%255s %u %u", name, &strategy, &size) == 3) && (size <= 65535) && (bucketsize = size) && (ptr = malloc(size)) && (hash = chash_ptr_new(1, 2.0, /* some fn pointer casting dodginess */ (unsigned int (*)(const void *)) str_len, (int (*)(const void *, const void *)) str_cmp)) && (bucket_new(ptr, bucketsize, strategy))) { /* succeeded, do nothing */ if (params.verbose) { printf("%s: new bucket with size %u strategy %u\n", name, size, strategy); } } else { fprintf(stderr, "%s: failed to create bucket\n", name); return 0; } } else if (!str_casecmp(pos, "add")) { /* adding a term to the bucket */ void *ret; unsigned int veclen, succeed, len; int toobig; if (!ptr) { return 0; } /* read parameters */ if ((fscanf(fp, "%65535s %u %u", buf, &veclen, &succeed) == 3) && (veclen <= 65535)) { len = str_len(buf); if ((((ret = bucket_alloc(ptr, bucketsize, strategy, buf, len, veclen, &toobig, NULL)) && succeed) || (!ret && !succeed))) { /* do nothing */ if (params.verbose) { printf("%s: added term '%s'\n", name, buf); } } else if (succeed) { fprintf(stderr, "%s: failed to add '%s' to bucket\n", name, buf); return 0; } else if (!succeed) { fprintf(stderr, "%s: add '%s' succeeded but shouldn't " "have\n", name, buf); return 0; } } else { fprintf(stderr, "%s: failed to add\n", name); return 0; } } else if (!str_casecmp(pos, "ls")) { /* matching stuff in the bucket */ unsigned int numterms, i, len, veclen, veclen2, state; void *addr; struct chash *tmphash; const char *term; void **tmpptr, *tmp; if (!ptr) { return 0; } if (!(tmphash = chash_ptr_new(1, 2.0, /* some fn pointer casting dodginess */ (unsigned int (*)(const void *)) str_len, (int (*)(const void *, const void *)) str_cmp))) { fprintf(stderr, "%s: failed to init hashtable\n", name); return 0; } /* first, fill hashtable with all terms from bucket */ state = 0; while ((term = bucket_next_term(ptr, bucketsize, strategy, &state, &len, &addr, &veclen))) { if (!((term = str_ndup(term, len)) && (chash_ptr_ptr_insert(tmphash, term, (void*) term) == CHASH_OK))) { fprintf(stderr, "%s: failed to init hashtable\n", name); return 0; } } /* now, take terms from file, comparing them with hashtable * entries */ if (fscanf(fp, "%u", &numterms)) { for (i = 0; i < numterms; i++) { if (fscanf(fp, "%65535s %u ", buf, &veclen)) { if (params.verbose) { printf("%s: ls checking %s\n", name, buf); } if ((addr = bucket_find(ptr, bucketsize, strategy, buf, str_len(buf), &veclen2, NULL)) /* remove it from hashtable */ && chash_ptr_ptr_find(tmphash, buf, &tmpptr) == CHASH_OK && chash_ptr_ptr_remove(tmphash, *tmpptr, &tmp) == CHASH_OK && (free(tmp), 1) && (veclen <= 65535) && (veclen2 == veclen) && fread(buf, veclen, 1, fp) && ((buf[veclen] = '\0'), 1) && (!params.verbose || printf("%s: ls check read '%s'\n", name, buf)) && !memcmp(buf, addr, veclen)) { /* do nothing */ } else { unsigned int j; fprintf(stderr, "%s: ls failed cmp '%s' with '", name, buf); for (j = 0; j < veclen; j++) { putc(((char *) addr)[j], stderr); } fprintf(stderr, "'\n"); return 0; } } else { fprintf(stderr, "%s: ls failed\n", name); return 0; } } if (chash_size(tmphash)) { fprintf(stderr, "%s: ls failed\n", name); return 0; } } else { fprintf(stderr, "%s: ls failed\n", name); return 0; } chash_delete(tmphash); if (params.verbose) { printf("%s: matched all (%u) entries\n", name, numterms); } } else if (!str_casecmp(pos, "set")) { /* setting the vector for a term in the bucket */ unsigned int veclen, reallen; void *addr; if (!ptr) { return 0; } /* read parameters */ if ((fscanf(fp, "%65535s %u ", buf, &veclen) == 2) && (veclen <= 65535)) { addr = bucket_find(ptr, bucketsize, strategy, buf, str_len(buf), &reallen, NULL); if (addr && (reallen == veclen) && fread(addr, 1, veclen, fp)) { /* do nothing */ if (params.verbose) { unsigned int j; printf("%s: set term '%s' to '", name, buf); for (j = 0; j < reallen; j++) { putc(((char *) addr)[j], stdout); } printf("'\n"); } } else { fprintf(stderr, "%s: failed to set!\n", name); return 0; } } else { fprintf(stderr, "%s: failed to set\n", name); return 0; } } else if (!str_casecmp(pos, "realloc")) { /* reallocating a term in the bucket */ unsigned int veclen, succeed; int toobig; if (!ptr) { return 0; } /* read parameters */ if ((fscanf(fp, "%65535s %u %u", buf, &veclen, &succeed) == 3) && (veclen <= 65535)) { if (!bucket_realloc(ptr, bucketsize, strategy, buf, str_len(buf), veclen, &toobig)) { fprintf(stderr, "%s: failed to realloc!\n", name); return 0; } } else { fprintf(stderr, "%s: failed to realloc\n", name); return 0; } if (params.verbose) { printf("%s: realloc'd term '%s'\n", name, buf); } } else if (!str_casecmp(pos, "rm")) { /* removing something from the bucket */ unsigned int succeed; if (!ptr) { return 0; } if (fscanf(fp, "%65535s %u", buf, &succeed) == 2) { if (succeed) { if (!(bucket_remove(ptr, bucketsize, strategy, buf, str_len(buf)))) { fprintf(stderr, "%s: failed to rm '%s'\n", name, buf); return 0; } else if (params.verbose) { printf("%s: rm term '%s'\n", name, buf); } } else if (succeed) { fprintf(stderr, "%s: failed to rm\n", name); return 0; } } else { fprintf(stderr, "%s: failed to rm\n", name); return 0; } } else if (!str_casecmp(pos, "print")) { /* printing out the bucket contents */ unsigned int state = 0, len, veclen; const char *term; char format[100]; void *addr; if (!ptr) { printf("can't print, no bucket\n"); } else { do { term = bucket_next_term(ptr, bucketsize, strategy, &state, &len, &addr, &veclen); } while (term && memcpy(buf, term, len) && ((buf[len] = '\0') || 1) && snprintf(format, 100, "%%.%us (%%u): '%%.%us' (%%u) " "(off %%u)\n", len, veclen) && printf(format, term, len, (char*) addr, veclen, ((char *) addr) - (char *) ptr)); if (!state) { printf("(empty)\n"); } printf("%u entries, %u data, %u string, %u overhead, %u free\n", bucket_entries(ptr, bucketsize, strategy), bucket_utilised(ptr, bucketsize, strategy), bucket_string(ptr, bucketsize, strategy), bucket_overhead(ptr, bucketsize, strategy), bucket_unused(ptr, bucketsize, strategy)); } } else if (!str_casecmp(pos, "match")) { unsigned int veclen, veclen2; void *addr; if (fscanf(fp, "%65535s %u ", buf, &veclen)) { if ((addr = bucket_find(ptr, bucketsize, strategy, buf, str_len(buf), &veclen2, NULL)) && (veclen <= 65535) && (veclen2 >= veclen) && (!params.verbose || printf("%s: match on '%s' ", name, buf)) && fread(buf, veclen, 1, fp) && !memcmp(buf, addr, veclen)) { if (params.verbose) { printf("content succeeded\n"); } } else { fprintf(stderr, "%s: match failed (%s vs %s)\n", name, buf, (char *) addr); return 0; } } else { fprintf(stderr, "%s: match failed\n", name); return 0; } } else if ((*pos != '#') && str_len(pos)) { fprintf(stderr, "%s: unknown command '%s'\n", name, pos); return 0; } } if (ptr) { chash_delete(hash); free(ptr); } return 1; }
/** * Removes leading and trailing characters from the specified character string * * @param str * [IN/OUT] string for processing * @param charlist * [IN] null terminated list of characters */ void str_lrtrim(char *str, const char *charlist) { str_rtrim(str, charlist); str_ltrim(str, charlist); }
void str_trim(char *str) { str_rtrim(str); str_ltrim(str); }
int str_trim(char* str) { str_ltrim(str); str_rtrim(str); return 0; }
main(int argc, char** argv) { int filedes1[2], filedes2[2]; int pid; FILE* ifTestFile; char line[BUFSIZ]; int ethNum = 0; int RxNum = 0; time_t updateTime; FILE* poolFile; FILE* RxTxFile; char strLine[BUFSIZ]; unsigned long dataCount = 0; float timeCount = 0.0; float inBandSpeed = 0.0; float outBandSpeed = 0.0; const char* ethName = "eth"; const char* RxBytesName = "RX bytes:"; char RxBytes[BUFSIZ]; char TxBytes[BUFSIZ]; char newUpdateTime[BUFSIZ]; char oldRxBytes[BUFSIZ]; char oldTxBytes[BUFSIZ]; char oldUpdateTime[BUFSIZ]; getSquidConf(); if (pipe(filedes1) == -1) { perror ("pipe"); exit(1); } if (pipe(filedes2) == -1) { perror ("pipe"); exit(1); } if ((pid = fork()) == 0) { dup2(filedes1[0], fileno(stdin)); /* Copy the reading end of the pipe. */ dup2(filedes2[1], fileno(stdout)); /* Copy the writing end of the pipe */ /* Uncomment this if you want stderr sent too. dup2(filedes2[1], fileno(stderr)); */ /* If execl() returns at all, there was an error. */ if (execl("/sbin/ifconfig", "ifconfig","-a", (char*)0)) { perror("execl"); exit(128); } } else if (pid == -1) { perror("fork"); exit(128); } else { FILE *program_input, *program_output, *output_file; int c; if ((program_input = fdopen(filedes1[1], "w")) == NULL) { perror("fdopen"); exit(1); } if ((program_output = fdopen(filedes2[0], "r")) == NULL) { perror("fdopen"); exit(1); } if ((output_file = fopen("/tmp/ifconfig.test", "w+")) == NULL) { perror ("ifconfig.test"); exit(1); } //fputs(argv[2], program_input); /* Write the string */ while ( (c = getc(program_output)) != EOF) { fputc(c, output_file); //printf("%c",c); close(filedes2[0]); } fclose(output_file); //Process ifconfig.test file if ((ifTestFile = fopen("/tmp/ifconfig.test", "r")) == NULL) { perror("ifTestFile"); exit(1); } while (fgets(line, BUFSIZ, ifTestFile) != NULL) { str_ltrim(line, "\t\r\n "); //include" "(space) to trim left space str_rtrim(line, "\t\r\n"); //if (!strncmp(ethName, line, strlen(ethName))) if (!strncmp(conf_device, line, strlen(conf_device))) { ethNum += 1; } if (!strncmp(RxBytesName, line, strlen(RxBytesName))) { RxNum += 1; if (ethNum == RxNum && RxNum == 1) { int i = 0; char* p; while ((RxBytes[i] = line[i + strlen(RxBytesName)]) != ' ') { i++; } str_ltrim(RxBytes, " "); str_rtrim(RxBytes, " "); //printf("%s\n", RxBytes); p = strrchr(line, ':'); i = 0; while ((TxBytes[i] = p[i + 1]) != ' ') { i++; } str_ltrim(TxBytes, " "); str_rtrim(TxBytes, " "); //printf("%s\n", TxBytes); //**************************************************** //Get UpdateTime updateTime = time(NULL); //******************************************************* // Read old Rx, Tx data if ((RxTxFile = fopen("/tmp/netBandwidth.dat", "r")) == NULL) { //perror("RxTx.dat"); //exit(1); } else { while (fgets(line, BUFSIZ, RxTxFile) != NULL) { str_ltrim(line, "\t "); str_rtrim(line, "\t\r\n "); if (!strncmp("RX", line, strlen("RX"))) { int i = 0; while (oldRxBytes[i] = line[i + strlen("RX")]) { i++; } //printf("%s\n", oldRxBytes); } if (!strncmp("TX", line, strlen("TX"))) { int i = 0; while (oldTxBytes[i] = line[i + strlen("TX")]) { i++; } //printf("%s\n", oldTxBytes); } if (!strncmp("UT", line, strlen("UT"))) { int i = 0; while (oldUpdateTime[i] = line[i + strlen("UT")]) { i++; } //printf("%s\n", oldUpdateTime); } } fclose(RxTxFile); } //******************************************************* //Write To the RxTx.dat if ((RxTxFile = fopen("/tmp/netBandwidth.dat", "w+")) == NULL) { perror("RxTx.dat"); exit(1); } sprintf(strLine,"RX"); fputs(strLine, RxTxFile); sprintf(strLine, "%s\n", RxBytes); fputs(strLine, RxTxFile); sprintf(strLine, "TX"); fputs(strLine, RxTxFile); sprintf(strLine, "%s\n", TxBytes); fputs(strLine, RxTxFile); sprintf(strLine,"UT"); fputs(strLine, RxTxFile); sprintf(strLine, "%ld\n", updateTime); fputs(strLine, RxTxFile); fclose(RxTxFile); //******************************************************* //Write To the netBandwidthMon.pool if ((poolFile = fopen("../pool/netBandwidthMon.pool", "w+")) == NULL) { perror("netBandwidthMon.pool"); exit(1); } //printf("%s",RxBytes); //printf("%s\n",oldRxBytes); //unsigned long temp =atoll(RxBytes); //printf("%u\n",temp); dataCount = atoll(RxBytes)-atoll(oldRxBytes); //printf("%u\n",dataCount); sprintf(strLine,"%ld\n",updateTime); //printf("%ld\n",updateTime); //printf("%d\n",atoi(strLine)-atoi(oldUpdateTime)); timeCount = atoi(strLine)-atoi(oldUpdateTime); if(timeCount > 0) { inBandSpeed = dataCount / timeCount / 1024; } else { inBandSpeed = 0; } //printf("%f\n", inBandSpeed); dataCount = atoll(TxBytes)-atoll(oldTxBytes); if(timeCount > 0) { outBandSpeed = dataCount / timeCount / 1024; } else { outBandSpeed = 0; } sprintf(strLine, "%.2f\n", inBandSpeed); fputs(strLine, poolFile); sprintf(strLine, "%.2f\n", outBandSpeed); fputs(strLine, poolFile); sprintf(strLine, "%ld\n", updateTime); fputs(strLine, poolFile); fclose(poolFile); }//if (ethNum == RxNum) } } // exit(0); } }
char *str_trim(char *string, const char character) { return str_rtrim(str_ltrim(string, character), character); }
const char *p_str_ltrim(pool_t pool, const char *str, const char *chars) { return p_strdup(pool, str_ltrim(str, chars)); }
const char *t_str_ltrim(const char *str, const char *chars) { return t_strdup(str_ltrim(str, chars)); }
void test_css_util_other() { char str[24]; char test[10]; char* s = NULL; char* t = NULL; assert(strcmp("922337203685477587",lltoa(922337203685477587,str,10)) == 0); strcpy(test," abc"); str_trim(test); assert( 0 == strcmp(test,"abc")); strcpy(test," abc "); str_trim(test); assert( 0 == strcmp(test,"abc")); strcpy(test,"abc "); str_trim(test); assert( 0 == strcmp(test,"abc")); strcpy(test," a b c "); str_trim(test); assert( 0 == strcmp(test,"a b c")); strcpy(test," "); str_trim(test); assert( 0 == strcmp(test,"")); strcpy(test," abc"); str_ltrim(test); assert( 0 == strcmp(test,"abc")); strcpy(test," abc "); str_rtrim(test); assert( 0 == strcmp(test," abc")); strcpy(test," abc "); str_ltrim(test); assert( 0 == strcmp(test,"abc ")); strcpy(test," a b c "); str_ltrim(test); assert( 0 == strcmp(test,"a b c ")); strcpy(test," "); str_ltrim(test); assert( 0 == strcmp(test,"")); strcpy(test," "); str_rtrim(test); assert( 0 == strcmp(test,"")); memset(test,' ',10); strcpy(test,"a=b"); get_split_str(test,"=",1,&s); assert( 0 == strcmp(s,"b")); FREE(s); get_split_str(test,"=",0,&s); assert( 0 == strcmp(s,"a")); FREE(s); get_split_strs(test,"=",&s,&t); assert( 0 == strcmp(s,"a")); assert( 0 == strcmp(t,"b")); FREE(s); FREE(t); memset(test,' ',10); strcpy(test," a = b "); get_split_str(test,"=",1,&s); str_trim(s); assert( 0 == strcmp(s,"b")); FREE(s); get_split_str(test,"=",0,&s); str_trim(s); assert( 0 == strcmp(s,"a")); FREE(s); get_split_strs(test,"=",&s,&t); str_trim(s); str_trim(t); assert( 0 == strcmp(s,"a")); assert( 0 == strcmp(t,"b")); FREE(s); FREE(t); assert(0 == ensure_dir("./test/dir")); assert(0 == ensure_dir("./test/dir/")); }