enum pubnub_res pbcc_parse_publish_response(struct pbcc_context *p) { char *reply = p->http_reply; int replylen = p->http_buf_len; if (replylen < 2) { return PNR_FORMAT_ERROR; } if ((reply[0] != '[') || (reply[replylen-1] != ']')) { return PNR_FORMAT_ERROR; } p->chan_ofs = p->chan_end = 0; p->msg_ofs = p->msg_end = 0; reply[replylen-1] = '\0'; if (split_array(reply + 1)) { if (1 != strtol(reply+1, NULL, 10)) { return PNR_PUBLISH_FAILED; } return PNR_OK; } else { return PNR_FORMAT_ERROR; } }
int* Quick_Sort(int* input_array, int start, int end) { int pivot; if(end>start) { pivot = split_array(input_array, start, end); Quick_Sort(input_array,start,pivot-1); Quick_Sort(input_array,pivot+1,end); } return input_array; }
void makeup_data(int id,int len) { switch(id) { case 1: split_array(len); //use to split array and use different proto to send break; case 2: case 3: cleanup_array(len,id); break; } }
static void calculate_mem(void) { FILE *f, *p; char buf[150]; char p1[50]; int n; if(file_exists("/proc/meminfo")) { f=fopen("/proc/meminfo", "r"); while(fgets(buf, sizeof(buf) - 1, f)) { trim(buf); if(split_array(buf," ",&chargv) > 0) { if(chargv[0]!=NULL && chargv[1]!=NULL) { if(!strncmp(chargv[0],"MemTotal:",9)) FOUNDMEM=atoi(chargv[1]); if(!strncmp(chargv[0],"MemFree:",8)) MEMFREE=atoi(chargv[1]); if(!strncmp(chargv[0],"Cached:",7)) MEMCACHE=atoi(chargv[1]); } } } fclose(f); } if(FOUNDMEM==0) { fprintf_stdout("**** MEMORY COUNTING FAILED! ****\n"); exit(1); } if(file_exists("/proc/kcore")) { memset(buf,0x0,sizeof(buf)); memset(p1,0x0,sizeof(p1)); p=popen("du /proc/kcore","r"); fgets(buf, sizeof(buf) - 1, p); if(buf[0]!='\0') { splitc(p1,buf,' '); n=atoi(p1); if(n!=0) fprintf_stdout("* Physical memory size: %d kB\n",n); if(n!=0 && n < 184324) { fprintf_stdout("**** NOT ENOUGH MEMORY (%d kB) ****\n",n); exit(1); } } pclose(p); } TOTALMEM=MEMFREE + MEMCACHE; fprintf_stdout("* Total memory found: %d kB\n",FOUNDMEM); do_spin("* Initializing.."); MAXSIZE=TOTALMEM - MINLEFT; RAMSIZE=TOTALMEM / 5; if(TOTALMEM > MINLEFT) { if(RAMSIZE < 0 ) RAMSIZE=65536; RAMSIZE=RAMSIZE * 4; } }
static void read_cmdline(void) { char buf[550], name[150]; char **xchargv; int n, k; read_oneline("/proc/cmdline",buf); trim(buf); if(split_array(buf," ",&chargv) > 0) { k=numtokens; for(n=0;n < k; n++) { memset(name,0x0,sizeof(name)); snprintf(name,sizeof(name),"%s",chargv[n]); if(split_array(name,"=",&xchargv) > 0) { if(xchargv[0]!=NULL && xchargv[1]!=NULL) { if(!strcmp(xchargv[0],"dev_boot")) { splitc(BOOT_DEV,xchargv[1],':'); strcpy(BOOT_FS,xchargv[1]); } else if(!strcmp(xchargv[0],"dev_strg")) { splitc(STRG_DEV,xchargv[1],':'); strcpy(STRG_FS,xchargv[1]); } else if(!strcmp(xchargv[0],"dev_swap")) { splitc(SWAP_DEV,xchargv[1],':'); } else if(!strcmp(xchargv[0],"keymap")) { strcpy(KEY_MAP,xchargv[1]); } else if(!strcmp(xchargv[0],"dev_lcd")) { strcpy(LCD_DEV,xchargv[1]); trim(LCD_DEV); } else if(!strcmp(xchargv[0],"lcdprog")) { LCD_PROG=atoi(xchargv[1]); } else if(!strcmp(xchargv[0],"numnet")) { NUM_NET=atoi(xchargv[1]); } } } } } }
static int simple_parse_response(struct pbcc_context *p) { char *reply = p->http_reply; int replylen = p->http_buf_len; if (replylen < 2) { return -1; } if ((reply[0] != '[') || (reply[replylen-1] != ']')) { return -1; } p->chan_ofs = 0; p->chan_end = 0; p->msg_ofs = 1; p->msg_end = replylen - 1; reply[replylen-1] = '\0'; return split_array(reply + p->msg_ofs) ? 0 : -1; }
// Replace: // array = { v1, v2, v3 }; // with // array[0] = v1; // array[1] = v2; // array[2] = v3; std::string replace_array_assignations (const std::string code, std::set<std::string>& local_declarations) { new_code_lines.clear(); new_instanciations.clear(); bool single_line_comment = false; bool multi_line_comment = false; bool preprocessor_directive = false; bool in_array_initialization = false; string_pos lvalue_end; string_pos current_code_line_start; string_pos array_start; unsigned int parenthesis_level = 0; char previous_character = ' '; std::map<std::string, std::string> replacements; std::map<std::string, std::string> replacement_lvalues; std::string new_code; for (std::string::size_type n = 0; n < code.size(); ++n) { char c = code[n]; new_code += c; if (c == ' ' || c == '\n' || c == '\t' || c == '\r' || single_line_comment || multi_line_comment ) { if (single_line_comment) { if (c == '\n' && previous_character != '\\') { single_line_comment = false; // new code line string_pos new_code_line_start = new_code.size(); string_pos p = n; char next_c = code[p]; while ((p < code.size()) && (next_c == '\n' || next_c == '\r')) { ++p; ++new_code_line_start; next_c = code[p]; } new_code_line (new_code_line_start, new_code); } } if (multi_line_comment) { if (c == '/' && previous_character == '*') multi_line_comment = false; } if (preprocessor_directive) { if (c == '\n' && previous_character != '\\') { preprocessor_directive = false; // new code line string_pos new_code_line_start = new_code.size(); current_code_line_start = new_code_line_start; string_pos p = n; char next_c = code[p]; while ((p < code.size()) && (next_c == '\n' || next_c == '\r')) { ++p; ++new_code_line_start; next_c = code[p]; } new_code_line (new_code_line_start, new_code); } } continue; } if (c == '/' && previous_character == '/') { single_line_comment = true; } else if (c == '*' && previous_character == '/') { multi_line_comment = true; } else if (c == '#') { preprocessor_directive = true; } else if (c == ';') { if (parenthesis_level == 0) { string_pos new_code_line_start = new_code.size(); current_code_line_start = n + 1; string_pos p = n; char next_c = code[p]; while (next_c == '\n' || next_c == '\r') { ++p; ++new_code_line_start; next_c = code[p]; } new_code_line (new_code_line_start, new_code); } } else if (c == '=') { lvalue_end = n - 1; } else if (c == '(') { parenthesis_level++; } else if (c == ')') { parenthesis_level--; } else if (c == '{') { if (previous_character == '=') { // found array inialization in_array_initialization = true; array_start = n + 1; } } else if (c == '}') { if (in_array_initialization) { // found array initialization's end in_array_initialization = false; // store l-value std::string lvalue = std::string (code, current_code_line_start, lvalue_end - current_code_line_start + 1); lvalue = trim (lvalue); // put array in a string string_pos array_end = n - 1; std::string array = std::string (code, array_start, array_end - array_start + 1); string_pos replacement_start = current_code_line_start; string_pos replacement_size = array_end - replacement_start + 2; std::string replacement = std::string (code, replacement_start, replacement_size); // save for later replacement replacements[replacement] = array; replacement_lvalues[replacement] = lvalue; } } previous_character = c; } // build shader block std::string shader_block = ""; for (std::vector<std::string>::iterator i = new_code_lines.begin(); i != new_code_lines.end(); ++i) { std::string line = *i; // quick'n'dirty replacements for (std::map<std::string, std::string>::iterator repl = replacements.begin(); repl != replacements.end(); ++repl) { std::string orig = repl->first; std::string array = repl->second; std::string lvalue = replacement_lvalues[orig]; std::string::size_type repl_start = line.find (orig); if (repl_start != std::string::npos) { // split array std::vector<std::string> array_values = split_array (array); // build replacement std::string new_array = "\n"; int array_index = 0; for (std::vector<std::string>::const_iterator val = array_values.begin(); val != array_values.end(); ++val) { if (array_index > 0) { new_array += ";\n"; } new_array += "\t" + lvalue + "[" + string_cast (array_index++) + "] = " + *val; } // replace line.replace (repl_start, orig.size(), new_array); } } // save line shader_block += "\t" + line; } return shader_block; }
static int parse_subscribe_response(pubnub_t *p) { char *reply = p->http_reply; unsigned int replylen = strlen(reply); if (reply[replylen-1] != ']' && replylen > 2) { replylen -= 2; // XXX: this seems required by Manxiang } if ((reply[0] != '[') || (reply[replylen-1] != ']') || (reply[replylen-2] != '"')) { return -1; } /* Extract the last argument. */ int i = find_string_start(reply, replylen-2); if (i < 0) { return -1; } reply[replylen - 2] = 0; /* Now, the last argument may either be a timetoken or a channel list. */ if (reply[i-2] == '"') { int k; /* It is a channel list, there is another string argument in front * of us. Process the channel list ... */ p->chan_ofs = i+1; p->chan_end = replylen - 1; for (k = p->chan_end - 1; k > p->chan_ofs; --k) { if (reply[k] == ',') { reply[k] = 0; } } /* ... and look for timetoken again. */ reply[i-2] = 0; i = find_string_start(reply, i-2); if (i < 0) { return -1; } } else { p->chan_ofs = 0; p->chan_end = 0; } /* Now, i points at * [[1,2,3],"5678"] * [[1,2,3],"5678","a,b,c"] * ^-- here */ /* Setup timetoken. */ if (replylen >= sizeof(p->timetoken) + 2 + (i+1)) { return -1; } strcpy(p->timetoken, reply + i+1); reply[i-2] = 0; // terminate the [] message array (before the ]!) /* Set up the message list - offset, length and NUL-characters splitting * the messages. */ p->msg_ofs = 2; p->msg_end = i-2; return split_array(reply + p->msg_ofs) ? 0 : -1; }
int pbcc_parse_subscribe_response(struct pbcc_context *p) { int i; int previous_i; unsigned time_token_length; char *reply = p->http_reply; int replylen = p->http_buf_len; if (replylen < 2) { return -1; } if (reply[replylen-1] != ']' && replylen > 2) { replylen -= 2; /* XXX: this seems required by Manxiang */ } if ((reply[0] != '[') || (reply[replylen-1] != ']') || (reply[replylen-2] != '"')) { return -1; } /* Extract the last argument. */ previous_i = replylen - 2; i = find_string_start(reply, previous_i); if (i < 0) { return -1; } reply[replylen - 2] = 0; /* Now, the last argument may either be a timetoken, a channel group list or a channel list. */ if (reply[i-2] == '"') { int k; /* It is a channel list, there is another string argument in front * of us. Process the channel list ... */ for (k = replylen - 2; k > i+1; --k) { if (reply[k] == ',') { reply[k] = '\0'; } } /* The previous argument is either a timetoken or a channel group list. */ reply[i-2] = '\0'; p->chan_ofs = i+1; p->chan_end = replylen - 1; previous_i = i-2; i = find_string_start(reply, previous_i); if (i < 0) { p->chan_ofs = 0; p->chan_end = 0; return -1; } if (reply[i-2] == '"') { /* It is a channel group list. For now, we shall skip it. In the future, we may process it like we do the channel list. */ reply[i-2] = '\0'; previous_i = i-2; i = find_string_start(reply, previous_i); if (i < 0) { return -1; } } } else { p->chan_ofs = 0; p->chan_end = 0; } /* Now, `i` points to: * [[1,2,3],"5678"] * [[1,2,3],"5678","a,b,c"] * [[1,2,3],"5678","gr-a,gr-b,gr-c","a,b,c"] * ^-- here */ /* Setup timetoken. */ time_token_length = previous_i - (i+1); if (time_token_length >= sizeof p->timetoken) { p->timetoken[0] = '\0'; return -1; } memcpy(p->timetoken, reply + i+1, time_token_length+1); /* terminate the [] message array (before the `]`!) */ reply[i-2] = 0; /* Set up the message list - offset, length and NUL-characters * splitting the messages. */ p->msg_ofs = 2; p->msg_end = i-2; return split_array(reply + p->msg_ofs) ? 0 : -1; }