void init_interupt(void) { SPECIAL_done = 1; g_vi.delay = g_vi.next_vi = 5000; clear_queue(); add_interupt_event_count(VI_INT, g_vi.next_vi); add_interupt_event_count(SPECIAL_INT, 0); }
void init_interupt(void) { SPECIAL_done = 1; next_vi = next_interupt = 5000; vi_register.vi_delay = next_vi; vi_field = 0; clear_queue(); add_interupt_event_count(VI_INT, next_vi); add_interupt_event_count(SPECIAL_INT, 0); }
void init_interupt(usf_state_t * state) { state->SPECIAL_done = 1; state->g_vi.delay = state->g_vi.next_vi = 5000; clear_queue(state); add_interupt_event_count(state, VI_INT, state->g_vi.next_vi); add_interupt_event_count(state, SPECIAL_INT, 0); }
// load the queue (for save states) void load_eventqueue_infos(char *buf) { unsigned int len = 0, type = 0, count = 0; clear_queue(); while (*((unsigned long*)&buf[len]) != 0xFFFFFFFF) { type = *((unsigned long*)&buf[len]); count = *((unsigned long*)&buf[len+4]); add_interupt_event_count(type, count); len += 8; } }
int enqueue(const char *fname, fqueue_t **queue) { struct list *temp; temp = (struct list *)malloc(sizeof(struct list)); if (temp != NULL) { temp->file_name = (char *)malloc( MAX_FILE_NAME_SIZE * sizeof(char)); if (temp->file_name == NULL) { free(temp); clear_queue(queue); return MEMORY_ALLOC_ERROR; } temp->buf = (char *)malloc(MAX_BUF_SIZE * sizeof(char)); if (temp->buf == NULL) { free(temp->file_name); free(temp); clear_queue(queue); return MEMORY_ALLOC_ERROR; } temp->bytes = 0; ZeroMemory(temp->buf, sizeof(temp->buf)); strcpy(temp->file_name, fname); temp->next = NULL; if ((*queue)->head == NULL) { (*queue)->head = temp; } else { (*queue)->tail->next = temp; } (*queue)->tail = temp; } else { return ENQUEUE_FAILURE; } return ENQUEUE_SUCCESS; }
void* run(void* arg) { int sockfd = *((int*)arg); printf("run sockfd %d\n", sockfd); pthread_cond_init(&s_signal.cond, NULL); pthread_mutex_init(&s_signal.lock, NULL); thread_wait(); bool is_response = false; while(1) { // printf("loop\n"); int r = 0; peek(sockfd, r); if(r & READ_AVAILABLE) { // printf("read available\n"); const int buf_len = 32; char buf[buf_len]; memset(buf, 0, buf_len); int nread = recv(sockfd, buf, buf_len, 0); printf("recv %d, %s\n", nread, buf); is_response = true; } if(r & WRITE_AVAILABLE) { pthread_mutex_lock(&send_queue.lock); if(send_queue.queue.size() > 0) { for (int i = 0; i < send_queue.queue.size(); ++i) { char* buf = (char*)send_queue.queue.front(); int sent = send(sockfd, buf, strlen(buf), 0); printf("sent %d, %s\n", sent, buf); } clear_queue(send_queue); } pthread_mutex_unlock(&send_queue.lock); } if(is_response) { thread_wait(); is_response = false; } usleep(1 * 1000); } }
void load_eventqueue_infos(char *buf) { int len = 0; clear_queue(&q); while (*((unsigned int*)&buf[len]) != 0xFFFFFFFF) { int type = *((unsigned int*)&buf[len]); unsigned int count = *((unsigned int*)&buf[len+4]); add_interrupt_event_count(type, count); len += 8; } }
void Obj_Data :: Extract( ) { obj_array* array; int i; if( !Is_Valid( ) ) { roach( "Extracting invalid object." ); roach( "-- Valid = %d", valid ); roach( "-- Obj = %s", this ); return; } if( this->array != NULL ) From( number ); extract( contents ); if( boot_stage == 2 ) pIndexData->count -= number; clear_queue( this ); stop_events( this ); if( save != NULL ) { array = &save->save_list; for( i = 0; ; i++ ) { if( i >= array->size ) panic( "Extract: Object not found in save array." ); if( this == array->list[i] ) { array->list[i] = NULL; save = NULL; break; } } } delete_list( affected ); free_string( source, MEM_OBJECT ); if( singular != pIndexData->singular ) free_string( singular, MEM_OBJECT ); if( plural != pIndexData->plural ) free_string( plural, MEM_OBJECT ); if( after != pIndexData->after ) free_string( after, MEM_OBJECT ); if( before != pIndexData->before ) free_string( before, MEM_OBJECT ); timer = -2; valid = -1; extracted += this; }
// This gets the local IP address TODO read if error bool get_local_ip(void) { bool return_value = false; char send_command[sizeof(AT_GET_IP_COMMAND)] = AT_GET_IP_COMMAND; return_value = clear_queue(xQueue_receive_from_gsm_handle, xSemaphore_receive_from_gsm_handle, 200); // Send AT command if (return_value) { return_value = add_string_to_queue(send_command, xQueue_uart_gsm_send_handle, 50, xSemaphore_uart_gsm_send_handle, 50); } vTaskDelay(500 / portTICK_RATE_MS); return return_value; }
int insert_item_test(void) { clear_queue(); _assert(queue_size() == 0); insert_item(42); _assert(front() == 42); _assert(rear() == 42); _assert(queue_size() == 1); insert_item(10); _assert(front() == 42); _assert(rear() == 10); _assert(queue_size() == 2); insert_item(-1); _assert(front() == 42); _assert(rear() == -1); _assert(queue_size() == 3); clear_queue(); return 0; }
void init_interupt() { SPECIAL_done = 1; next_vi = next_interupt = 5000; vi_register.vi_delay = next_vi; vi_field = 0; if (qbase != NULL) free(qbase); qbase = (interupt_queue *) malloc(sizeof(interupt_queue) * QUEUE_SIZE ); memset(qbase,0,sizeof(interupt_queue) * QUEUE_SIZE ); qstackindex=0; clear_queue(); add_interupt_event_count(VI_INT, next_vi); add_interupt_event_count(SPECIAL_INT, 0); }
/* Process all the characters in INPUTQ as if they had just been read. */ void rescan_inputq () { short *buf; int i, n; n = qsize (inputq); buf = alloca (n * sizeof (quoted_char)); memcpy (buf, inputq->cs, n * sizeof (quoted_char)); clear_queue (inputq); for (i = 0; i < n; i++) input_character (unquote_char (buf[i])); }
static DBusHandlerResult got_message( DBusConnection __unused *conn, DBusMessage *message, void __unused *user_data) { if (dbus_message_is_signal(message, "org.kerneloops.submit.ping", "ping")) { pinged = 1; return DBUS_HANDLER_RESULT_HANDLED; } if (dbus_message_is_signal(message, "org.kerneloops.submit.permission", "yes")) { submit_queue(); return DBUS_HANDLER_RESULT_HANDLED; } if (dbus_message_is_signal(message, "org.kerneloops.submit.permission", "always")) { submit_queue(); opted_in = 2; return DBUS_HANDLER_RESULT_HANDLED; } if (dbus_message_is_signal(message, "org.kerneloops.submit.permission", "never")) { clear_queue(); opted_in = 0; return DBUS_HANDLER_RESULT_HANDLED; } if (dbus_message_is_signal(message, "org.kerneloops.submit.permission", "no")) { clear_queue(); return DBUS_HANDLER_RESULT_HANDLED; } return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; }
//销毁队列 bool destroy_queue(s_queue *queue) { if (queue == null) { return false; } if (queue->header == null) { return true; } clear_queue(queue); return true; }
void dvbsub_setpid(int pid) { if(!pid) return; dvbsub_pid = pid; clear_queue(); if(dvbSubtitleConverter) dvbSubtitleConverter->Reset(); pid_change_req = 1; dvbsub_stopped = 0; pthread_mutex_lock(&readerMutex); pthread_cond_broadcast(&readerCond); pthread_mutex_unlock(&readerMutex); }
SlideshowController::SlideshowController(QObject *parent) : QObject(parent) { /*************************/ slideshow_window_view_ = new SlideshowWindowView; slideshow_window_view_->resize(800, 450); slideshow_data_model_ = new SlideshowDataModel; settings_window_view_ = new SettingsWindowView; about_window_view_ = new AboutWindowView; connect(slideshow_window_view_, SIGNAL(settings_option_clicked()), this, SLOT(show_settings_view())); connect(slideshow_window_view_, SIGNAL(video_finished_playing()), this, SLOT(post_video_unpause())); connect(slideshow_data_model_->slideshow_queue(), SIGNAL(finished_marketing()), &marketing_slide_timer_, SLOT(start(slideshow_data_model_-> marketing_timer_interval())) + slideshow_data_model_-> indiv_info_slide_interval()); connect(&marketing_slide_timer_, SIGNAL(timeout()), this, SLOT(queue_marketing_slide())); connect(settings_window_view_, SIGNAL(ok_button_pressed()), this, SLOT(gui_ok_button_pressed())); connect(settings_window_view_, SIGNAL(apply_button_pressed()), this, SLOT(gui_apply_button_pressed())); connect(settings_window_view_, SIGNAL(cancel_button_pressed()), this, SLOT(gui_cancel_button_pressed())); connect(&main_slide_timer_, SIGNAL(timeout()), this, SLOT(show_next_slide())); connect(settings_window_view_, SIGNAL(slide_folder_changed()), slideshow_data_model_->slideshow_queue(), SLOT(clear_queue())); connect(slideshow_window_view_, SIGNAL(about_slideshow_clicked()), this, SLOT(show_about_slideshow())); connect(slideshow_window_view_, SIGNAL(about_qt_clicked()), this, SLOT(show_about_qt())); /*************************/ }
void main(void) { int i; init_queue(); printf("\nPut 5, 4, 7, 8, 2, 1"); put(5); put(4); put(7); put(8); put(2); put(1); print_queue(); printf("\nGet"); i = get(); print_queue(); printf("\n getting value is %d", i); printf("\nPut 3, 2, 5, 7"); put(3); put(2); put(5); put(7); print_queue(); printf("\nNow queue is full, put 3"); put(3); print_queue(); printf("\nInitialize queue"); clear_queue(); print_queue(); printf("\nNow queue is empty, get"); i = get(); print_queue(); printf("\n getting value is %d", i); }
void queue(JitterBuffer *q, RTPMessage *pk) { uint16_t sequnum = pk->header->sequnum; unsigned int num = sequnum % q->size; if ((uint32_t)(sequnum - q->bottom) > q->size) { clear_queue(q); q->bottom = sequnum; q->queue[num] = pk; q->top = sequnum + 1; return; } if (q->queue[num]) return; q->queue[num] = pk; if ((sequnum - q->bottom) >= (q->top - q->bottom)) q->top = sequnum + 1; }
//destroy memory allocated in simulation void destroy_simulation() { for(int i=0;i<component_size;i++) { if(component_list[i]) { if(component_list[i]->type=='Q') { clear_queue(C2Q(component_list[i])); }else if(component_list[i]->type=='F') { clear_fork(C2F(component_list[i])); } free(component_list[i]); } } for(Event *e = (&fel)->next;e!=NULL;) { Event* tmp = e->next; free(e); e = tmp; } }
// Get signal quality report void get_sqr_report(char *rssi, char *ber) { char receive_buffer[20]; clear_queue(xQueue_receive_from_gsm_handle, xSemaphore_receive_from_gsm_handle, 100); add_string_to_queue(AT_QSR_REPORT_COMMAND, xQueue_uart_gsm_send_handle, 100, xSemaphore_uart_gsm_send_handle, 100); uint8_t i=0; uint8_t j=0; uint8_t k=0; if(return_string_with_identifier(xQueue_receive_from_gsm_handle, receive_buffer, AT_QSR_REPORT_RESPOND, AT_GENERAL_END_OF_MSG, MAX_SQR_LEN, 100)) { if(sqr_report_check(receive_buffer)) { while(!((receive_buffer[i] > 0x30) && (receive_buffer[i] < 0x3A))) { i++; } // Update rssi data while(receive_buffer[i] != 0x2c) { rssi[j] = receive_buffer[i]; i++; j++; } // Update ber data i++; while(receive_buffer[i] != '\r') { ber[k] = receive_buffer[i]; i++; k++; } } } }
static switch_status_t channel_on_destroy(switch_core_session_t *session) { switch_channel_t *channel = NULL; loopback_private_t *tech_pvt = NULL; switch_event_t *vars; channel = switch_core_session_get_channel(session); switch_assert(channel != NULL); tech_pvt = switch_core_session_get_private(session); if ((vars = (switch_event_t *) switch_channel_get_private(channel, "__loopback_vars__"))) { switch_channel_set_private(channel, "__loopback_vars__", NULL); switch_event_destroy(&vars); } if (tech_pvt) { switch_core_timer_destroy(&tech_pvt->timer); if (switch_core_codec_ready(&tech_pvt->read_codec)) { switch_core_codec_destroy(&tech_pvt->read_codec); } if (switch_core_codec_ready(&tech_pvt->write_codec)) { switch_core_codec_destroy(&tech_pvt->write_codec); } if (tech_pvt->write_frame) { switch_frame_free(&tech_pvt->write_frame); } clear_queue(tech_pvt); } return SWITCH_STATUS_SUCCESS; }
void QueueTest::run_tests() { // Tests enqueue initialize_queue(); // Tests isEmpty check_queue_is_not_empty(); // Tests size check_queue_size(); // Tests front check_queue_front(); // Ensure size did not change check_queue_size(); // Tests dequeue check_queue_dequeue(); // Ensure size changed check_queue_size(); // Ensure front is updated to next value check_queue_front(); // Exercises isEmpty and dequeue clear_queue(); // Ensure empty check_queue_is_empty(); // Ensure default value returned check_queue_default_value(); }
/* Return 0 if packet was queued, -1 if it wasn't. */ static int queue(Group_JitterBuffer *q, Group_Audio_Packet *pk) { uint16_t sequnum = pk->sequnum; unsigned int num = sequnum % q->size; if ((uint32_t)(sequnum - q->bottom) > q->size) { clear_queue(q); q->bottom = sequnum; q->queue[num] = pk; q->top = sequnum + 1; return 0; } if (q->queue[num]) return -1; q->queue[num] = pk; if ((sequnum - q->bottom) >= (q->top - q->bottom)) q->top = sequnum + 1; return 0; }
/* My beautiful main */ int main(int argc, char * argv[]){ /* Declare instance data */ int i; PCB **headOfQueue; PCB **tailOfQueue; PCB pcbNode; /* allocate space on the stack for the head and tail */ headOfQueue = (PCB**)malloc(sizeof(PCB*)); tailOfQueue = (PCB**)malloc(sizeof(PCB*)); /* Start the pointers out as null */ *headOfQueue = 0; *tailOfQueue = 0; /* for loop to populate the gueue and assign ID a random value */ for(i=0; i<100; i++){ /* Initialize the PCB variable values to 0 */ pcbNode.ID = rand()% 10000 + 1; enqueue(headOfQueue,tailOfQueue,pcbNode); } /* Prints the nodes ID in the queue */ print_queue(*tailOfQueue); /* Prints the number of nodes in the queue*/ printf("There are %d elements in the queue.\n", size_of_queue(*tailOfQueue)); /* Clears the queue */ clear_queue(headOfQueue,tailOfQueue); /* Reprints the number of nodes to ensure it was cleared*/ printf("There are %d elements in the queue.\n", size_of_queue(*tailOfQueue)); return(0); }
static void* reader_thread(void * /*arg*/) { int fds[2]; pipe(fds); fcntl(fds[0], F_SETFD, FD_CLOEXEC); fcntl(fds[0], F_SETFL, O_NONBLOCK); fcntl(fds[1], F_SETFD, FD_CLOEXEC); fcntl(fds[1], F_SETFL, O_NONBLOCK); flagFd = fds[1]; uint8_t tmp[16]; /* actually 6 should be enough */ int count; int len; uint16_t packlen; uint8_t* buf; bool bad_startcode = false; set_threadname("dvbsub:reader"); dmx = new cDemux(0); #if HAVE_TRIPLEDRAGON dmx->Open(DMX_PES_CHANNEL, NULL, 16*1024); #else dmx->Open(DMX_PES_CHANNEL, NULL, 64*1024); #endif while (reader_running) { if(dvbsub_stopped /*dvbsub_paused*/) { sub_debug.print(Debug::VERBOSE, "%s stopped\n", __FUNCTION__); dmx->Stop(); pthread_mutex_lock(&packetMutex); pthread_cond_broadcast(&packetCond); pthread_mutex_unlock(&packetMutex); pthread_mutex_lock(&readerMutex ); int ret = pthread_cond_wait(&readerCond, &readerMutex); pthread_mutex_unlock(&readerMutex); if (ret) { sub_debug.print(Debug::VERBOSE, "pthread_cond_timedwait fails with %d\n", ret); } if(!reader_running) break; dvbsub_stopped = 0; sub_debug.print(Debug::VERBOSE, "%s (re)started with pid 0x%x\n", __FUNCTION__, dvbsub_pid); } if(pid_change_req) { pid_change_req = 0; clear_queue(); dmx->Stop(); dmx->pesFilter(dvbsub_pid); dmx->Start(); sub_debug.print(Debug::VERBOSE, "%s changed to pid 0x%x\n", __FUNCTION__, dvbsub_pid); } struct pollfd pfds[2]; pfds[0].fd = fds[1]; pfds[0].events = POLLIN; char _tmp[64]; #if HAVE_SPARK_HARDWARE || HAVE_DUCKBOX_HARDWARE if (isEplayer) { poll(pfds, 1, -1); while (0 > read(pfds[0].fd, _tmp, sizeof(tmp))); continue; } #endif pfds[1].fd = dmx->getFD(); pfds[1].events = POLLIN; switch (poll(pfds, 2, -1)) { case 0: case -1: if (pfds[0].revents & POLLIN) while (0 > read(pfds[0].fd, _tmp, sizeof(tmp))); continue; default: if (pfds[0].revents & POLLIN) while (0 > read(pfds[0].fd, _tmp, sizeof(tmp))); if (!(pfds[1].revents & POLLIN)) continue; } len = dmx->Read(tmp, 6, 0); if(len <= 0) continue; if(!memcmp(tmp, "\x00\x00\x01\xbe", 4)) { // padding stream packlen = getbits(tmp, 4*8, 16) + 6; count = 6; buf = (uint8_t*) malloc(packlen); // actually, we're doing slightly too much here ... memmove(buf, tmp, 6); /* read rest of the packet */ while((count < packlen) && !dvbsub_stopped) { len = dmx->Read(buf+count, packlen-count, 1000); if (len < 0) { break; } else { count += len; } } free(buf); buf = NULL; continue; } if(memcmp(tmp, "\x00\x00\x01\xbd", 4)) { if (!bad_startcode) { sub_debug.print(Debug::VERBOSE, "[subtitles] bad start code: %02x%02x%02x%02x\n", tmp[0], tmp[1], tmp[2], tmp[3]); bad_startcode = true; } continue; } bad_startcode = false; count = 6; packlen = getbits(tmp, 4*8, 16) + 6; buf = (uint8_t*) malloc(packlen); memmove(buf, tmp, 6); /* read rest of the packet */ while((count < packlen) && !dvbsub_stopped) { len = dmx->Read(buf+count, packlen-count, 1000); if (len < 0) { break; } else { count += len; } } #if 0 for(int i = 6; i < packlen - 4; i++) { if(!memcmp(&buf[i], "\x00\x00\x01\xbd", 4)) { int plen = getbits(&buf[i], 4*8, 16) + 6; sub_debug.print(Debug::VERBOSE, "[subtitles] ******************* PES header at %d ?! *******************\n", i); sub_debug.print(Debug::VERBOSE, "[subtitles] start code: %02x%02x%02x%02x len %d\n", buf[i+0], buf[i+1], buf[i+2], buf[i+3], plen); free(buf); continue; } } #endif if(!dvbsub_stopped /*!dvbsub_paused*/) { sub_debug.print(Debug::VERBOSE, "[subtitles] *** new packet, len %d buf 0x%x pts-stc diff %lld ***\n", count, buf, get_pts_stc_delta(get_pts(buf))); /* Packet now in memory */ pthread_mutex_lock(&packetMutex); packet_queue.push(buf); /* TODO: allocation exception */ // wake up dvb thread pthread_cond_broadcast(&packetCond); pthread_mutex_unlock(&packetMutex); } else { free(buf); buf=NULL; } } dmx->Stop(); delete dmx; dmx = NULL; close(fds[0]); close(fds[1]); flagFd = -1; sub_debug.print(Debug::VERBOSE, "%s shutdown\n", __FUNCTION__); pthread_exit(NULL); }
void QContentHubServer::dispatch(msgpack::rpc::request req) { try { std::string method; req.method().convert(&method); if(method == "push") { msgpack::type::tuple<std::string, std::string> params; req.params().convert(¶ms); push_queue(req, params.get<0>(), params.get<1>()); } else if(method == "pop") { msgpack::type::tuple<std::string> params; req.params().convert(¶ms); pop_queue(req, params.get<0>()); } else if(method == "push_nowait") { msgpack::type::tuple<std::string, std::string> params; req.params().convert(¶ms); push_queue_nowait(req, params.get<0>(), params.get<1>()); } else if(method == "pop_nowait") { msgpack::type::tuple<std::string> params; req.params().convert(¶ms); pop_queue_nowait(req, params.get<0>()); } else if(method == "add") { msgpack::type::tuple<std::string, int> params; req.params().convert(¶ms); add_queue(req, params.get<0>(), params.get<1>()); /* } else if(method == "del") { msgpack::type::tuple<std::string> params; req.params().convert(¶ms); del_queue(req, params.get<0>()); } else if(method == "fdel") { msgpack::type::tuple<std::string> params; req.params().convert(¶ms); force_del_queue(req, params.get<0>()); */ } else if(method == "set_capacity") { msgpack::type::tuple<std::string, int> params; req.params().convert(¶ms); set_queue_capacity(req, params.get<0>(), params.get<1>()); } else if(method == "start") { msgpack::type::tuple<std::string> params; req.params().convert(¶ms); start_queue(req, params.get<0>()); } else if(method == "stop") { msgpack::type::tuple<std::string> params; req.params().convert(¶ms); stop_queue(req, params.get<0>()); } else if(method == "clear") { msgpack::type::tuple<std::string> params; req.params().convert(¶ms); clear_queue(req, params.get<0>()); } else if(method == "stats") { stats(req); } else if(method == "stat_queue") { msgpack::type::tuple<std::string> params; req.params().convert(¶ms); stat_queue(req, params.get<0>()); } else { req.error(msgpack::rpc::NO_METHOD_ERROR); } } catch (msgpack::type_error& e) { req.error(msgpack::rpc::ARGUMENT_ERROR); return; } catch (std::exception& e) { req.error(std::string(e.what())); return; } }
//销毁队列 void destory_queue(link_queue_t* &queue) { clear_queue(queue); delete queue; queue = NULL; }
static queue_t *infixtorpn(char *infix, size_t size) { /* Use RPN */ int is_mismatched = 0; int func_size = 16; int func_idx = -1; char *formula = malloc(size); int *func_args = malloc(func_size * sizeof(int)); char *delims = "():,+-*/"; char *arg, *iter; char delim; char arith_op[2] = {'\0', '\0'}; char *value; cell_type_e arg_type; stack_t *operators = NULL; queue_t *output_q = NULL; cgc_memcpy(formula, infix, size); if (sanitize_formula(formula, size) != 0) goto cleanup; arg = iter = formula; size_t i = 0; char prev_char = 0; while ( i++ < size) { if (strchr(delims, *iter) == NULL && *iter != '\0') { prev_char = *iter; iter++; continue; } else if (strchr(delims, *iter) != NULL) { if (*iter == '-') { if(i <= 1 || (prev_char != ')' && (prev_char < '0' || prev_char > '9'))) { prev_char = *iter; iter++; continue; } } } prev_char = *iter; delim = *iter; *iter = '\0'; arg_type = parsearg(arg); switch (arg_type) { case DOUBLE: case CELL_ID: enqueue_copy(&output_q, arg, cgc_strlen(arg) + 1); break; case FUNCTION: #ifdef PATCHED if(func_idx == func_size-1) { #else if(func_idx == func_size) { #endif func_size *= 2; int *temp = realloc(func_args, func_size * sizeof(int)); if (temp == NULL) goto error; func_args = temp; } func_args[++func_idx] = 0; push_copy(&operators, arg, cgc_strlen(arg) + 1); break; case BAD_CELL: break; default: goto error; } is_mismatched = 0; switch(delim) { case '(': push_copy(&operators, "(", cgc_strlen("(") + 1); break; case ')': is_mismatched = 1; while (operators != NULL) { if (strcmp(peek_top(operators), "(") == 0) { value = pop_copy(&operators); free(value); is_mismatched = 0; break; } else { enqueue(&output_q, pop_copy(&operators)); } } // handle parens without any operator if (peek_top(operators) == NULL || func_idx < 0) break; char num_args_str[16]; if(strchr(delims, peek_top(operators)[0]) != NULL) { break; } else if (parsearg(peek_top(operators)) == FUNCTION) { enqueue_copy(&output_q, itoa(func_args[func_idx--] + 1, num_args_str, sizeof(num_args_str)), sizeof(num_args_str)); enqueue(&output_q, pop_copy(&operators)); } break; case ',': is_mismatched = 1; while (operators != NULL) { if (strcmp(peek_top(operators), "(") == 0) { if (func_idx >= 0) func_args[func_idx]++; is_mismatched = 0; break; } else { enqueue(&output_q, pop_copy(&operators)); } } break; case '+': case '-': //TODO - FIXME - precedence is broken //TODO - FIXME - negative is still broken // 4/5-5 arith_op[0] = delim; while (operators != NULL) { if (strcmp(peek_top(operators), "-") == 0 || strcmp(peek_top(operators), "+") == 0 || strcmp(peek_top(operators), "+") == 0 || strcmp(peek_top(operators), "/") == 0) enqueue(&output_q, pop_copy(&operators)); else break; } push_copy(&operators, arith_op, cgc_strlen(arith_op)+1); break; case '*': case '/': //TODO - FIXME - precedence is broken arith_op[0] = delim; while (operators != NULL) { if (strcmp(peek_top(operators), "/") == 0 || strcmp(peek_top(operators), "*") == 0) enqueue(&output_q, pop_copy(&operators)); else break; } push_copy(&operators, arith_op, cgc_strlen(arith_op)+1); break; case '\0': goto finish; default: goto error; } if (is_mismatched) goto error; arg = ++iter; } finish: while (operators != NULL) { if (strcmp(peek_top(operators), "(") == 0 || strcmp(peek_top(operators), ")") == 0) goto error; enqueue(&output_q, pop_copy(&operators)); } goto cleanup; error: clear_queue(&output_q); clear_stack(&operators); cleanup: free(formula); free(func_args); return output_q; }
static double eval_formula(char *formula, int *is_bad_formula, stack_t **cir_ref, int id) { char val_str[TMP_STR_SIZE]; char tmp_id_str[TMP_STR_SIZE]; size_t size = TMP_STR_SIZE; double val = 0.0; double result = 0.0; *is_bad_formula = 0; cell_type_e cell_type; char *arg; if(itoa(id, tmp_id_str, size) == NULL) goto error; push_copy(cir_ref, tmp_id_str, cgc_strlen(tmp_id_str) + 1); queue_t *rpn = infixtorpn(formula, cgc_strlen(formula) + 1); queue_t *args = NULL; stack_t *values = NULL; stack_t *tmp = NULL; operator_t *op = NULL; while (rpn != NULL) { arg = dequeue_copy(&rpn); cell_type = parsearg(arg); switch(cell_type) { case DOUBLE: push(&values, arg); break; case FUNCTION: op = get_op(arg); if (eval_function(op, &values, val_str, size) == -1) { goto error; } push_copy(&values, val_str, size); break; case CELL_ID: tmp = *cir_ref; cell_t *cell = get_cell(arg); if(cell == NULL) goto error; while (tmp != NULL) { if(itoa(cell->id, tmp_id_str, size) == NULL) goto error; if (memcmp(tmp->data, tmp_id_str, cgc_strlen(tmp->data) + 1) == 0) goto error; //Circular reference tmp = tmp->next; } if (cell->cell_type == UNUSED) { push_copy(&values, "0", sizeof("0")); } else if (cell->cell_type == DOUBLE) { push_copy(&values, cell->str, cgc_strlen(cell->str) + 1); } else if(cell->cell_type == FORMULA) { val = eval_formula(cell->formula, is_bad_formula, cir_ref, cell->id); if(*is_bad_formula) goto error; ftoa(val, val_str, size); push_copy(&values, val_str, size); } else { goto error; } break; default: goto error; } } char *result_str = pop_copy(&values); if (values != NULL) goto error; result = atof(result_str, size, is_bad_formula); if (*is_bad_formula) goto error; goto cleanup; error: *is_bad_formula = 1; val = 0.0; clear_queue(&rpn); clear_queue(&args); clear_stack(&values); cleanup: free(pop_copy(cir_ref)); return result; }
static switch_status_t channel_write_frame(switch_core_session_t *session, switch_frame_t *frame, switch_io_flag_t flags, int stream_id) { switch_channel_t *channel = NULL; private_t *tech_pvt = NULL; switch_status_t status = SWITCH_STATUS_FALSE; channel = switch_core_session_get_channel(session); switch_assert(channel != NULL); tech_pvt = switch_core_session_get_private(session); switch_assert(tech_pvt != NULL); if (switch_test_flag(frame, SFF_CNG) || (switch_test_flag(tech_pvt, TFLAG_BOWOUT) && switch_test_flag(tech_pvt, TFLAG_BOWOUT_USED))) { switch_core_timer_sync(&tech_pvt->timer); switch_core_timer_sync(&tech_pvt->other_tech_pvt->timer); return SWITCH_STATUS_SUCCESS; } switch_mutex_lock(tech_pvt->mutex); if (!switch_test_flag(tech_pvt, TFLAG_BOWOUT) && tech_pvt->other_tech_pvt && switch_test_flag(tech_pvt, TFLAG_BRIDGE) && switch_test_flag(tech_pvt->other_tech_pvt, TFLAG_BRIDGE) && switch_channel_test_flag(tech_pvt->channel, CF_BRIDGED) && switch_channel_test_flag(tech_pvt->other_channel, CF_BRIDGED) && switch_channel_test_flag(tech_pvt->channel, CF_ANSWERED) && switch_channel_test_flag(tech_pvt->other_channel, CF_ANSWERED) && --tech_pvt->bowout_frame_count <= 0) { const char *a_uuid = NULL; const char *b_uuid = NULL; const char *vetoa, *vetob; vetoa = switch_channel_get_variable(tech_pvt->channel, "loopback_bowout"); vetob = switch_channel_get_variable(tech_pvt->other_tech_pvt->channel, "loopback_bowout"); if ((!vetoa || switch_true(vetoa)) && (!vetob || switch_true(vetob))) { switch_core_session_t *br_a, *br_b; switch_channel_t *ch_a = NULL, *ch_b = NULL; int good_to_go = 0; switch_mutex_unlock(tech_pvt->mutex); find_non_loopback_bridge(session, &br_a, &a_uuid); find_non_loopback_bridge(tech_pvt->other_session, &br_b, &b_uuid); switch_mutex_lock(tech_pvt->mutex); if (br_a) { ch_a = switch_core_session_get_channel(br_a); switch_core_media_bug_transfer_recordings(session, br_a); } if (br_b) { ch_b = switch_core_session_get_channel(br_b); switch_core_media_bug_transfer_recordings(tech_pvt->other_session, br_b); } if (ch_a && ch_b && switch_channel_test_flag(ch_a, CF_BRIDGED) && switch_channel_test_flag(ch_b, CF_BRIDGED)) { switch_set_flag_locked(tech_pvt, TFLAG_BOWOUT); switch_set_flag_locked(tech_pvt->other_tech_pvt, TFLAG_BOWOUT); switch_clear_flag_locked(tech_pvt, TFLAG_WRITE); switch_clear_flag_locked(tech_pvt->other_tech_pvt, TFLAG_WRITE); switch_set_flag_locked(tech_pvt, TFLAG_BOWOUT_USED); switch_set_flag_locked(tech_pvt->other_tech_pvt, TFLAG_BOWOUT_USED); if (a_uuid && b_uuid) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "%s detected bridge on both ends, attempting direct connection.\n", switch_channel_get_name(channel)); /* channel_masquerade eat your heart out....... */ switch_ivr_uuid_bridge(a_uuid, b_uuid); good_to_go = 1; switch_mutex_unlock(tech_pvt->mutex); } } if (br_a) switch_core_session_rwunlock(br_a); if (br_b) switch_core_session_rwunlock(br_b); if (good_to_go) { return SWITCH_STATUS_SUCCESS; } } } if (switch_test_flag(tech_pvt, TFLAG_LINKED) && tech_pvt->other_tech_pvt) { switch_frame_t *clone; if (frame->codec->implementation != tech_pvt->write_codec.implementation) { /* change codecs to match */ tech_init(tech_pvt, session, frame->codec); tech_init(tech_pvt->other_tech_pvt, tech_pvt->other_session, frame->codec); } if (switch_frame_dup(frame, &clone) != SWITCH_STATUS_SUCCESS) { abort(); } if ((status = switch_queue_trypush(tech_pvt->other_tech_pvt->frame_queue, clone)) != SWITCH_STATUS_SUCCESS) { clear_queue(tech_pvt->other_tech_pvt); status = switch_queue_trypush(tech_pvt->other_tech_pvt->frame_queue, clone); } if (status == SWITCH_STATUS_SUCCESS) { switch_set_flag_locked(tech_pvt->other_tech_pvt, TFLAG_WRITE); } else { switch_frame_free(&clone); } status = SWITCH_STATUS_SUCCESS; } switch_mutex_unlock(tech_pvt->mutex); return status; }