Node* DoublyLinkedList::modify_field (Node* node, field_name_t field_name, Node* value, Node*& head) { #ifdef LOGGING string field_name_s = field_name == DATA ? "DATA" : field_name == PREV ? "PREV" : "NEXT"; stringstream ss; if (field_name == DATA) { ss << ( (size_t) value); } else if (value) { ss << node_to_string (value); } else { ss << "/"; } string value_s = ss.str (); string node_s = node_to_string (node); cout << "modify_field (" << node_s << ", " << field_name_s << ", " << value_s << ")" << endl; #endif if (node->n_mods < MAX_MODS) { Node::mod_t mod; mod.version = version; mod.field_name = field_name; mod.value = value; node->mods[node->n_mods++] = mod; } else { Node* copy = new Node (); #ifdef MEASURE_SPACE space += sizeof (*copy); #endif copy_live_node (node, copy, field_name, value, head); if (copy->prev() == 0x0) { head = copy; } #ifdef LOGGING cout << " +> " << node_to_string (copy) << endl; #endif return copy; } #ifdef LOGGING cout << " => " << node_to_string (node) << endl; #endif return node; }
static void node_to_string(struct pnode *p) { char constant_buffer[64]; switch(p->tag) { case constant: if (p->value.constant < 0) { snprintf(constant_buffer, sizeof(constant_buffer), "-%#x", -p->value.constant); } else { snprintf(constant_buffer, sizeof(constant_buffer), "%#x", p->value.constant); } cat_string(constant_buffer); break; case symbol: cat_string(p->value.symbol); break; case unop: cat_string("("); cat_symbol(p->value.unop.op); cat_string(" "); node_to_string(p->value.unop.p0); cat_string(")"); break; case binop: if (p->value.binop.op == CONCAT) { cat_string(evaluate_concatenation(p)); } else { cat_string("("); node_to_string(p->value.binop.p0); cat_symbol(p->value.binop.op); node_to_string(p->value.binop.p1); cat_string(")"); } break; case string: cat_string("\""); cat_string(p->value.string); cat_string("\""); break; case list: default: assert(0); } return; }
int _get_node_value_string(QSP_ARG_DECL char *buf, size_t *buflen_p, spinNodeHandle hNode ) { size_t n_need; // Ensure allocated buffer is large enough for storing the string if( node_to_string(hNode, NULL, &n_need) < 0 ) return -1; if( n_need <= *buflen_p ) { // does this cound the terminating null??? if( node_to_string(hNode, buf, buflen_p) < 0 ) return -1; } else { strcpy(buf,"(too many chars)"); } return 0; }
void setup_macro(struct macro_head *h, int arity, struct pnode *parms) { if (enforce_arity(arity, list_length(h->parms))) { /* push table for the marco parms */ state.stTopDefines = push_symbol_table(state.stTopDefines, state.case_insensitive); /* Now add the macro's declared parameter list to the new defines table. */ if (arity > 0) { struct pnode *pFrom, *pFromH; struct pnode *pTo, *pToH; struct symbol *sym; pTo = parms; for (pFrom = h->parms; pFrom; pFrom = TAIL(pFrom)) { pToH = HEAD(pTo); pFromH = HEAD(pFrom); assert(pFromH->tag == symbol); arg_index = 0; arg_buffer[0] = '\0'; node_to_string(pToH); sym = add_symbol(state.stTopDefines, pFromH->value.symbol); annotate_symbol(sym, strdup(arg_buffer)); pTo = TAIL(pTo); } } state.next_state = state_macro; state.next_buffer.macro = h; state.lst.line.linetype = none; } }
bool xml_document::to_string(buffer& buf, bool pretty_print, const struct node* node, unsigned depth) const { do { if (pretty_print) { // If not the first node... if (node != _M_nodes) { if (!buf.append("\r\n", 2)) { return false; } } } if (!node_to_string(buf, pretty_print, node, depth)) { return false; } if (node->child != -1) { if (!to_string(buf, pretty_print, &(_M_nodes[node->child]), depth + 1)) { return false; } if (pretty_print) { if (!buf.append("\r\n", 2)) { return false; } for (unsigned i = 0; i < depth; i++) { if (!buf.append('\t')) { return false; } } } if (!buf.format("</%.*s>", node->len, _M_buf.data() + node->text)) { return false; } } if (node->next < 0) { return true; } node = &(_M_nodes[node->next]); } while (true); }
/*================================================================ * add_indi_no_cache -- Add new person to database * does not insert into cache * (used by import) * (no user interaction) *==============================================================*/ BOOLEAN add_indi_no_cache (NODE indi) { NODE node, name, refn, sex, body, famc, fams; STRING str, key; split_indi_old(indi, &name, &refn, &sex, &body, &famc, &fams); key = rmvat(nxref(indi)); for (node = name; node; node = nsibling(node)) add_name(nval(node), key); for (node = refn; node; node = nsibling(node)) if (nval(node)) add_refn(nval(node), key); join_indi(indi, name, refn, sex, body, famc, fams); resolve_refn_links(indi); str = node_to_string(indi); store_record(key, str, strlen(str)); stdfree(str); return TRUE; }
int main(int argc, char** argv){ int i; char *filename, *query_string, *node; if(argc == 2){ filename = "-"; } else if(argc != 3){ printf("usage: %s \"query\" \"xml_file\"\n", argv[0]); return 0; } else{ filename = argv[2]; } query_string = argv[1]; doc* document = parse_xml(filename); if(document == NULL) exit(1); register_extended_operators(); list* result = query(query_string, document->root); if(result == NULL) exit(1); for(i=0; i < result->count; i++){ dom_node* t = (dom_node*)get_element_at(result, i); node = node_to_string(t, XML); printf("%s", node); free(node); } if(result) destroy_generic_list(result); if(document != NULL) destroy_dom_tree(document); destroy_dictionary(); destroy_custom_filters(); destroy_custom_operators(); return 0; }
int main() { // get a test document pugi::xml_document doc; doc.load_string("<foo bar='baz'>hey</foo>"); // get contents as std::string (single pass) std::cout << "contents: [" << node_to_string(doc) << "]\n"; // get contents into fixed-size buffer (single pass) char large_buf[128]; std::cout << "contents: [" << node_to_buffer(doc, large_buf, sizeof(large_buf)) << "]\n"; // get contents into fixed-size buffer (single pass, shows truncating behavior) char small_buf[22]; std::cout << "contents: [" << node_to_buffer(doc, small_buf, sizeof(small_buf)) << "]\n"; // get contents into heap-allocated buffer (two passes) char *heap_buf = node_to_buffer_heap(doc); std::cout << "contents: [" << heap_buf << "]\n"; delete[] heap_buf; }
/** \todo Fix memory leak @ node_to_string:186 */ char * node_to_string(Wht *W) { char *buf, *tmp; size_t nn, i, j, len; buf = name_to_string(W); if (W->children == NULL) return buf; len = strlen(buf) + 3; /* [ .. ] \0 */ buf = realloc(buf, sizeof(char) * len); strncat(buf,"[",1); nn = W->children->nn; /* Iterate over children WHTs, stored anti lexigraphically */ for (i = 0; i < nn; i++) { j = nn - i - 1; tmp = node_to_string(W->children->Ws[j]); len += strlen(tmp) + 2; /* , \0*/ buf = realloc(buf, sizeof(char) * len); strncat(buf, tmp, strlen(tmp) + 1); /* Don't add comma for last child */ if (i < nn - 1) strncat(buf, ",", 1); i_free(tmp); } strncat(buf,"]",1); return buf; }
pair < size_t, Node* >DoublyLinkedList::set_data (Node* node, size_t value) { #ifdef LOGGING cout << "v" << version << ": set_data (" << node_to_string (node) << ", " << value << ")" << endl; #endif version_info_t new_version; ++version; new_version.size = versions.back ().size; Node* current_head = head (); Node* modified_node = modify_field (node, DATA, reinterpret_cast < Node* > (value), current_head); if (!modified_node->prev ()) { new_version.head = modified_node; } else { new_version.head = current_head; } versions.push_back (new_version); }
static PyObject *object_to_string(PyObject *object) { PyObject *result = NULL; if (PyUnicode_Check(object)) { Py_INCREF(object); result = object; } else if (PyString_Check(object)) { /* Python DOM binding: string objects must be UTF-8 */ result = PyUnicode_FromEncodedObject(object, "UTF-8", "strict"); } else if (PyFloat_Check(object)) { double d = PyFloat_AS_DOUBLE(object); if (PyNumber_Finite(object)) { if (floor(d) == d) { /* Format as integer */ PyObject *num = PyNumber_Long(object); if (!num) return NULL; result = PyObject_Unicode(num); Py_DECREF(num); } else { /* worst case length calc to ensure no buffer overrun: fmt = %#.<prec>g buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp for any double rep.) len = 1 + prec + 1 + 2 + 5 = 9 + prec If prec=0 the effective precision is 1 (the leading digit is always given), therefore increase by one to 10+prec. */ char buf[32]; /* only 10 + 12 + '\0' is needed, more than enough */ int len; len = sprintf(buf, "%0.12g", d); result = PyUnicode_DecodeASCII(buf, len, "strict"); } } else if (PyNumber_IsNaN(object)) { result = PyUnicode_DecodeASCII("NaN", 3, "strict"); } else if (d < 0) { result = PyUnicode_DecodeASCII("-Infinity", 9, "strict"); } else { result = PyUnicode_DecodeASCII("Infinity", 8, "strict"); } } else if (PyBoolean_Check(object)) { if (PyObject_IsTrue(object)) result = PyUnicode_DecodeASCII("true", 4, "strict"); else result = PyUnicode_DecodeASCII("false", 5, "strict"); } else if (PyInt_Check(object) || PyLong_Check(object)) { result = PyObject_Unicode(object); } else if (PyList_Check(object)) { if (PyList_GET_SIZE(object)) result = object_to_string(PyList_GET_ITEM(object, 0)); else result = PyUnicode_FromUnicode(NULL, 0); } else { /* check for pre-computed string value */ result = PyObject_GetAttrString(object, "stringValue"); if (result == NULL) { /* assume a DOM node, node_to_string() returns empty string if not */ PyErr_Clear(); result = node_to_string(object); } } return result; }
void node_fwrite(Node * node, FILE *output) { String *str = node_to_string(node); fwrite(str->bytes, 1, str->len, output); String_free(&str); }
/* ---------------------------------------------------------------- * procedure_create * * Note: allParameterTypes, parameterModes, parameterNames, and proconfig * are either arrays of the proper types or NULL. We declare them Datum, * not "ArrayType *", to avoid importing array.h into pg_proc_fn.h. * ---------------------------------------------------------------- */ oid_t procedure_create( const char *procedureName, oid_t procNamespace, bool replace, bool returnsSet, oid_t returnType, oid_t languageObjectId, oid_t languageValidator, const char *prosrc, const char *probin, bool isAgg, bool isWindowFunc, bool security_definer, bool isStrict, char volatility, oid_vector_s *parameterTypes, datum_t allParameterTypes, datum_t parameterModes, datum_t parameterNames, struct list *parameterDefaults, datum_t proconfig, float4 procost, float4 prorows) { oid_t retval; int parameterCount; int allParamCount; oid_t* allParams; bool genericInParam = false; bool genericOutParam = false; bool internalInParam = false; bool internalOutParam = false; oid_t variadicType = INVALID_OID; oid_t proowner = get_uid(); acl_s* proacl = NULL; struct relation* rel; struct heap_tuple* tup; struct heap_tuple* oldtup; bool nulls[Natts_pg_proc]; datum_t values[Natts_pg_proc]; bool replaces[Natts_pg_proc]; oid_t relid; struct name procname; struct tuple* tupDesc; bool is_update; struct objaddr myself; struct objaddr referenced; int i; /* * sanity checks */ ASSERT(PTR_VALID(prosrc)); parameterCount = parameterTypes->dim1; if (parameterCount < 0 || parameterCount > FUNC_MAX_ARGS) { ereport(ERROR, ( errcode(E_TOO_MANY_ARGUMENTS), errmsg_plural("functions cannot have more than %d argument", "functions cannot have more than %d arguments", FUNC_MAX_ARGS, FUNC_MAX_ARGS))); } /* note: the above is correct, we do NOT count output arguments */ if (allParameterTypes != PTR_TO_D(NULL)) { /* * We expect the array to be a 1-D OID array; verify that. We don't * need to use deconstruct_array() since the array data is just going * to look like a C array of OID values. */ array_s *allParamArray; allParamArray = (array_s*) D_TO_PTR(allParameterTypes); allParamCount = ARR_DIMS(allParamArray)[0]; if (ARR_NDIM(allParamArray) != 1 || allParamCount <= 0 || ARR_HASNULL(allParamArray) || ARR_ELEMTYPE(allParamArray) != OIDOID) elog(ERROR, "allParameterTypes is not a 1-D oid_t array"); allParams = (oid_t*) ARR_DATA_PTR(allParamArray); ASSERT(allParamCount >= parameterCount); /* we assume caller got the contents right */ } else { allParamCount = parameterCount; allParams = parameterTypes->values; } /* * Do not allow polymorphic return type unless at least one input argument * is polymorphic. Also, do not allow return type INTERNAL unless at * least one input argument is INTERNAL. */ for (i = 0; i < parameterCount; i++) { switch (parameterTypes->values[i]) { case ANYARRAYOID: case ANYELEMENTOID: case ANYNONARRAYOID: case ANYENUMOID: genericInParam = true; break; case INTERNALOID: internalInParam = true; break; } } if (allParameterTypes != PTR_TO_D(NULL)) { for (i = 0; i < allParamCount; i++) { /* * We don't bother to distinguish input and output params here, so * if there is, say, just an input INTERNAL param then we will * still set internalOutParam. This is OK since we don't really * care. */ switch (allParams[i]) { case ANYARRAYOID: case ANYELEMENTOID: case ANYNONARRAYOID: case ANYENUMOID: genericOutParam = true; break; case INTERNALOID: internalOutParam = true; break; } } } if ((is_polymorphic_type(returnType) || genericOutParam) && !genericInParam) { ereport(ERROR, ( errcode(E_INVALID_FUNCTION_DEFINITION), errmsg("cannot determine result data type"), errdetail("A function returning a polymorphic type must have" " at least one polymorphic argument."))); } if ((returnType == INTERNALOID || internalOutParam) && !internalInParam) { ereport(ERROR, ( errcode(E_INVALID_FUNCTION_DEFINITION), errmsg("unsafe use of pseudo-type \"internal\""), errdetail("A function returning \"internal\" must have at" " least one \"internal\" argument."))); } /* * don't allow functions of complex types that have the same name as * existing attributes of the type */ if (parameterCount == 1 && OID_VALID(parameterTypes->values[0]) && (relid = typeid_to_relid(parameterTypes->values[0])) != INVALID_OID && get_attnum(relid, procedureName) != INVALID_ATTR_NR) { ereport(ERROR, ( errcode(E_DUPLICATE_COLUMN), errmsg("\"%s\" is already an attribute of type %s", procedureName, format_type_be(parameterTypes->values[0])))); } if (parameterModes != PTR_TO_D(NULL)) { /* * We expect the array to be a 1-D CHAR array; verify that. We don't * need to use deconstruct_array() since the array data is just going * to look like a C array of char values. */ array_s* modesArray; char* modes; modesArray = (array_s *) D_TO_PTR(parameterModes); if (ARR_NDIM(modesArray) != 1 || ARR_DIMS(modesArray)[0] != allParamCount || ARR_HASNULL(modesArray) || ARR_ELEMTYPE(modesArray) != CHAROID) elog(ERROR, "parameterModes is not a 1-D char array"); modes = (char*) ARR_DATA_PTR(modesArray); /* * Only the last input parameter can be variadic; if it is, save its * element type. Errors here are just elog since caller should have * checked this already. */ for (i = 0; i < allParamCount; i++) { switch (modes[i]) { case PROARGMODE_IN: case PROARGMODE_INOUT: if (OID_VALID(variadicType)) elog(ERROR, "variadic parameter must be last"); break; case PROARGMODE_OUT: case PROARGMODE_TABLE: /* okay */ break; case PROARGMODE_VARIADIC: if (OID_VALID(variadicType)) elog(ERROR, "variadic parameter must be last"); switch (allParams[i]) { case ANYOID: variadicType = ANYOID; break; case ANYARRAYOID: variadicType = ANYELEMENTOID; break; default: variadicType = get_element_type(allParams[i]); if (!OID_VALID(variadicType)) elog(ERROR, "variadic parameter is not an array"); break; } break; default: elog(ERROR, "invalid parameter mode '%c'", modes[i]); break; } } } /* * All seems OK; prepare the data to be inserted into pg_proc. */ for (i = 0; i < Natts_pg_proc; ++i) { nulls[i] = false; values[i] = (datum_t) 0; replaces[i] = true; } namestrcpy(&procname, procedureName); values[Anum_pg_proc_proname - 1] = NAME_TO_D(&procname); values[Anum_pg_proc_pronamespace - 1] = OID_TO_D(procNamespace); values[Anum_pg_proc_proowner - 1] = OID_TO_D(proowner); values[Anum_pg_proc_prolang - 1] = OID_TO_D(languageObjectId); values[Anum_pg_proc_procost - 1] = FLOAT4_TO_D(procost); values[Anum_pg_proc_prorows - 1] = FLOAT4_TO_D(prorows); values[Anum_pg_proc_provariadic - 1] = OID_TO_D(variadicType); values[Anum_pg_proc_proisagg - 1] = BOOL_TO_D(isAgg); values[Anum_pg_proc_proiswindow - 1] = BOOL_TO_D(isWindowFunc); values[Anum_pg_proc_prosecdef - 1] = BOOL_TO_D(security_definer); values[Anum_pg_proc_proisstrict - 1] = BOOL_TO_D(isStrict); values[Anum_pg_proc_proretset - 1] = BOOL_TO_D(returnsSet); values[Anum_pg_proc_provolatile - 1] = CHAR_TO_D(volatility); values[Anum_pg_proc_pronargs - 1] = UINT16_TO_D(parameterCount); values[Anum_pg_proc_pronargdefaults - 1] = UINT16_TO_D(list_length(parameterDefaults)); values[Anum_pg_proc_prorettype - 1] = OID_TO_D(returnType); values[Anum_pg_proc_proargtypes - 1] = PTR_TO_D(parameterTypes); if (allParameterTypes != PTR_TO_D(NULL)) values[Anum_pg_proc_proallargtypes - 1] = allParameterTypes; else nulls[Anum_pg_proc_proallargtypes - 1] = true; if (parameterModes != PTR_TO_D(NULL)) values[Anum_pg_proc_proargmodes - 1] = parameterModes; else nulls[Anum_pg_proc_proargmodes - 1] = true; if (parameterNames != PTR_TO_D(NULL)) values[Anum_pg_proc_proargnames - 1] = parameterNames; else nulls[Anum_pg_proc_proargnames - 1] = true; if (parameterDefaults != NIL) values[Anum_pg_proc_proargdefaults - 1] = CStringGetTextDatum( node_to_string(parameterDefaults)); else nulls[Anum_pg_proc_proargdefaults - 1] = true; values[Anum_pg_proc_prosrc - 1] = CStringGetTextDatum(prosrc); if (probin) values[Anum_pg_proc_probin - 1] = CStringGetTextDatum(probin); else nulls[Anum_pg_proc_probin - 1] = true; if (proconfig != PTR_TO_D(NULL)) values[Anum_pg_proc_proconfig - 1] = proconfig; else nulls[Anum_pg_proc_proconfig - 1] = true; /* * proacl will be determined later */ rel = heap_open(ProcedureRelationId, ROW_EXCL_LOCK); tupDesc = REL_DESC(rel); /* Check for pre-existing definition */ oldtup = search_syscache3( PROCNAMEARGSNSP, PTR_TO_D(procedureName), PTR_TO_D(parameterTypes), OID_TO_D(procNamespace)); if (HT_VALID(oldtup)) { /* There is one; okay to replace it? */ Form_pg_proc oldproc; datum_t proargnames; bool isnull; oldproc = (Form_pg_proc) GET_STRUCT(oldtup); if (!replace) { ereport(ERROR, ( errcode(E_DUPLICATE_FUNCTION), errmsg("function \"%s\" already exists with same argument types", procedureName))); } if (!pg_proc_ownercheck(HEAPTUP_OID(oldtup), proowner)) aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_PROC, procedureName); /* * Not okay to change the return type of the existing proc, since * existing rules, views, etc may depend on the return type. */ if (returnType != oldproc->prorettype || returnsSet != oldproc->proretset) { ereport(ERROR, ( errcode(E_INVALID_FUNCTION_DEFINITION), errmsg("cannot change return type of existing function"), errhint("Use DROP FUNCTION first."))); } /* * If it returns RECORD, check for possible change of record type * implied by OUT parameters */ if (returnType == RECORDOID) { struct tuple* olddesc; struct tuple* newdesc; olddesc = build_function_result_tupdesc_t(oldtup); newdesc = build_function_result_tupdesc_d( allParameterTypes, parameterModes, parameterNames); if (olddesc == NULL && newdesc == NULL) { /* ok, both are runtime-defined RECORDs */ ; } else if (olddesc == NULL || newdesc == NULL || !tupdesc_equal(olddesc, newdesc)) { ereport(ERROR, ( errcode(E_INVALID_FUNCTION_DEFINITION), errmsg("cannot change return type of existing function"), errdetail("Row type defined by OUT parameters is different."), errhint("Use DROP FUNCTION first."))); } } /* * If there were any named input parameters, check to make sure the * names have not been changed, as this could break existing calls. We * allow adding names to formerly unnamed parameters, though. */ proargnames = syscache_attr( PROCNAMEARGSNSP, oldtup, Anum_pg_proc_proargnames, &isnull); if (!isnull) { datum_t proargmodes; char** old_arg_names; char** new_arg_names; int n_old_arg_names; int n_new_arg_names; int j; proargmodes = syscache_attr( PROCNAMEARGSNSP, oldtup, Anum_pg_proc_proargmodes, &isnull); if (isnull) proargmodes = PTR_TO_D(NULL); /* just to be sure */ n_old_arg_names = get_func_input_arg_names( proargnames, proargmodes, &old_arg_names); n_new_arg_names = get_func_input_arg_names( parameterNames, parameterModes, &new_arg_names); for (j = 0; j < n_old_arg_names; j++) { if (old_arg_names[j] == NULL) continue; if (j >= n_new_arg_names || new_arg_names[j] == NULL || strcmp(old_arg_names[j], new_arg_names[j]) != 0) { ereport(ERROR,( errcode(E_INVALID_FUNCTION_DEFINITION), errmsg("cannot change name of input parameter \"%s\"", old_arg_names[j]), errhint("Use DROP FUNCTION first."))); } } } /* * If there are existing defaults, check compatibility: redefinition * must not remove any defaults nor change their types. (Removing a * default might cause a function to fail to satisfy an existing call. * Changing type would only be possible if the associated parameter is * polymorphic, and in such cases a change of default type might alter * the resolved output type of existing calls.) */ if (oldproc->pronargdefaults != 0) { datum_t proargdefaults; struct list* oldDefaults; struct list_cell* oldlc; struct list_cell* newlc; if (list_length(parameterDefaults) < oldproc->pronargdefaults) { ereport(ERROR, ( errcode(E_INVALID_FUNCTION_DEFINITION), errmsg("cannot remove parameter defaults from existing function"), errhint("Use DROP FUNCTION first."))); } proargdefaults = syscache_attr( PROCNAMEARGSNSP, oldtup, Anum_pg_proc_proargdefaults, &isnull); ASSERT(!isnull); oldDefaults = (struct list*) string_to_node( TextD_TO_CSTRING(proargdefaults)); ASSERT(IS_A(oldDefaults, List)); ASSERT(list_length(oldDefaults) == oldproc->pronargdefaults); /* new list can have more defaults than old, advance over 'em */ newlc = list_head(parameterDefaults); for (i = list_length(parameterDefaults) - oldproc->pronargdefaults; i > 0; i--) newlc = lnext(newlc); foreach(oldlc, oldDefaults) { node_n* oldDef; node_n* newDef; oldDef = (node_n*) lfirst(oldlc); newDef = (node_n*) lfirst(newlc); if (expr_type(oldDef) != expr_type(newDef)) { ereport(ERROR,( errcode(E_INVALID_FUNCTION_DEFINITION), errmsg("cannot change data type of existing" " parameter default value"), errhint("Use DROP FUNCTION first."))); } newlc = lnext(newlc); } }