size_t nab_next_line(int const fd, char **line, char **spill) { int ret; char buf[BUF_SIZE + 1]; size_t spill_len; size_t line_len; char *spill_temp; spill_temp = *spill; spill_len = ft_strlen(*spill); if ((spill_len > 0) & ((line_len = ft_strlen(*line = str_slice(*spill, '\n'))) < spill_len)) return (line_len); while ((ret = read(fd, buf, BUF_SIZE)) > 0) { buf[ret] = '\0'; if ((ret == 0) || (buf[ret - 1] == '\0' && buf[ret - 2] != '\n')) return (0); if (slice_and_stick(line, &line_len, str_slice(buf, '\n')) < (size_t)ret) break ; } if (ret == -1) exit(ft_puterror("read()", "line N/A")); *spill = ft_strdup(buf); free(spill_temp); return (line_len); }
int proc_getrange(NetworkServer *net, Link *link, const Request &req, Response *resp){ SSDBServer *serv = (SSDBServer *)net->data; CHECK_NUM_PARAMS(2); CHECK_KEY_RANGE(1); const Bytes &key = req[1]; int start = 0; if(req.size() > 2){ start = req[2].Int(); } int size = -1; if(req.size() > 3){ size = req[3].Int(); } std::string val; int ret = serv->ssdb->get(key, &val); if(ret == -1){ resp->push_back("error"); }else{ std::string str = str_slice(val, start, size); resp->push_back("ok"); resp->push_back(str); } return 0; }
static int proc_redis_getrange(Server *serv, Link *link, const Request &req, Response *resp){ if(req.size() < 2){ resp->push_back("client_error"); }else{ const Bytes &key = req[1]; int start = 0; if(req.size() > 2){ start = req[2].Int(); } int size = -1; if(req.size() > 3){ size = req[3].Int(); } std::string val; int ret = serv->ssdb->get(key, &val); if(ret == -1){ resp->push_back("error"); }else{ std::string str = str_slice(val, start, size); resp->push_back("ok"); resp->push_back(str); } } return 0; }
void test_str_slice(void) { str src; str dst; str_create_from_cstr(&src, "test-data-a"); str_create(&dst); str_slice(&dst, &src, 0, 9); CU_ASSERT(dst.len == 9); CU_ASSERT_NSTRING_EQUAL(dst.data, "test-data", 9); str_free(&src); str_free(&dst); }
/** ** Explode a string by separator ** Once the needle is found, slice until the end to show the resulting string */ char *str_explode(char *subject,char *separator) { char *buffer,*match; int i,j=0,size; int found=0; if( strlen(subject)>0 ) { buffer = (char*) malloc(sizeof(char)*strlen(subject)); memcpy(buffer,subject,strlen(subject)+1); size = strlen(buffer); for(i=0;i<size||found!=1;i++) { if( buffer[i]==*separator ){ /** ** The maximum size is now from the place i found the string until the maximum lenght of the string ** Generate a new string with space for size-i chars */ match = (char*) malloc(sizeof(char)*(strlen(buffer)-i)); found = 1; str_slice(buffer,i,match); } } } return match; }
int main() { char data[1024]; char *buf[8]; //目前有8行数据,指针数组,每一个数组元素都指向一行字符串 FILE *fp; int index = 0; int line = 0; int i; //g_ARR_A0 if((fp = fopen("/home/work/1.cfg", "a+")) == NULL) { perror("write2file"); return -1; } while(fgets(data, 1024, fp) != NULL) { for(i = 0; i < strlen(data); i++) { if(data[i] == '\r' || data[i] == '\n' || data[i] == '#') { data[i] = '\0'; break; } } printf("strlen[data]: %d, fgets: %s\n", strlen(data), data); printf("index: %d, line: %d\n", index, line); if(line > 0) { buf[index] = malloc(strlen(data) + 1); strcpy(buf[index], data); str_trim(buf[index]); printf("buf[%d]: %s\n", index, buf[index]); line--; index++; } if(strcmp(data, "A0:") == 0){ line = 1; } if(strcmp(data, "A1:") == 0){ line = 1; } if(strcmp(data, "A2:") == 0){ line = 2; } if(strcmp(data, "A3:") == 0){ line = 2; } if(strcmp(data, "A4:") == 0){ line = 1; } if(strcmp(data, "A5:") == 0){ line = 1; } } fclose(fp); printf("result: \n"); for(i = 0; i < 8; i++) { printf("%s\n", buf[i]); } //心跳间隔数组 hexStringToBytes(buf[0], g_ARR_A0); //心跳内容数组,数字+普通字符(ID+车站名) char res[64]; str_slice(buf[1], 0, 6, res); //AS,SS赋值 hexStringToBytes(res, g_ARR_A1); //ID,车站名 str_slice(buf[1], 6, strlen(buf[1]), res); i = 0; while(res[i] != '\0') { g_ARR_A1[i + 3] = res[i]; i++; } //二维数组 hexStringToBytes(buf[2], g_ARR_A2[0]); hexStringToBytes(buf[3], g_ARR_A2[1]); //二维数组 hexStringToBytes(buf[4], g_ARR_A3[0]); hexStringToBytes(buf[5], g_ARR_A3[1]); //两路can的控制参数 hexStringToBytes(buf[6], g_ARR_A4); hexStringToBytes(buf[7], g_ARR_A5); /*u8 g_ARR_A0[2]; //心跳间隔,初始值 u8 g_ARR_A1[64]; //心跳帧内容初始值 u8 g_ARR_A2[2][64]; //CAN1,设备列表 u8 g_ARR_A3[2][64]; //CAN2,设备列表 u8 g_ARR_A4[4]; //CAN1, 设备控制参数初始值 u8 g_ARR_A5[4]; //CAN2,设备控制参数初始值*/ printf("g_ARR_A0: "); print_arr(g_ARR_A0, 2); printf("g_ARR_A1: "); print_arr(g_ARR_A1, 64); printf("g_ARR_A4: "); print_arr(g_ARR_A4, 4); printf("g_ARR_A5: "); print_arr(g_ARR_A5, 4); return 0; }
void newsroom_fixup_str(newsroom_local *local) { /* * Substitution table 1= Player1 - Crystal 2= Player2 - Steffan 3= HAR1 - jaguar 4= HAR2 - shadow 5= Arena - power plant 6= His/Her P1 - Her 7= Him/Her P1 - Her 8= He/She P1 - She 10= Him/Her P2 - Him 11= He/She P2 - He */ const char *text = NULL; if(local->screen == 0) { text = lang_get(NEWSROOM_TEXT+local->news_id); } else { text = lang_get(NEWSROOM_TEXT+local->news_id+1); } str textstr; size_t prevpos=0, pos = 0; str_create_from_cstr(&textstr, text); str_free(&local->news_str); while(str_next_of(&textstr, '~', &pos)) { str tmp; str_create(&tmp); str_slice(&tmp, &textstr, prevpos, pos); str_append(&local->news_str, &tmp); str_free(&tmp); // replace ~n tokens char n = str_at(&textstr, pos+1); char nn = str_at(&textstr, pos+2); switch(n) { case '1': if(nn == '0') { // ~10 str_append_c(&local->news_str, object_pronoun(local->sex2)); pos++; } else if(nn == '1') { // ~11 str_append_c(&local->news_str, subject_pronoun(local->sex2)); pos++; } else { // ~1 str_append(&local->news_str, &local->pilot1); } break; case '2': str_append(&local->news_str, &local->pilot2); break; case '3': str_append(&local->news_str, &local->har1); break; case '4': str_append(&local->news_str, &local->har2); break; case '5': str_append_c(&local->news_str, "Stadium"); break; case '6': str_append_c(&local->news_str, "The"); break; case '7': str_append_c(&local->news_str, object_pronoun(local->sex1)); break; case '8': str_append_c(&local->news_str, subject_pronoun(local->sex1)); break; case '9': str_append_c(&local->news_str, "WTF"); break; } pos+=2; prevpos = pos; } str tmp; str_create(&tmp); str_slice(&tmp, &textstr, pos, str_size(&textstr)); str_append(&local->news_str, &tmp); str_free(&tmp); str_free(&textstr); }