/*** map * Mapping functional form. Accepts a single function argument. Input to the * form should always be in the form of a list, and the return value will be * the result of applying the argument function to each element in the list. * * map{ f } : < x, y, z > = < f : x, f : y, f : z > */ struct value *map(struct list *args, struct value *in) { struct value *out = NULL; struct function *f = list_get(args, 0); struct list *l = NULL; struct cursor *c; // First ensure valid input if(args->count != 1 || in->type != SEQ_VAL) { value_delete(in); return value_new(); } // Otherwise create an output list by applying f to each element of in out = value_new(); out->type = SEQ_VAL; out->data.seq_val = list_new(); l = in->data.seq_val; for(c = cursor_new_front(l); cursor_valid(c); cursor_next(c)) list_push_back(out->data.seq_val, function_exec(f, value_copy(cursor_get(c)))); value_delete(in); cursor_delete(c); return out; }
// Deletes a symtable void symtable_delete(struct symtable *table) { int i; struct list *list = NULL; struct cursor *c = NULL; struct symtable_entry *entry = NULL; if(!table) return; for(i = 0; i < SYMTABLE_SIZE; i++) { list = table->entries[i]; for(c = cursor_new_front(list); cursor_valid(c); cursor_next(c)) { entry = cursor_get(c); free(entry->name); function_delete(entry->data); free(entry); } list_delete(list); cursor_delete(c); } free(table); }
static void query_callback (GObject *object, GAsyncResult *result, gpointer user_data) { NemoSearchEngineTracker *tracker; TrackerSparqlConnection *connection; TrackerSparqlCursor *cursor; GError *error = NULL; tracker = NEMO_SEARCH_ENGINE_TRACKER (user_data); connection = TRACKER_SPARQL_CONNECTION (object); cursor = tracker_sparql_connection_query_finish (connection, result, &error); if (error) { tracker->details->query_pending = FALSE; nemo_search_engine_error (NEMO_SEARCH_ENGINE (tracker), error->message); g_error_free (error); return; } if (!cursor) { tracker->details->query_pending = FALSE; nemo_search_engine_finished (NEMO_SEARCH_ENGINE (tracker)); return; } cursor_next (tracker, cursor); }
// Deletes every function in a list void clear_function_list(struct list *args) { struct cursor *c = NULL; for(c = cursor_new_front(args); cursor_valid(c); cursor_next(c)) if(cursor_get(c)) function_delete(cursor_get(c)); list_delete(args); cursor_delete(c); }
static void cursor_callback (GObject *object, GAsyncResult *result, gpointer user_data) { GtkSearchEngineTracker *tracker; GError *error = NULL; TrackerSparqlCursor *cursor; GList *hits; gboolean success; gdk_threads_enter (); tracker = GTK_SEARCH_ENGINE_TRACKER (user_data); cursor = TRACKER_SPARQL_CURSOR (object); success = tracker_sparql_cursor_next_finish (cursor, result, &error); if (error) { _gtk_search_engine_error (GTK_SEARCH_ENGINE (tracker), error->message); g_error_free (error); if (cursor) g_object_unref (cursor); gdk_threads_leave (); return; } if (!success) { _gtk_search_engine_finished (GTK_SEARCH_ENGINE (tracker)); if (cursor) g_object_unref (cursor); gdk_threads_leave (); return; } /* We iterate result by result, not n at a time. */ hits = g_list_append (NULL, (gchar*) tracker_sparql_cursor_get_string (cursor, 0, NULL)); _gtk_search_engine_hits_added (GTK_SEARCH_ENGINE (tracker), hits); g_list_free (hits); /* Get next */ cursor_next (tracker, cursor); gdk_threads_leave (); }
int main (int argc, char *argv[]) { struct queryopt opt; struct ofields *fields; struct select_doc *s_doc; struct query_doc *qu_doc; struct db_connect *db_conn; struct output *out; struct db_cursor *db_c; struct results *res; opt.e_skip = 0; // standard opt.e_ret = 0; // standard opt.bsever = 0; opt.blevel = 0; opt.bdate = 0; opt.bdateu = 0; opt.bdatef = 0; opt.bmsg = 0; opt.bskip = 0; opt.bsys = 0; getoptions(argc, argv, &opt); qu_doc = create_query(&opt); // crate query s_doc = create_select(); db_conn = create_conn(); // create connection out = launch_query(&opt, s_doc, qu_doc, db_conn); // launch the query db_c = open_cursor(db_conn, out); // open cursor while (cursor_next(db_c)) { res = read_data(db_c); fields = get_data(res); formater(fields); // formate output free(fields); free(res); } free_cursor(db_c); close_conn(db_conn); free(out); free(s_doc); free(qu_doc); return (0); }
int btree_select(btree_t bt, const void *key, btree_selmode_t mode, void *key_ret, void *data_ret) { const struct btree_def *def = bt->def; check_btree(bt); switch (mode) { case BTREE_CLEAR: bt->slot[0] = -1; break; case BTREE_READ: break; case BTREE_EXACT: case BTREE_LE: if (!trace_path(bt, key, bt->path, bt->slot) && mode == BTREE_EXACT) bt->slot[0] = -1; break; case BTREE_FIRST: cursor_first(bt); break; case BTREE_NEXT: cursor_next(bt); break; } /* Return the data at the cursor */ if (bt->slot[0] >= 0) { if (key_ret) memcpy(key_ret, PAGE_KEY(bt->path[0], bt->slot[0]), def->key_size); if (data_ret) memcpy(data_ret, PAGE_DATA(bt->path[0], bt->slot[0]), def->data_size); return 0; } return 1; }
/*** construct * Sequence construction. Feeds its input to each of its argument functions, * and generate a sequence where each element is the output of one of the * argument functions. * * construct{ f, g } : x = < f : x, g : x > */ struct value *construct(struct list *args, struct value *in) { struct value *out = value_new(); struct cursor *c = NULL; out->type = SEQ_VAL; out->data.seq_val = list_new(); for(c = cursor_new_front(args); cursor_valid(c); cursor_next(c)) { list_push_back(out->data.seq_val, function_exec(cursor_get(c), value_copy(in))); } value_delete(in); cursor_delete(c); return out; }
static void query_callback (GObject *object, GAsyncResult *result, gpointer user_data) { GtkSearchEngineTracker *tracker; TrackerSparqlConnection *connection; TrackerSparqlCursor *cursor; GError *error = NULL; gdk_threads_enter (); tracker = GTK_SEARCH_ENGINE_TRACKER (user_data); tracker->priv->query_pending = FALSE; connection = TRACKER_SPARQL_CONNECTION (object); cursor = tracker_sparql_connection_query_finish (connection, result, &error); g_debug ("Query returned cursor:%p", cursor); if (error) { _gtk_search_engine_error (GTK_SEARCH_ENGINE (tracker), error->message); g_error_free (error); gdk_threads_leave (); return; } if (!cursor) { _gtk_search_engine_finished (GTK_SEARCH_ENGINE (tracker)); gdk_threads_leave (); return; } cursor_next (tracker, cursor); gdk_threads_leave (); }
// Retrieves an entry from the table struct function *symtable_find(struct symtable *table, char *name) { struct list *list = table->entries[hash(name) % table->size]; struct symtable_entry *entry = NULL; struct cursor *c = NULL; for(c = cursor_new_front(list); cursor_valid(c); cursor_next(c)) { entry = cursor_get(c); if(!entry) break; if(!strcmp(entry->name, name)) { cursor_delete(c); return entry->data; } } return NULL; }
// you may want to have a look into /usr/src/linux/drivers/char/console.c void parse_character(termstate_t * term, l4_uint8_t c) { int i; // these must be handled independetly of state switch (c) { case 0: // ignore return; case 7: // bell // todo: impl. me return; case 8: // backspace if (term->cur_x > 0) term->cur_x--; return; case 9: // tab cursor_tab(term); return; case 10: // newline (lf) case 11: // ? case 12: // ? cursor_nl(term); return; case 13: // do a cr here, maybe a smart one (+ lf) cursor_cr(term); return; case 27: // ESC term->esc_state = ESesc; return; } // now check for the state-dependant characters switch (term->esc_state) { case ESnormal: // normal character if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa1 && c <= 0xfe)) { if (term->insert_mode == VT100_INSMODE_REPLACE) { set_char(term, c); //vt100_redraw_xy(term, term->cur_x, term->cur_y); cursor_next(term); } else // VT100_INSMODE_INSERT { insert_char(term, c); //vt100_redraw(term); } return; } // switch (c) // { // } break; case ESesc: // normal ESC found term->esc_state = ESnormal; switch (c) { case '[': term->esc_state = ESsquare; return; case 'D': // lf cursor_lf(term); //vt100_redraw(term); return; case 'E': // cr + lf cursor_nl(term); //vt100_redraw(term); return; case 'H': // set tab at current position set_tab( term, term->cur_x ); return; case 'M': rev_scroll(term); //vt100_redraw(term); return; case '7': // save cursor position and attributes term->cur_stored_x = term->cur_x; term->cur_stored_y = term->cur_y; term->cur_stored_attr = term->attrib_mode; return; case '8': // restore saved cursor position and attributes term->cur_x = term->cur_stored_x; term->cur_y = term->cur_stored_y; term->attrib_mode = term->cur_stored_attr; return; case '#': term->esc_state = EShash; return; case 'c': // reset vt to default settings init_termstate(term, term->w, term->phys_h, term->virt_h); return; } case ESsquare: for(i = 0; i < NUM_PAR; i++) term->par[i] = 0; term->used_par = 0; term->esc_state = ESgetpars; term->ques = (c=='?'); if (term->ques) return; // fall-through case ESgetpars: if (c == ';' && term->used_par < NUM_PAR - 1) { term->used_par++; return; } else if (c >= '0' && c <= '9') { term->par[term->used_par] *= 10; term->par[term->used_par] += c - '0'; return; } else term->esc_state = ESgotpars; // fall-through case ESgotpars: term->esc_state = ESnormal; switch (c) { case 'h': if (term->ques) { // handle question commands ending with h for (i = 0; i <= term->used_par; i++) { switch( term->par[i] ) { case 5: // activate inverse screen if (set_mode(term, 1)) //vt100_redraw(term); break; case 6: // origin mode = scroll region term->origin_mode = VT100_ORIGIN_SCROLL; break; case 7: // autowrap mode on term->autowrap = 1; break; case 8: // autorepeat on term->autorepeat = 1; break; case 25: // activate cursor term->cursor_vis = 1; return; default: break; } } return; } else // handle commands without question mark { for(i = 0; i <= term->used_par; i++) { switch( term->par[i] ) { case 4: // insert mode on term->insert_mode = VT100_INSMODE_INSERT; break; case 12: // echo on term->echo = 1; break; case 20: // line feed mode term->newline = 1; break; default: break; } } return; } case 'l': if (term->ques) { for(i = 0; i <= term->used_par; i++) { // handle question commands ending with l switch( term->par[i] ) { case 3: // set 80 column mode, clear screen // todo: impl. resize and use it here break; case 5: // deactivate inverse screen if(set_mode(term, 0)) //vt100_redraw(term); break; case 6: // origin mode = whole screen term->origin_mode = VT100_ORIGIN_GLOBAL; case 7: // autowrap mode off term->autowrap = 0; break; case 8: // autorepeat off term->autorepeat = 0; break; case 25: // deactivate cursor term->cursor_vis = 0; return; default: break; } return; } } else // handle commands without question mark { for( i=0; i <= term->used_par; i++ ) { switch( term->par[i] ) { case 4: // insert mode off term->insert_mode = VT100_INSMODE_REPLACE; break; case 12: // echo off term->echo = 0; break; case 20: // carriage return mode term->newline = 0; break; default: break; } return; } } } if (term->ques) { term->ques = 0; return; } switch (c) { case 'F': // CR + up term->cur_x = 0; // fall through case 'A': // up if (! term->par[0]) term->par[0]++; cursor_up(term); return; case 'E': // CR + down term->cur_x = 0; // fall through case 'B': // down if (! term->par[0]) term->par[0]++; cursor_down(term); return; case 'C': // right // no parameter -> set parameter to default (=1) if (! term->par[0]) term->par[0]++; cursor_right(term); return; case 'D': // left if (! term->par[0]) term->par[0]++; cursor_left(term); return; case 'G': // cursor position horizontal absolute if ( (term->par[0] > 0) && (term->par[0] < term->w) ) term->cur_x = term->par[0]; return; case 'H': // absolute position (x,y) case 'f': // f is the same as H (correct me, if I'm wrong) // well it is the same in the Linux kernel, in theorie the // 'f' commands depend on the PUM (Position unit mode), // which can be characters (seems to be default) or inch if (term->par[0] > 0) // convert to (0, 0) based coords term->par[0]--; if (term->par[1] > 0) term->par[1]--; cursor_move_abs(term, term->par[1], term->par[0]); return; case 'I': // insert tabs ('\t', term->par[0] times) if (!term->par[0]) term->par[0]++; for (i=0;i<term->par[0];i++) cursor_tab(term); return; case 'J': switch(term->par[0]) { case 0: // kill to end of screen clean_to_eos(term); //vt100_redraw(term); return; case 1: // kill from start of screen clean_from_sos(term); //vt100_redraw(term); return; case 2: // kill whole screen clean_screen(term, ' '); //vt100_redraw(term); return; } case 'K': // kill to end of line switch(term->par[0]) { case 0: // clean to end of line clean_to_eol(term); //vt100_redraw(term); return; case 1: // clean from start of line clean_from_sol(term); return; case 2: // erase whole line clean_line(term, term->cur_y); return; } case 'L': // insert lines if (!term->par[0]) term->par[0] = 1; for (i=0; i<term->par[0]; i++) insert_line(term); //vt100_redraw(term); return; case 'M': // erase lines if (!term->par[0]) term->par[0] = 1; for (i=0; i<term->par[0]; i++) erase_line(term); //vt100_redraw(term); return; case 'c': // we were asked to identify the terminal type identify(term); return; case 'g': // erase tab(s) switch(term->par[0]) { case 0: remove_tab( term, term->cur_x ); break; case 3: clear_tabs( term ); break; default: break; } return; case 'm': csi_m(term); return; case 'r': // set scrolling region if (term->par[0] == 0) term->par[0]++; if (term->par[1] == 0) term->par[1] = term->phys_h; /* Minimum allowed region is 2 lines */ if (term->par[0] < term->par[1] && term->par[1] <= term->phys_h) { term->scroll_top = term->par[0] - 1; term->scroll_bottom = term->par[1]; // this line is excluded // if in origin mode, make sure that the cursor is placed // inside the scrolling region immediately if (term->origin_mode == VT100_ORIGIN_SCROLL) { term->cur_x = 0; term->cur_y = term->scroll_top; } } return; } case EShash: term->esc_state = ESnormal; switch(c) { case '8': // fill the screen with 'E' clean_screen(term, 'E'); //vt100_redraw(term); break; } break; } }
int btree_delete(btree_t bt, const void *key) { const struct btree_def *def = bt->def; const int halfsize = def->branches / 2; struct btree_page *path[MAX_HEIGHT] = {0}; int slot[MAX_HEIGHT] = {0}; int h; check_btree(bt); /* Trace a path to the item to be deleted */ if (!key) { if (bt->slot[0] < 0) return 1; memcpy(path, bt->path, sizeof(path)); memcpy(slot, bt->slot, sizeof(slot)); } else if (!trace_path(bt, key, path, slot)) { return 1; } /* Select the next item if we're deleting at the cursor */ if (bt->slot[0] == slot[0] && bt->path[0] == path[0]) cursor_next(bt); /* Delete from the leaf node. If it's still full enough, then we don't * need to do anything else. */ delete_item(path[0], slot[0]); if (path[0]->num_children >= halfsize) return 0; /* Trace back up the tree, fixing underfull nodes. If we can fix by * borrowing, do it and we're done. Otherwise, we need to fix by * merging, which may result in another underfull node, and we need * to continue. */ for (h = 1; h <= bt->root->height; h++) { struct btree_page *p = path[h]; struct btree_page *c = path[h - 1]; int s = slot[h]; if (s > 0) { /* Borrow/merge from lower page */ struct btree_page *d = *PAGE_PTR(p, s - 1); if (d->num_children > halfsize) { move_item(d, d->num_children - 1, c, 0); memcpy(PAGE_KEY(p, s), PAGE_KEY(c, 0), def->key_size); return 0; } merge_pages(d, c); delete_item(p, s); free(c); } else { /* Borrow/merge from higher page */ struct btree_page *d = *PAGE_PTR(p, s + 1); if (d->num_children > halfsize) { move_item(d, 0, c, c->num_children); memcpy(PAGE_KEY(p, s + 1), PAGE_KEY(d, 0), def->key_size); return 0; } merge_pages(c, d); delete_item(p, s + 1); free(d); } if (p->num_children >= halfsize) return 0; } /* If the root contains only a single pointer to another page, * shrink the tree. This does not affect the cursor. */ if (bt->root->height && bt->root->num_children == 1) { struct btree_page *old = bt->root; bt->root = *PAGE_PTR(old, 0); free(old); } return 0; }