static char *next_item(FILE *fp) { /* This routine reads strings from the file fp, strips comment * and buffers. If there are multiple strings on a line, they will * be stored here, and indices in the line buffer (buf) will be * stored in bufindex. This way we can uncomment on the fly, * without too much double work. Each string is first read through * fscanf in this routine, and then through sscanf in do_read. * No unnecessary string copying is done. */ #define MAXBUF 20 static char buf[STRLEN]; static int bufindex[MAXBUF]; int i,j0; char ccc; if (nbuf) { j0 = bufindex[0]; for(i=1; (i<nbuf); i++) bufindex[i-1] = bufindex[i]; nbuf--; return buf+j0; } else { /* First read until we find something that is not comment */ if (fgets2(buf,STRLEN-1,fp) == NULL) gmx_file("End of file"); i = 0; do { /* Skip over leading spaces */ while ((buf[i] != '\0') && (buf[i] != ';') && isspace(buf[i])) i++; /* Store start of something non-space */ j0 = i; /* Look for next spaces */ while ((buf[i] != '\0') && (buf[i] != ';') && !isspace(buf[i])) i++; /* Store the last character in the string */ ccc = buf[i]; /* If the string is non-empty, add it to the list */ if (i > j0) { buf[i] = '\0'; bufindex[nbuf++] = j0; /* We increment i here; otherwise the next test for buf[i] would be * '\0', since we test the main loop for ccc anyway, we cant go SEGV */ i++; } } while ((ccc != '\0') && (ccc != ';')); return next_item(fp); } }
/* print_attribute_list * Print each attribute in a NUL-separated list on its own line. * * Parameters: * list: The list of attributes * len: Length of the list */ void print_attribute_list (char* list, int len) { char *end = list + len; while (list < end) { printf(" %s\n", list); list = next_item(list); } }
/** * @brief Parse a @a dlabel definition. * * Syntax: dlabel=x,y,width,align,fontfile,"text" * * @param in definition to be analyzed * * @return 0 (ok) or 1 (error) */ static int item_dlabel(char *in) { int x, y, w, a, id; char fnt[256]; char txt[256]; wItem *item; if (!window_item("dlabel")) return 1; if (in_window("video")) return 1; if (in_window("menu")) return 1; x = cutItemToInt(in, ',', 0); y = cutItemToInt(in, ',', 1); w = cutItemToInt(in, ',', 2); a = cutItemToInt(in, ',', 3); cutItem(in, fnt, ',', 4); cutItem(in, txt, ',', 5); cutItem(txt, txt, '"', 1); mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[skin] dlabel: \"%s\"\n", txt); mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[skin] pos: %d,%d\n", x, y); mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[skin] width: %d, align: %d\n", w, a); id = fntFindID(fnt); if (id < 0) { skin_error(MSGTR_SKIN_FONT_NonExistentFont, fnt); return 1; } mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[skin] font: %s (#%d)\n", fnt, id); item = next_item(); if (!item) return 1; item->type = itDLabel; item->x = x; item->y = y; item->width = w; item->height = -1; item->fontid = id; item->align = a; item->label = strdup(txt); if (!item->label) { skin_error(MSGTR_SKIN_NotEnoughMemory); return 1; } return 0; }
int main(int argc, char *argv[]) { int i = 0; char *cmd = argv[++i]; char *str; if (argc < 2) { fprintf(stderr, "Missing function\n"); return 2; } else if (streq(cmd, "next")) { if (argc < 3) return 2; str = argv[++i]; return next_item(str, 0); } else if (streq(cmd, "nextw")) { if (argc < 3) return 2; str = argv[++i]; return next_item(str, 1); } else if (streq(cmd, "prev")) { if (argc != 3) return 2; str = argv[++i]; return prev_item(str, 0); } else if (streq(cmd, "prevw")) { if (argc != 3) return 2; str = argv[++i]; return prev_item(str, 1); } else if (streq(cmd, "rstrip")) { if (argc != 3) return 2; str = argv[++i]; return strip_tail(str); } else { fprintf(stderr, "Unknown function '%s'\n", cmd); return 2; } }
/* * python btsort_iter_pyobject.next implementation (the real iteration) */ static PyObject * python_sorted_btreeiterator_next(btsort_iter_pyobject *iter) { PyObject *item; if (next_item(iter->path, &item)) { PyErr_SetString(PyExc_StopIteration, ""); return NULL; } Py_INCREF(item); return item; }
void IntegerItem::Key(UINT nMsg, WPARAM wParam) { Active(2); if (WM_KEYDOWN == nMsg) { if (VK_LEFT == wParam) { m_direction = -1; set_next_value(); } else if (VK_RIGHT == wParam) { m_direction = 1; set_next_value(); } else if (VK_UP == wParam || VK_DOWN==wParam || VK_TAB == wParam) { next_item(wParam); } } else if (WM_KEYUP == nMsg) { if (VK_LEFT == wParam || VK_RIGHT == wParam) { Invoke(INVOKE_LEFT); } } else if (WM_CHAR == nMsg && (VK_ESCAPE == wParam || VK_RETURN==wParam)) { next_item(0); } }
/* * input */ static enum mad_flow input(void *data, struct mad_stream *stream) { http_t *p = (http_t *) data; static int haveread = 0; /* Clear MAD_BUFFER_GUARD chunk (last 8 bytes): */ memset(buf + MP3_SIZE, 0, MAD_BUFFER_GUARD); int Remaining; char *ReadStart; int ReadSize; /* MAD_ERROR_BUFLEN (= 0x0001) "input buffer too small (or EOF)" */ if (stream->error == MAD_ERROR_BUFLEN) { /* Some data not decoded; must reissue: */ Remaining = stream->bufend - stream->next_frame; memcpy(buf, stream->next_frame, Remaining); ReadStart = buf + Remaining; ReadSize = MP3_SIZE - Remaining; } else { ReadStart = buf; ReadSize = MP3_SIZE; Remaining = 0; } int got = http_read(p, ReadStart, ReadSize); if (got == 0) { /* Reset: */ buf = buf1; haveread = 0; next_item(); return MAD_FLOW_STOP; } haveread += got; mad_stream_buffer(stream, buf, Remaining + got); /* Switch between buffers: */ if (buf == buf1) { buf = buf2; } else { buf = buf1; } return MAD_FLOW_CONTINUE; }
/** * @brief Parse a @a menu definition. * * Syntax: menu=x,y,width,height,message * * @param in definition to be analyzed * * @return 0 (ok) or 1 (error) */ static int item_menu(char *in) { int x, y, w, h, message; char msg[32]; wItem *item; if (!window_item("menu")) return 1; if (in_window("main")) return 1; if (in_window("video")) return 1; if (in_window("playbar")) return 1; x = cutItemToInt(in, ',', 0); y = cutItemToInt(in, ',', 1); w = cutItemToInt(in, ',', 2); h = cutItemToInt(in, ',', 3); cutItem(in, msg, ',', 4); message = appFindMessage(msg); if (message == -1) { skin_error(MSGTR_SKIN_UnknownMessage, msg); return 1; } item = next_item(); if (!item) return 1; item->type = itMenu; item->x = x; item->y = y; item->width = w; item->height = h; item->message = message; mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[skin] item #%d: %d,%d %dx%d\n", *currWinItemIdx, x, y, w, h); mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[skin] message: %s (#%d)\n", msg, message); item->Bitmap.Image = NULL; return 0; }
static int foreach_item(TCListItem *start, int direction, TCListVisitor vis, void *userdata) { int ret = 0; TCListItem *cur = NULL, *inc = NULL; for (cur = start; cur; cur = inc) { inc = next_item(cur, direction); ret = vis(cur, userdata); if (ret != 0) { break; } } return ret; }
/* * output */ static enum mad_flow output(void *data, struct mad_header const *header, struct mad_pcm *pcm) { unsigned int nsamples; mad_fixed_t const *left_ch, *right_ch; switch (mp3_state()) { case MP3_PAUSE: mp3_wait_state(MP3_EXIT_PAUSE); if (mp3_check_state(MP3_PLAY) != MP3_PLAY) { return MAD_FLOW_STOP; /* JDH - skip to end of while loop to re-parse non-MP3_PLAY state */ } mp3_set_state(MP3_PLAYING); break; case MP3_NEXT: next_item(); return MAD_FLOW_STOP; case MP3_BACK: prev_item(); return MAD_FLOW_STOP; case MP3_STOP: return MAD_FLOW_STOP; default: break; } /* nchannels = pcm->channels; assume 2 */ nsamples = pcm->length; left_ch = pcm->samples[0]; right_ch = pcm->samples[1]; while (nsamples--) { int sample; sample = scale(*left_ch++) << 16; sample |= scale(*right_ch++) & 0xFFFF; write (out, &sample, sizeof(sample)); } return MAD_FLOW_CONTINUE; }
int glp_sdf_read_int(glp_data *data) { /* read integer number */ int x; next_item(data); switch (str2int(data->item, &x)) { case 0: break; case 1: glp_sdf_error(data, "integer `%s' out of range\n", data->item); case 2: glp_sdf_error(data, "cannot convert `%s' to integer\n", data->item); default: xassert(data != data); } return x; }
double glp_sdf_read_num(glp_data *data) { /* read floating-point number */ double x; next_item(data); switch (str2num(data->item, &x)) { case 0: break; case 1: glp_sdf_error(data, "number `%s' out of range\n", data->item); case 2: glp_sdf_error(data, "cannot convert `%s' to number\n", data->item); default: xassert(data != data); } return x; }
bool bin_index_t::file_node::next(data_t& key,data_t& val) const { validate_key_len(key); open_index_file(); if(!root_page) return false; index_t ind; align_key(key,ind); if(!next_item(root_page,ind))return false; key=ind.key; key.resize(key_len); val.resize(ind.data_len); load_data(ind.data_offset,val); return true; }
/* print_each_attribute * Retrieve every xattr in a list from a certain file and print them out. * * Parameters: * file: File to read attrs from * list: NUL-separated list of attribute names to read * len: length of list */ void print_each_attribute (char *file, char *list, int len) { char buffer[BUFFER_SIZE] = {0}; char *end = list + len; int ret; while (list < end) { printf("Getting attribute \"%s\"\n", list); ret = getxattr(file, list, buffer, BUFFER_SIZE); printf("getxattr() returned %d\n", ret); if (ret == -1) { printf("getxattr() failed: %s\n", strerror(errno)); } else { printf(" Attribute value:\n"); print_buffer(buffer, ret); } list = next_item(list); printf("\n"); } }
void quickbook_template_parameter_list(std::vector<parameter> const& parameters, std::string const& related_name, std::ostream& out) { if (!parameters.empty()) { std::string const header = "template<"; std::size_t index = 0; std::string const indent(header.length(), ' '); out << header; BOOST_FOREACH(parameter const& p, parameters) { if (p.fulltype.empty()) { std::cerr << "Warning: template parameter " << p.name << " has no type in " << related_name << std::endl; } next_item("", indent, 4, index, out); out << p.fulltype; } out << ">" << std::endl; }
const char *glp_sdf_read_item(glp_data *data) { /* read data item */ next_item(data); return data->item; }
void InputConfigurator::handle_event(const SDL_Event& event) { if (items.empty()) { std::cout << "InputConfigurator: done" << std::endl; ScreenManager::current()->pop_overlay(); return; } switch(event.type) { case SDL_MOUSEMOTION: // event.motion: break; case SDL_MOUSEBUTTONDOWN: if (items.back().mode == ConfigureItem::CONFIGURE_BUTTON) { InputManagerSDL::current()->bind_mouse_button(items.back().event_id, 0, // SDL only supports one mouse event.button.button); out << "(mouse-button (device " << 0 << ")\n" << " (button " << int(event.button.button) << "))" << std::endl; next_item(); } break; case SDL_MOUSEBUTTONUP: // event.button break; case SDL_JOYAXISMOTION: if (items.back().mode == ConfigureItem::CONFIGURE_AXIS && (event.jaxis.value > 16384 || event.jaxis.value < -16384)) { // FIXME: This doesn't work well with analog Axis! InputManagerSDL::current()->bind_joystick_axis(items.back().event_id, event.jaxis.which, event.jaxis.axis, false); out << "(joystick-axis (device " << int(event.jaxis.which) << ")\n" << " (axis " << int(event.jaxis.axis) << "))" << std::endl; next_item(); } else { } break; case SDL_JOYBALLMOTION: // event.jball break; case SDL_JOYHATMOTION: // event.jhat break; case SDL_JOYBUTTONUP: break; case SDL_JOYBUTTONDOWN: if (items.back().mode == ConfigureItem::CONFIGURE_BUTTON) { InputManagerSDL::current()->bind_joystick_button(items.back().event_id, event.jbutton.which, event.jbutton.button); out << "(joystick-button (device " << int(event.jbutton.which) << ")\n" << " (button " << int(event.jbutton.button) << "))" << std::endl; next_item(); } else if (items.back().mode == ConfigureItem::CONFIGURE_AXIS) { if (wait_for_plus && minus.type == SDL_JOYBUTTONDOWN) { out << "(joystick-axis-button (minus " << InputManagerSDL::current()->keyid_to_string(minus.key.keysym.sym) << ") " << "(plus " << InputManagerSDL::current()->keyid_to_string(event.key.keysym.sym) << "))" << std::endl; InputManagerSDL::current()->bind_joystick_button_axis(items.back().event_id, event.jbutton.which, minus.jbutton.button, event.jbutton.button); next_item(); wait_for_plus = false; } else if (!wait_for_plus) { out << "Press key for other direction" << std::endl; area.set_text(out.str()); minus = event; wait_for_plus = true; } } break; case SDL_KEYUP: break; case SDL_KEYDOWN: if (event.key.keysym.sym == SDLK_ESCAPE) { std::cout << "InputConfigurator: abort" << std::endl; ScreenManager::current()->pop_overlay(); //next_item(); } else if (event.key.keysym.sym == SDLK_RETURN) { std::cout << "Binding the enter key is not allowed" << std::endl; } else { if (items.back().mode == ConfigureItem::CONFIGURE_BUTTON) { InputManagerSDL::current()->bind_keyboard_button(items.back().event_id, event.key.keysym.sym); out << "(keyboard-button (key " << InputManagerSDL::current()->keyid_to_string(event.key.keysym.sym) << "))" << std::endl; next_item(); } else if (items.back().mode == ConfigureItem::CONFIGURE_AXIS) { if (wait_for_plus && minus.type == SDL_KEYDOWN) { out << "(keyboard-axis (minus " << InputManagerSDL::current()->keyid_to_string(minus.key.keysym.sym) << ") " << "(plus " << InputManagerSDL::current()->keyid_to_string(event.key.keysym.sym) << "))" << std::endl; InputManagerSDL::current()->bind_keyboard_axis(items.back().event_id, minus.key.keysym.sym, event.key.keysym.sym); next_item(); wait_for_plus = false; } else if (!wait_for_plus) { out << "Print key for other direction" << std::endl; area.set_text(out.str()); minus = event; wait_for_plus = true; } } } break; } }
static void _dump_object(BSP_OBJECT *obj, int layer) { if (!obj) { return; } int i; BSP_VALUE *val; reset_object(obj); switch (obj->type) { case OBJECT_TYPE_SINGLE : // Single fprintf(stderr, "\033[1;37mObject type : [SINGLE]\033[0m\n"); val = object_get_single(obj); _dump_value(val, layer); break; case OBJECT_TYPE_ARRAY : // Array fprintf(stderr, "\033[1;37mObject type : [ARRAY]\033[0m\n"); size_t idx = 0; for (idx = 0; idx < object_size(obj); idx ++) { for (i = 0; i <= layer; i ++) { fprintf(stderr, "\t"); } fprintf(stderr, "\033[1;35m%lld\033[0m\t=> ", (long long int) idx); val = object_get_array(obj, idx); _dump_value(val, layer); } fprintf(stderr, "\n"); break; case OBJECT_TYPE_HASH : // Dict fprintf(stderr, "\033[1;37mObject type : [HASH]\033[0m\n"); val = curr_item(obj); BSP_STRING *key; while (val) { key = curr_hash_key(obj); for (i = 0; i <= layer; i ++) { fprintf(stderr, "\t"); } if (key) { fprintf(stderr, "\033[1;33m"); write(STDERR_FILENO, STR_STR(key), STR_LEN(key)); fprintf(stderr, "\033[0m"); } else { fprintf(stderr, "### NO KEY ###"); } fprintf(stderr, "\t=> "); _dump_value(val, layer); next_item(obj); val = curr_item(obj); } fprintf(stderr, "\n"); break; case OBJECT_TYPE_UNDETERMINED : default : // Null fprintf(stderr, "\033[1;36mObject type : [UNKNOWN]\033[0m\n"); break; } return; }
static int handle_input(int ch) { switch (ch) { case 'q': quit_mode = quit_mode ? 0 : 1; return 1; case 0x1b: quit_mode = 0; print_help = 0; return 1; case 'y': if (quit_mode) exit(0); break; case 'a': next_attr(); return 1; case 'n': if (quit_mode) quit_mode = 0; else new_graph(); return 1; case 'x': del_graph(); return 1; case 'f': fold(); return 1; case 12: case KEY_CLEAR: #ifdef HAVE_REDRAWWIN redrawwin(stdscr); #endif clear(); return 1; case 'c': c_combined_node_list = c_combined_node_list ? 0 : 1; return 1; case 'S': clear(); set_graph_unit(X_SEC); return 1; case 'M': clear(); set_graph_unit(X_MIN); return 1; case 'H': clear(); set_graph_unit(X_HOUR); return 1; case 'D': clear(); set_graph_unit(X_DAY); return 1; case 'R': clear(); set_graph_unit(X_READ); return 1; case '?': clear(); print_help = print_help ? 0 : 1; return 1; case 'g': c_graphical_in_list = c_graphical_in_list ? 0 : 1; return 1; case 'd': c_detailed_in_list = c_detailed_in_list ? 0 : 1; return 1; case 'l': c_list_in_list = c_list_in_list ? 0 : 1; return 1; case KEY_PPAGE: if (print_help) help_page = help_page ? 0 : 1; else prev_node(); return 1; case KEY_NPAGE: if (print_help) help_page = help_page ? 0 : 1; else next_node(); return 1; case KEY_DOWN: if (next_item() == END_OF_LIST) { if (next_node() != END_OF_LIST) first_item(); } return 1; case KEY_UP: if (prev_item() == END_OF_LIST) { if (prev_node() != END_OF_LIST) last_item(); } return 1; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': goto_item(ch - 48); return 1; case '>': next_graph(); return 1; case '<': prev_graph(); return 1; default: if (get_current_item()) if (handle_bindings(ch, get_current_item()->i_name)) return 1; break; } return 0; }
/* * This is an alternate interface to 'buildlist' which allows the application * to read the list item states back directly without putting them in the * output buffer. */ int dlg_buildlist(const char *title, const char *cprompt, int height, int width, int list_height, int item_no, DIALOG_LISTITEM * items, const char *states, int order_mode, int *current_item) { /* *INDENT-OFF* */ static DLG_KEYS_BINDING binding[] = { HELPKEY_BINDINGS, ENTERKEY_BINDINGS, DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_RIGHT ), DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ), DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ), DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_LEFT ), DLG_KEYS_DATA( DLGK_ITEM_FIRST, KEY_HOME ), DLG_KEYS_DATA( DLGK_ITEM_LAST, KEY_END ), DLG_KEYS_DATA( DLGK_ITEM_LAST, KEY_LL ), DLG_KEYS_DATA( DLGK_ITEM_NEXT, '+' ), DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_DOWN ), DLG_KEYS_DATA( DLGK_ITEM_NEXT, CHR_NEXT ), DLG_KEYS_DATA( DLGK_ITEM_PREV, '-' ), DLG_KEYS_DATA( DLGK_ITEM_PREV, KEY_UP ), DLG_KEYS_DATA( DLGK_ITEM_PREV, CHR_PREVIOUS ), DLG_KEYS_DATA( DLGK_PAGE_NEXT, KEY_NPAGE ), DLG_KEYS_DATA( DLGK_PAGE_NEXT, DLGK_MOUSE(KEY_NPAGE) ), DLG_KEYS_DATA( DLGK_PAGE_NEXT, DLGK_MOUSE(KEY_NPAGE+KEY_MAX) ), DLG_KEYS_DATA( DLGK_PAGE_PREV, KEY_PPAGE ), DLG_KEYS_DATA( DLGK_PAGE_PREV, DLGK_MOUSE(KEY_PPAGE) ), DLG_KEYS_DATA( DLGK_PAGE_PREV, DLGK_MOUSE(KEY_PPAGE+KEY_MAX) ), DLG_KEYS_DATA( DLGK_GRID_LEFT, KEY_LEFTCOL ), DLG_KEYS_DATA( DLGK_GRID_RIGHT, KEY_RIGHTCOL ), END_KEYS_BINDING }; /* *INDENT-ON* */ #ifdef KEY_RESIZE int old_height = height; int old_width = width; #endif ALL_DATA all; MY_DATA *data = all.list; int i, j, k, key2, found, x, y, cur_x, cur_y; int key = 0, fkey; bool save_visit = dialog_state.visit_items; int button; int cur_item; int was_mouse; int name_width, text_width, full_width, list_width; int result = DLG_EXIT_UNKNOWN; int num_states; bool first = TRUE; WINDOW *dialog; char *prompt = dlg_strclone(cprompt); const char **buttons = dlg_ok_labels(); const char *widget_name = "buildlist"; (void) order_mode; /* * Unlike other uses of --visit-items, we have two windows to visit. */ if (dialog_state.visit_cols) dialog_state.visit_cols = 2; memset(&all, 0, sizeof(all)); all.items = items; all.item_no = item_no; if (dialog_vars.default_item != 0) { cur_item = dlg_default_listitem(items); } else { if ((cur_item = first_item(&all, 0)) < 0) cur_item = first_item(&all, 1); } button = (dialog_state.visit_items ? (items[cur_item].state ? sRIGHT : sLEFT) : dlg_default_button()); dlg_does_output(); dlg_tab_correct_str(prompt); #ifdef KEY_RESIZE retry: #endif all.use_height = list_height; all.use_width = (2 * (dlg_calc_list_width(item_no, items) + 4 + 2 * MARGIN) + 1); all.use_width = MAX(26, all.use_width); if (all.use_height == 0) { /* calculate height without items (4) */ dlg_auto_size(title, prompt, &height, &width, MIN_HIGH, all.use_width); dlg_calc_listh(&height, &all.use_height, item_no); } else { dlg_auto_size(title, prompt, &height, &width, MIN_HIGH + all.use_height, all.use_width); } dlg_button_layout(buttons, &width); dlg_print_size(height, width); dlg_ctl_size(height, width); /* we need at least two states */ if (states == 0 || strlen(states) < 2) states = " *"; num_states = (int) strlen(states); x = dlg_box_x_ordinate(width); y = dlg_box_y_ordinate(height); dialog = dlg_new_window(height, width, y, x); dlg_register_window(dialog, widget_name, binding); dlg_register_buttons(dialog, widget_name, buttons); dlg_mouse_setbase(all.base_x = x, all.base_y = y); dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr); dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr); dlg_draw_title(dialog, title); (void) wattrset(dialog, dialog_attr); dlg_print_autowrap(dialog, prompt, height, width); list_width = (width - 6 * MARGIN - 2) / 2; getyx(dialog, cur_y, cur_x); data[0].box_y = cur_y + 1; data[0].box_x = MARGIN + 1; data[1].box_y = cur_y + 1; data[1].box_x = data[0].box_x + 1 + 2 * MARGIN + list_width; /* * After displaying the prompt, we know how much space we really have. * Limit the list to avoid overwriting the ok-button. */ if (all.use_height + MIN_HIGH > height - cur_y) all.use_height = height - MIN_HIGH - cur_y; if (all.use_height <= 0) all.use_height = 1; for (k = 0; k < 2; ++k) { /* create new window for the list */ data[k].win = dlg_sub_window(dialog, all.use_height, list_width, y + data[k].box_y + 1, x + data[k].box_x + 1); /* draw a box around the list items */ dlg_draw_box(dialog, data[k].box_y, data[k].box_x, all.use_height + 2 * MARGIN, list_width + 2 * MARGIN, menubox_border_attr, menubox_border2_attr); } text_width = 0; name_width = 0; /* Find length of longest item to center buildlist */ for (i = 0; i < item_no; i++) { text_width = MAX(text_width, dlg_count_columns(items[i].text)); name_width = MAX(name_width, dlg_count_columns(items[i].name)); } /* If the name+text is wider than the list is allowed, then truncate * one or both of them. If the name is no wider than 1/4 of the list, * leave it intact. */ all.use_width = (list_width - 6 * MARGIN); if (dialog_vars.no_tags && !dialog_vars.no_items) { full_width = MIN(all.use_width, text_width); } else if (dialog_vars.no_items) { full_width = MIN(all.use_width, name_width); } else { if (text_width >= 0 && name_width >= 0 && all.use_width > 0 && text_width + name_width > all.use_width) { int need = (int) (0.25 * all.use_width); if (name_width > need) { int want = (int) (all.use_width * ((double) name_width) / (text_width + name_width)); name_width = (want > need) ? want : need; } text_width = all.use_width - name_width; } full_width = text_width + name_width; } all.check_x = (all.use_width - full_width) / 2; all.item_x = ((dialog_vars.no_tags ? 0 : (dialog_vars.no_items ? 0 : (name_width + 2))) + all.check_x); /* ensure we are scrolled to show the current choice */ j = MIN(all.use_height, item_no); for (i = 0; i < 2; ++i) { int top_item = 0; if ((items[cur_item].state != 0) == i) { top_item = cur_item - j + 1; if (top_item < 0) top_item = 0; set_top_item(&all, top_item, i); } else { set_top_item(&all, 0, i); } } /* register the new window, along with its borders */ for (i = 0; i < 2; ++i) { dlg_mouse_mkbigregion(data[i].box_y + 1, data[i].box_x, all.use_height, list_width + 2, 2 * KEY_MAX + (i * (1 + all.use_height)), 1, 1, 1 /* by lines */ ); } dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width); while (result == DLG_EXIT_UNKNOWN) { int which = (items[cur_item].state != 0); MY_DATA *moi = data + which; int at_top = index2row(&all, moi->top_index, which); int at_end = index2row(&all, -1, which); int at_bot = skip_rows(&all, at_top, all.use_height, which); dlg_trace_msg("\t** state %d:%d top %d (%d:%d:%d) %d\n", cur_item, item_no - 1, moi->top_index, at_top, at_bot, at_end, which); if (first) { print_both(&all, cur_item); dlg_trace_win(dialog); first = FALSE; } if (button < 0) { /* --visit-items */ int cur_row = index2row(&all, cur_item, which); cur_y = (data[which].box_y + cur_row + 1); if (at_top > 0) cur_y -= at_top; cur_x = (data[which].box_x + all.check_x + 1); dlg_trace_msg("\t...visit row %d (%d,%d)\n", cur_row, cur_y, cur_x); wmove(dialog, cur_y, cur_x); } key = dlg_mouse_wgetch(dialog, &fkey); if (dlg_result_key(key, fkey, &result)) break; was_mouse = (fkey && is_DLGK_MOUSE(key)); if (was_mouse) key -= M_EVENT; if (!was_mouse) { ; } else if (key >= 2 * KEY_MAX) { i = (key - 2 * KEY_MAX) % (1 + all.use_height); j = (key - 2 * KEY_MAX) / (1 + all.use_height); k = row2index(&all, i + at_top, j); dlg_trace_msg("MOUSE column %d, row %d ->item %d\n", j, i, k); if (k >= 0 && j < 2) { if (j != which) { /* * Mouse click was in the other column. */ moi = data + j; fix_top_item(&all, k, j); } which = j; at_top = index2row(&all, moi->top_index, which); at_bot = skip_rows(&all, at_top, all.use_height, which); cur_item = k; print_both(&all, cur_item); key = KEY_TOGGLE; /* force the selected item to toggle */ } else { beep(); continue; } fkey = FALSE; } else if (key >= KEY_MIN) { if (key > KEY_MAX) { if (which == 0) { key = KEY_RIGHTCOL; /* switch to right-column */ fkey = FALSE; } else { key -= KEY_MAX; } } else { if (which == 1) { key = KEY_LEFTCOL; /* switch to left-column */ fkey = FALSE; } } key = dlg_lookup_key(dialog, key, &fkey); } /* * A space toggles the item status. Normally we put the cursor on * the next available item in the same column. But if there are no * more items in the column, move the cursor to the other column. */ if (key == KEY_TOGGLE) { int new_choice; int new_state = items[cur_item].state + 1; if ((new_choice = next_item(&all, cur_item, which)) == cur_item) { new_choice = prev_item(&all, cur_item, which); } dlg_trace_msg("cur_item %d, new_choice:%d\n", cur_item, new_choice); if (new_state >= num_states) new_state = 0; items[cur_item].state = new_state; if (cur_item == moi->top_index) { set_top_item(&all, new_choice, which); } if (new_choice >= 0) { fix_top_item(&all, cur_item, !which); cur_item = new_choice; } print_both(&all, cur_item); dlg_trace_win(dialog); continue; /* wait for another key press */ } /* * Check if key pressed matches first character of any item tag in * list. If there is more than one match, we will cycle through * each one as the same key is pressed repeatedly. */ found = FALSE; if (!fkey) { if (button < 0 || !dialog_state.visit_items) { for (j = cur_item + 1; j < item_no; j++) { if (check_hotkey(items, j, which)) { found = TRUE; i = j; break; } } if (!found) { for (j = 0; j <= cur_item; j++) { if (check_hotkey(items, j, which)) { found = TRUE; i = j; break; } } } if (found) dlg_flush_getc(); } else if ((j = dlg_char_to_button(key, buttons)) >= 0) { button = j; ungetch('\n'); continue; } } /* * A single digit (1-9) positions the selection to that line in the * current screen. */ if (!found && (key <= '9') && (key > '0') && (key - '1' < at_bot)) { found = TRUE; i = key - '1'; } if (!found && fkey) { switch (key) { case DLGK_FIELD_PREV: if ((button == sRIGHT) && dialog_state.visit_items) { key = DLGK_GRID_LEFT; button = sLEFT; } else { button = dlg_prev_button(buttons, button); dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width); if (button == sRIGHT) { key = DLGK_GRID_RIGHT; } else { continue; } } break; case DLGK_FIELD_NEXT: if ((button == sLEFT) && dialog_state.visit_items) { key = DLGK_GRID_RIGHT; button = sRIGHT; } else { button = dlg_next_button(buttons, button); dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width); if (button == sLEFT) { key = DLGK_GRID_LEFT; } else { continue; } } break; } } if (!found && fkey) { i = cur_item; found = TRUE; switch (key) { case DLGK_GRID_LEFT: i = closest_item(&all, cur_item, 0); fix_top_item(&all, i, 0); break; case DLGK_GRID_RIGHT: i = closest_item(&all, cur_item, 1); fix_top_item(&all, i, 1); break; case DLGK_PAGE_PREV: if (cur_item > moi->top_index) { i = moi->top_index; } else if (moi->top_index != 0) { int temp = at_top; if ((temp -= all.use_height) < 0) temp = 0; i = row2index(&all, temp, which); } break; case DLGK_PAGE_NEXT: if ((at_end - at_bot) < all.use_height) { i = next_item(&all, row2index(&all, at_end, which), which); } else { i = next_item(&all, row2index(&all, at_bot, which), which); at_top = at_bot; set_top_item(&all, next_item(&all, row2index(&all, at_top, which), which), which); at_bot = skip_rows(&all, at_top, all.use_height, which); at_bot = MIN(at_bot, at_end); } break; case DLGK_ITEM_FIRST: i = first_item(&all, which); break; case DLGK_ITEM_LAST: i = last_item(&all, which); break; case DLGK_ITEM_PREV: i = prev_item(&all, cur_item, which); if (stop_prev(&all, cur_item, which)) continue; break; case DLGK_ITEM_NEXT: i = next_item(&all, cur_item, which); break; default: found = FALSE; break; } } if (found) { if (i != cur_item) { int now_at = index2row(&all, i, which); int oops = item_no; int old_item; dlg_trace_msg("<--CHOICE %d\n", i); dlg_trace_msg("<--topITM %d\n", moi->top_index); dlg_trace_msg("<--now_at %d\n", now_at); dlg_trace_msg("<--at_top %d\n", at_top); dlg_trace_msg("<--at_bot %d\n", at_bot); if (now_at >= at_bot) { while (now_at >= at_bot) { if ((at_bot - at_top) >= all.use_height) { set_top_item(&all, next_item(&all, moi->top_index, which), which); } at_top = index2row(&all, moi->top_index, which); at_bot = skip_rows(&all, at_top, all.use_height, which); dlg_trace_msg("...at_bot %d (now %d vs %d)\n", at_bot, now_at, at_end); dlg_trace_msg("...topITM %d\n", moi->top_index); dlg_trace_msg("...at_top %d (diff %d)\n", at_top, at_bot - at_top); if (at_bot >= at_end) { /* * If we bumped into the end, move the top-item * down by one line so that we can display the * last item in the list. */ if ((at_bot - at_top) > all.use_height) { set_top_item(&all, next_item(&all, moi->top_index, which), which); } else if (at_top > 0 && (at_bot - at_top) >= all.use_height) { set_top_item(&all, next_item(&all, moi->top_index, which), which); } break; } if (--oops < 0) { dlg_trace_msg("OOPS-forward\n"); break; } } } else if (now_at < at_top) { while (now_at < at_top) { old_item = moi->top_index; set_top_item(&all, prev_item(&all, moi->top_index, which), which); at_top = index2row(&all, moi->top_index, which); dlg_trace_msg("...at_top %d (now %d)\n", at_top, now_at); dlg_trace_msg("...topITM %d\n", moi->top_index); if (moi->top_index >= old_item) break; if (at_top <= now_at) break; if (--oops < 0) { dlg_trace_msg("OOPS-backward\n"); break; } } } dlg_trace_msg("-->now_at %d\n", now_at); cur_item = i; print_both(&all, cur_item); } dlg_trace_win(dialog); continue; /* wait for another key press */ } if (fkey) { switch (key) { case DLGK_ENTER: result = dlg_enter_buttoncode(button); break; #ifdef KEY_RESIZE case KEY_RESIZE: /* reset data */ height = old_height; width = old_width; /* repaint */ dlg_clear(); dlg_del_window(dialog); refresh(); dlg_mouse_free_regions(); goto retry; #endif default: if (was_mouse) { if ((key2 = dlg_ok_buttoncode(key)) >= 0) { result = key2; break; } beep(); } } } else { beep(); } } dialog_state.visit_cols = save_visit; dlg_del_window(dialog); dlg_mouse_free_regions(); free(prompt); *current_item = cur_item; return result; }
static gmx_bool do_ascread(t_fileio *fio, void *item, int nitem, int eio, const char *desc, const char *srcfile, int line) { FILE *fp = fio->fp; int i, m, res = 0, *iptr, ix; gmx_int64_t s; double d, x; char c; real *ptr; unsigned char *ucptr; char *cptr; #define NEXT_ITEM_BUF_LEN 128 char ni_buf[NEXT_ITEM_BUF_LEN]; gmx_fio_check_nitem(eio, nitem, srcfile, line); switch (eio) { case eioREAL: case eioFLOAT: case eioDOUBLE: res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%lf", &d); if (item) { *((real *) item) = d; } break; case eioINT: res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%d", &i); if (item) { *((int *) item) = i; } break; case eioINT64: res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%"GMX_SCNd64, &s); if (item) { *((gmx_int64_t *) item) = s; } break; case eioUCHAR: res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%c", &c); if (item) { *((unsigned char *) item) = (unsigned char)c; } break; case eioNUCHAR: ucptr = (unsigned char *) item; for (i = 0; (i < nitem); i++) { res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%d", &ix); if (item) { ucptr[i] = ix; } } break; case eioUSHORT: res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%d", &i); if (item) { *((unsigned short *) item) = i; } break; case eioRVEC: ptr = (real *) item; for (m = 0; (m < DIM); m++) { res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%lf\n", &x); ptr[m] = x; } break; case eioNRVEC: for (i = 0; (i < nitem); i++) { ptr = ((rvec *) item)[i]; for (m = 0; (m < DIM); m++) { res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%lf\n", &x); if (item) { ptr[m] = x; } } } break; case eioIVEC: iptr = (int *) item; for (m = 0; (m < DIM); m++) { res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%d\n", &ix); if (item) { iptr[m] = ix; } } break; case eioSTRING: cptr = next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN); if (item) { decode_string(strlen(cptr) + 1, (char *) item, cptr); /* res = sscanf(cptr,"%s",(char *)item);*/ res = 1; } break; default: gmx_fio_fe(fio, eio, desc, srcfile, line); } if ((res <= 0) && fio->bDebug) { fprintf(stderr, "Error reading %s %s from file %s (source %s, line %d)\n", eioNames[eio], desc, fio->fn, srcfile, line); } return (res > 0); }
static bool do_ascread(void *item,int nitem,int eio, const char *desc,const char *srcfile,int line) { FILE *fp = curfio->fp; int i,m,res=0,*iptr,ix; gmx_step_t s; double d,x; real *ptr; unsigned char uc,*ucptr; char *cptr; check_nitem(); switch (eio) { case eioREAL: case eioDOUBLE: res = sscanf(next_item(fp),"%lf",&d); if (item) *((real *)item) = d; break; case eioINT: res = sscanf(next_item(fp),"%d",&i); if (item) *((int *)item) = i; break; case eioGMX_STEP_T: res = sscanf(next_item(fp),gmx_step_pfmt,&s); if (item) *((gmx_step_t *)item) = s; break; case eioUCHAR: res = sscanf(next_item(fp),"%c",&uc); if (item) *((unsigned char *)item) = uc; break; case eioNUCHAR: ucptr = (unsigned char *)item; for(i=0; (i<nitem); i++) { res = sscanf(next_item(fp),"%d",&ix); if (item) ucptr[i] = ix; } break; case eioUSHORT: res = sscanf(next_item(fp),"%d",&i); if (item) *((unsigned short *)item) = i; break; case eioRVEC: ptr = (real *)item; for(m=0; (m<DIM); m++) { res = sscanf(next_item(fp),"%lf\n",&x); ptr[m] = x; } break; case eioNRVEC: for(i=0; (i<nitem); i++) { ptr = ((rvec *)item)[i]; for(m=0; (m<DIM); m++) { res = sscanf(next_item(fp),"%lf\n",&x); if (item) ptr[m] = x; } } break; case eioIVEC: iptr = (int *)item; for(m=0; (m<DIM); m++) { res = sscanf(next_item(fp),"%d\n",&ix); if (item) iptr[m] = ix; } break; case eioSTRING: cptr = next_item(fp); if (item) { decode_string(strlen(cptr)+1,(char *)item,cptr); /* res = sscanf(cptr,"%s",(char *)item);*/ res = 1; } break; default: FE(); } if ((res <= 0) && curfio->bDebug) fprintf(stderr,"Error reading %s %s from file %s (source %s, line %d)\n", eioNames[eio],desc,curfio->fn,srcfile,line); return (res > 0); }
/** * @brief Parse a @a button definition. * * Syntax: button=image,x,y,width,height,message * * @param in definition to be analyzed * * @return 0 (ok) or 1 (error) */ static int item_button(char *in) { unsigned char fname[256]; unsigned char file[512]; int x, y, w, h, message; char msg[32]; wItem *item; if (!window_item("button")) return 1; if (in_window("video")) return 1; if (in_window("menu")) return 1; cutItem(in, fname, ',', 0); x = cutItemToInt(in, ',', 1); y = cutItemToInt(in, ',', 2); w = cutItemToInt(in, ',', 3); h = cutItemToInt(in, ',', 4); cutItem(in, msg, ',', 5); message = appFindMessage(msg); if (message == -1) { skin_error(MSGTR_SKIN_UnknownMessage, msg); return 1; } mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[skin] button image: %s %d,%d\n", fname, x, y); mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[skin] message: %s (#%d)\n", msg, message); mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[skin] size: %dx%d\n", w, h); item = next_item(); if (!item) return 1; item->type = itButton; item->x = x; item->y = y; item->width = w; item->height = h; item->message = message; item->pressed = btnReleased; if (item->message == evPauseSwitchToPlay) item->pressed = btnDisabled; item->Bitmap.Image = NULL; if (strcmp(fname, "NULL") != 0) { av_strlcpy(file, path, sizeof(file)); av_strlcat(file, fname, sizeof(file)); if (skinImageRead(file, &item->Bitmap) != 0) return 1; mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[skin] (bitmap: %lux%lu)\n", item->Bitmap.Width, item->Bitmap.Height); } return 0; }
int32_t read_genome(char* read_genome_fname, uintptr_t unfiltered_indiv_ct, uintptr_t* indiv_exclude, uintptr_t indiv_ct, char* person_ids, uintptr_t max_person_id_len, uintptr_t* cluster_merge_prevented, double* cluster_sdistances, double* mds_plot_dmatrix_copy, uint32_t neighbor_n2, double* neighbor_quantiles, uint32_t* neighbor_qindices, uint32_t* ppc_fail_counts, double min_ppc, uintptr_t cluster_ct, uint32_t* cluster_starts, uint32_t* indiv_to_cluster) { unsigned char* wkspace_mark = wkspace_base; gzFile gz_infile = NULL; uint32_t neighbor_load_quantiles = neighbor_quantiles && cluster_sdistances; uint32_t ppc_warning = 0; uintptr_t loaded_entry_ct = 0; uint32_t ppc_fail = 0; char* idbuf = &(tbuf[MAXLINELEN]); char* sorted_ids; uint32_t* id_map; char* bufptr; char* fam_id; char* indiv_id; uint32_t indiv_idx1; uint32_t indiv_idx2; double cur_ibs; double cur_ppc; uintptr_t tcoord; uint32_t uii; int32_t ii; int32_t retval; retval = sort_item_ids(&sorted_ids, &id_map, unfiltered_indiv_ct, indiv_exclude, unfiltered_indiv_ct - indiv_ct, person_ids, max_person_id_len, 0, 1, strcmp_deref); if (retval) { goto read_genome_ret_1; } if (gzopen_checked(&gz_infile, read_genome_fname, "rb")) { goto read_genome_ret_OPEN_FAIL; } tbuf[MAXLINELEN - 1] = ' '; // header line do { if (!gzgets(gz_infile, tbuf, MAXLINELEN)) { goto read_genome_ret_READ_FAIL; } if (!tbuf[MAXLINELEN - 1]) { goto read_genome_ret_INVALID_FORMAT_3; } bufptr = skip_initial_spaces(tbuf); } while (is_eoln_kns(*bufptr)); // a little bit of input validation if (memcmp(bufptr, "FID1", 4)) { logprint("Error: Invalid --read-genome input file.\n"); goto read_genome_ret_INVALID_FORMAT; } while (gzgets(gz_infile, tbuf, MAXLINELEN)) { if (!tbuf[MAXLINELEN - 1]) { goto read_genome_ret_INVALID_FORMAT_3; } fam_id = skip_initial_spaces(tbuf); if (is_eoln_kns(*fam_id)) { continue; } indiv_id = next_item(fam_id); bufptr = next_item_mult(indiv_id, 2); if (no_more_items(bufptr)) { goto read_genome_ret_INVALID_FORMAT_4; } ii = bsearch_fam_indiv(idbuf, sorted_ids, max_person_id_len, indiv_ct, fam_id, indiv_id); if (ii == -1) { continue; } indiv_idx1 = id_map[(uint32_t)ii]; fam_id = next_item(indiv_id); indiv_id = bufptr; ii = bsearch_fam_indiv(idbuf, sorted_ids, max_person_id_len, indiv_ct, fam_id, indiv_id); if (ii == -1) { continue; } indiv_idx2 = id_map[(uint32_t)ii]; if (indiv_idx2 == indiv_idx1) { logprint("Error: FID1/IID1 matches FID2/IID2 in --read-genome input file line.\n"); goto read_genome_ret_INVALID_FORMAT; } bufptr = next_item_mult(indiv_id, 8); // distance fam_id = next_item(bufptr); // repurposed to PPC test value if (no_more_items(fam_id)) { goto read_genome_ret_INVALID_FORMAT_4; } if (min_ppc != 0.0) { if (sscanf(fam_id, "%lg", &cur_ppc) != 1) { logprint("Error: Invalid PPC test value in --read-genome input file.\n"); goto read_genome_ret_INVALID_FORMAT; } ppc_fail = (cur_ppc < min_ppc)? 1 : 0; if (ppc_fail && ppc_fail_counts) { ppc_fail_counts[indiv_idx1] += 1; ppc_fail_counts[indiv_idx2] += 1; } } if (sscanf(bufptr, "%lg", &cur_ibs) != 1) { logprint("Error: Invalid IBS value in --read-genome input file.\n"); goto read_genome_ret_INVALID_FORMAT; } if (neighbor_load_quantiles) { update_neighbor(indiv_ct, neighbor_n2, indiv_idx1, indiv_idx2, cur_ibs, neighbor_quantiles, neighbor_qindices); } loaded_entry_ct++; if (cluster_ct) { indiv_idx1 = indiv_to_cluster[indiv_idx1]; indiv_idx2 = indiv_to_cluster[indiv_idx2]; if (indiv_idx1 == indiv_idx2) { if (ppc_fail && (!ppc_warning)) { logprint("Warning: Initial cluster assignment violates PPC test constraint.\n"); ppc_warning = 1; } continue; } } if (indiv_idx2 < indiv_idx1) { uii = indiv_idx1; indiv_idx1 = indiv_idx2; indiv_idx2 = uii; } tcoord = tri_coord_no_diag(indiv_idx1, indiv_idx2); if (ppc_fail) { set_bit_ul(cluster_merge_prevented, tcoord); } if (cluster_sdistances) { cluster_sdistances[tcoord] += cur_ibs; } } if (!gzeof(gz_infile)) { goto read_genome_ret_READ_FAIL; } if (loaded_entry_ct != (indiv_ct * (indiv_ct - 1)) / 2) { sprintf(logbuf, "Error: %s does not include all individual pairs.\n", read_genome_fname); goto read_genome_ret_INVALID_FORMAT_2; } if (cluster_sdistances) { cluster_dist_divide(indiv_ct, cluster_ct, cluster_starts, cluster_sdistances); } while (0) { read_genome_ret_OPEN_FAIL: retval = RET_OPEN_FAIL; break; read_genome_ret_READ_FAIL: retval = RET_READ_FAIL; break; read_genome_ret_INVALID_FORMAT_4: sprintf(logbuf, "Error: Fewer entries than expected in %s line.\n", read_genome_fname); logprintb(); retval = RET_INVALID_FORMAT; break; read_genome_ret_INVALID_FORMAT_3: sprintf(logbuf, "Error: Pathologically long line in %s.\n", read_genome_fname); read_genome_ret_INVALID_FORMAT_2: logprintb(); read_genome_ret_INVALID_FORMAT: retval = RET_INVALID_FORMAT; break; } read_genome_ret_1: wkspace_reset(wkspace_mark); gzclose_cond(gz_infile); return retval; }
int32_t load_clusters(char* fname, uintptr_t unfiltered_indiv_ct, uintptr_t* indiv_exclude, uintptr_t indiv_ct, char* person_ids, uintptr_t max_person_id_len, uint32_t mwithin_col, uint32_t keep_na, uintptr_t* cluster_ct_ptr, uint32_t** cluster_map_ptr, uint32_t** cluster_starts_ptr, char** cluster_ids_ptr, uintptr_t* max_cluster_id_len_ptr) { unsigned char* wkspace_mark = wkspace_base; FILE* infile = NULL; uintptr_t indiv_ctl = (indiv_ct + (BITCT - 1)) / BITCT; uintptr_t topsize = 0; int32_t retval = 0; char* idbuf = &(tbuf[MAXLINELEN]); uintptr_t max_cluster_id_len = 0; uintptr_t assigned_ct = 0; uintptr_t cluster_ct = 0; Ll_str* cluster_names = NULL; uintptr_t* already_seen; char* cluster_ids; uint32_t* cluster_map; uint32_t* cluster_starts; uint32_t* tmp_cluster_starts; uintptr_t topsize_bak; Ll_str* llptr; char* sorted_ids; uint32_t* id_map; char* fam_id; char* indiv_id; char* cluster_name_ptr; uintptr_t ulii; int32_t sorted_idx; uint32_t indiv_uidx; uint32_t slen; uint32_t uii; sorted_ids = (char*)top_alloc(&topsize, indiv_ct * max_person_id_len); if (!sorted_ids) { goto load_clusters_ret_NOMEM; } id_map = (uint32_t*)top_alloc(&topsize, indiv_ct * sizeof(int32_t)); if (!id_map) { goto load_clusters_ret_NOMEM; } topsize_bak = topsize; already_seen = (uintptr_t*)top_alloc(&topsize, indiv_ctl * sizeof(intptr_t)); if (!already_seen) { goto load_clusters_ret_NOMEM; } fill_ulong_zero(already_seen, indiv_ctl); memcpy(sorted_ids, person_ids, indiv_ct * max_person_id_len); wkspace_left -= topsize; retval = sort_item_ids_noalloc(sorted_ids, id_map, unfiltered_indiv_ct, indiv_exclude, indiv_ct, person_ids, max_person_id_len, 0, 0, strcmp_deref); wkspace_left += topsize; if (retval) { goto load_clusters_ret_1; } // two-pass load // 1. load cluster names, track longest length, validate format, verify no // individual ID appears multiple times // intermission. sort cluster names, purge duplicates, allocate memory for // return values // 2. populate return name arrays if (fopen_checked(&infile, fname, "r")) { goto load_clusters_ret_OPEN_FAIL; } tbuf[MAXLINELEN - 1] = ' '; if (!mwithin_col) { mwithin_col = 1; } while (fgets(tbuf, MAXLINELEN, infile)) { if (!tbuf[MAXLINELEN - 1]) { logprint("Error: Pathologically long line in --within file.\n"); goto load_clusters_ret_INVALID_FORMAT; } fam_id = skip_initial_spaces(tbuf); if (is_eoln_kns(*fam_id)) { continue; } indiv_id = next_item(fam_id); cluster_name_ptr = next_item_mult(indiv_id, mwithin_col); if (no_more_items_kns(cluster_name_ptr)) { logprint("Error: Fewer entries than expected in --within file line.\n"); goto load_clusters_ret_INVALID_FORMAT; } sorted_idx = bsearch_fam_indiv(idbuf, sorted_ids, max_person_id_len, indiv_ct, fam_id, indiv_id); if (sorted_idx == -1) { continue; } if (is_set(already_seen, sorted_idx)) { idbuf[strlen_se(fam_id)] = ' '; sprintf(logbuf, "Error: Duplicate individual %s in --within file.\n", idbuf); logprintb(); goto load_clusters_ret_INVALID_FORMAT; } set_bit_noct(already_seen, sorted_idx); slen = strlen_se(cluster_name_ptr); if ((!keep_na) && (slen == 2) && (!memcmp(cluster_name_ptr, "NA", 2))) { // postponed to here because, even without 'keep-NA', we do not want to // ignore cluster=NA lines for the purpose of detecting duplicate indivs continue; } if (slen >= max_cluster_id_len) { max_cluster_id_len = slen + 1; } llptr = top_alloc_llstr(&topsize, slen + 1); llptr->next = cluster_names; memcpyx(llptr->ss, cluster_name_ptr, slen, '\0'); cluster_names = llptr; assigned_ct++; } if (!feof(infile)) { goto load_clusters_ret_READ_FAIL; } if (cluster_names) { *max_cluster_id_len_ptr = max_cluster_id_len; wkspace_left -= topsize; if (wkspace_alloc_c_checked(cluster_ids_ptr, assigned_ct * max_cluster_id_len)) { goto load_clusters_ret_NOMEM2; } cluster_ids = *cluster_ids_ptr; for (ulii = 0; ulii < assigned_ct; ulii++) { strcpy(&(cluster_ids[ulii * max_cluster_id_len]), cluster_names->ss); cluster_names = cluster_names->next; } // deallocate cluster ID linked list and duplicate indiv ID detector from // top of stack, allocate cluster size tracker wkspace_left += topsize; topsize = topsize_bak; tmp_cluster_starts = (uint32_t*)top_alloc(&topsize, assigned_ct * sizeof(int32_t)); if (!tmp_cluster_starts) { goto load_clusters_ret_NOMEM; } wkspace_left -= topsize; // may as well use natural sort of cluster names qsort(cluster_ids, assigned_ct, max_cluster_id_len, strcmp_natural); cluster_ct = collapse_duplicate_ids(cluster_ids, assigned_ct, max_cluster_id_len, tmp_cluster_starts); *cluster_ct_ptr = cluster_ct; // shrink wkspace_reset(cluster_ids); wkspace_alloc_c_checked(cluster_ids_ptr, cluster_ct * max_cluster_id_len); if (wkspace_alloc_ui_checked(cluster_map_ptr, assigned_ct * sizeof(int32_t)) || wkspace_alloc_ui_checked(cluster_starts_ptr, (cluster_ct + 1) * sizeof(int32_t))) { goto load_clusters_ret_NOMEM2; } wkspace_left += topsize; cluster_map = *cluster_map_ptr; cluster_starts = *cluster_starts_ptr; memcpy(cluster_starts, tmp_cluster_starts, cluster_ct * sizeof(int32_t)); cluster_starts[cluster_ct] = assigned_ct; rewind(infile); // second pass while (fgets(tbuf, MAXLINELEN, infile)) { fam_id = skip_initial_spaces(tbuf); if (is_eoln_kns(*fam_id)) { continue; } indiv_id = next_item(fam_id); cluster_name_ptr = next_item_mult(indiv_id, mwithin_col); slen = strlen_se(cluster_name_ptr); if ((!keep_na) && (slen == 2) && (!memcmp(cluster_name_ptr, "NA", 2))) { continue; } sorted_idx = bsearch_fam_indiv(idbuf, sorted_ids, max_person_id_len, indiv_ct, fam_id, indiv_id); if (sorted_idx == -1) { continue; } indiv_uidx = id_map[(uint32_t)sorted_idx]; cluster_name_ptr[slen] = '\0'; sorted_idx = bsearch_str_natural(cluster_name_ptr, cluster_ids, max_cluster_id_len, 0, cluster_ct - 1); uii = tmp_cluster_starts[(uint32_t)sorted_idx]; tmp_cluster_starts[(uint32_t)sorted_idx] += 1; cluster_map[uii] = indiv_uidx; } if (!feof(infile)) { goto load_clusters_ret_READ_FAIL; } for (ulii = 0; ulii < cluster_ct; ulii++) { if (cluster_starts[ulii + 1] - cluster_starts[ulii] > 1) { #ifdef __cplusplus std::sort(&(cluster_map[cluster_starts[ulii]]), &(cluster_map[cluster_starts[ulii + 1]])); #else qsort(&(cluster_map[cluster_starts[ulii]]), cluster_starts[ulii + 1] - cluster_starts[ulii], sizeof(int32_t), intcmp); #endif } } sprintf(logbuf, "--within: %" PRIuPTR " cluster%s loaded, covering a total of %" PRIuPTR " %s.\n", cluster_ct, (cluster_ct == 1)? "" : "s", assigned_ct, species_str(assigned_ct)); logprintb(); } else { logprint("Warning: No individuals named in --within file remain in the current analysis.\n"); } while (0) { load_clusters_ret_NOMEM2: wkspace_left += topsize; load_clusters_ret_NOMEM: retval = RET_NOMEM; break; load_clusters_ret_OPEN_FAIL: retval = RET_OPEN_FAIL; break; load_clusters_ret_READ_FAIL: retval = RET_READ_FAIL; break; load_clusters_ret_INVALID_FORMAT: retval = RET_INVALID_FORMAT; break; } load_clusters_ret_1: if (retval) { wkspace_reset(wkspace_mark); } fclose_cond(infile); return retval; }
/** * @brief Parse a @a potmeter definition. * * Syntax: potmeter=phases,numphases,default,x,y,width,height,message * * @param in definition to be analyzed * * @return 0 (ok) or 1 (error) */ static int item_potmeter(char *in) { unsigned char phfname[256]; unsigned char buf[512]; int ph, d, x, y, w, h, message; wItem *item; if (!window_item("potmeter")) return 1; if (in_window("video")) return 1; if (in_window("menu")) return 1; cutItem(in, phfname, ',', 0); ph = cutItemToInt(in, ',', 1); d = cutItemToInt(in, ',', 2); x = cutItemToInt(in, ',', 3); y = cutItemToInt(in, ',', 4); w = cutItemToInt(in, ',', 5); h = cutItemToInt(in, ',', 6); cutItem(in, buf, ',', 7); message = appFindMessage(buf); if (message == -1) { skin_error(MSGTR_SKIN_UnknownMessage, buf); return 1; } mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[skin] potmeter image: %s %d,%d %dx%d\n", phfname, x, y, w, h); mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[skin] numphases: %d, default: %d%%\n", ph, d); mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[skin] message: %s (#%d)\n", buf, message); item = next_item(); if (!item) return 1; item->type = itPotmeter; item->x = x; item->y = y; item->width = w; item->height = h; item->numphases = ph; item->value = (float)d; item->message = message; item->Bitmap.Image = NULL; if (strcmp(phfname, "NULL") != 0) { av_strlcpy(buf, path, sizeof(buf)); av_strlcat(buf, phfname, sizeof(buf)); if (skinImageRead(buf, &item->Bitmap) != 0) return 1; mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[skin] (bitmap: %lux%lu)\n", item->Bitmap.Width, item->Bitmap.Height); } return 0; }