String *sConcat(const String *s, const String *t) { size_t len; String *n = NULL; char *p; const String *vs, *vt; if (s == NULL) { vs = new_String(""); } else { vs = s; } if (t == NULL) { vt = new_String(""); } else { vt = t; } len = sLength(vs) + sLength(vt); if ((p = (char *) GC_MALLOC(sizeof(char) * len + 1)) != NULL) { strcpy(p, vs->characters); strcat(p, vt->characters); if ((n = (String *) GC_MALLOC(sizeof(struct String_))) != NULL) { n->cls = CLS_STRING; n->characters = p; n->length = len; } else { GC_FREE(p); } } return n; }
void set_params(int argc, char * argv[]) { /* {{{ */ struct stat file_stat; if(argc != 3) { usage(argv[0]); exit(SHELL_FALSE); } if(stat(argv[1], &file_stat) == -1) { perror("stat() error for format file"); exit(1); } if(! S_ISREG(file_stat.st_mode)) { fprintf(stderr, "Format file is not a regular file.\n"); exit(1); } if(stat(argv[2], &file_stat) == -1) { perror("stat() error for input file"); exit(1); } if(! S_ISREG(file_stat.st_mode)) { fprintf(stderr, "Input file is not a regular file.\n"); exit(1); } format_file = new_String(argv[1], strlen(argv[1])); input_file = new_String(argv[2], strlen(argv[2])); } /* }}} */
int main(int argc, char *argv[]){ String s1; String s2; char *c1 = NULL; s1 = new_String("This "); s2 = new_String("is a test"); if(!String_appendString(s1, s2)){ fprintf(stderr, "String_appendString(): Uh oh!\n"); } else{ if(String_getChars(s1, &c1)){ printf("%s\n", c1); } else{ fprintf(stderr, "String_getChars(): Uh oh!\n"); } } delete_String(s1); delete_String(s2); return EXIT_SUCCESS; }
void set_params(int argc, char * argv[]) { /* {{{ */ struct stat file_stat; if(argc <= 2 || argc >= 5) { usage(argv[0]); exit(SHELL_FALSE); } /* Format File */ if(stat(argv[1], &file_stat) == -1) { perror("stat() error for format file"); exit(1); } if(! S_ISREG(file_stat.st_mode)) { fprintf(stderr, "Format file is not a regular file.\n"); exit(1); } format_file = new_String(argv[1], strlen(argv[1])); /* Input File */ if(stat(argv[2], &file_stat) == -1) { perror("stat() error for input file"); exit(1); } if(! S_ISREG(file_stat.st_mode)) { fprintf(stderr, "Input file is not a regular file.\n"); exit(1); } input_file = new_String(argv[2], strlen(argv[2])); /* Output File */ if(argc == 4 && strncmp(argv[3], "-", 1) != 0) { if(stat(argv[3], &file_stat) == -1) { if(errno != ENOENT) { perror("stat() error for output file"); exit(1); } } else if(! S_ISREG(file_stat.st_mode)) { fprintf(stderr, "Output file exists, but is not a regular file.\n"); exit(1); } output_file = new_String(argv[3], strlen(argv[3])); } else { output_file = new_String("-", 1); } } /* }}} */
static ktype_t msgpack_read(CTX ctx, msgpack_object obj, ksfp_t *sfp) { switch (obj.type) { case MSGPACK_OBJECT_NIL: KNH_SETv(ctx, sfp[0].o, KNH_NULL); return CLASS_Tdynamic; case MSGPACK_OBJECT_BOOLEAN: sfp[0].bvalue = obj.via.boolean; return CLASS_Boolean; case MSGPACK_OBJECT_NEGATIVE_INTEGER: sfp[0].ivalue = obj.via.i64; return CLASS_Int; case MSGPACK_OBJECT_POSITIVE_INTEGER: sfp[0].uvalue = obj.via.u64; return CLASS_Int; case MSGPACK_OBJECT_DOUBLE: sfp[0].fvalue = obj.via.dec; return CLASS_Float; case MSGPACK_OBJECT_RAW: KNH_SETv(ctx, sfp[0].o, new_String(ctx, obj.via.raw.ptr)); return CLASS_String; case MSGPACK_OBJECT_ARRAY: { msgpack_object *a = obj.via.array.ptr; size_t asize = obj.via.array.size; const msgpack_object *array_end = obj.via.array.ptr + asize; KNH_SETv(ctx, sfp[0].o, new_Array(ctx, CLASS_Tdynamic, asize)); for (; a < array_end; a++) { ktype_t type = msgpack_read(ctx, *a, sfp+1); knh_boxing(ctx, sfp+1, type); knh_Array_add(ctx, sfp[0].a, sfp[1].o); } return CLASS_Array; } case MSGPACK_OBJECT_MAP: { if (obj.via.map.size == 0) break; msgpack_object_kv *map = obj.via.map.ptr; msgpack_object_kv *map_end = obj.via.map.ptr + obj.via.map.size; KNH_SETv(ctx, sfp[0].o, new_DataMap(ctx/*, obj.via.map.size*/)); for (; map < map_end; map++) { const char *key = map->key.via.raw.ptr; ktype_t type = msgpack_read(ctx, map->val, sfp+1); knh_boxing(ctx, sfp+1, type); klr_setesp(ctx, sfp+2); knh_DataMap_set(ctx, sfp[0].m, new_String(ctx, key), sfp[1].o); } return CLASS_Map; } } return CLASS_Tvoid; }
static SV *new_Token(pTHX_ Token *token) { HV *hash = (HV*)new_Hash(); (void)hv_stores(hash, "stype", set(new_Int(token->stype))); (void)hv_stores(hash, "type", set(new_Int(token->info.type))); (void)hv_stores(hash, "kind", set(new_Int(token->info.kind))); (void)hv_stores(hash, "line", set(new_Int(token->finfo.start_line_num))); (void)hv_stores(hash, "has_warnings", set(new_Int(token->info.has_warnings))); (void)hv_stores(hash, "name", set(new_String(token->info.name, strlen(token->info.name)))); (void)hv_stores(hash, "data", set(new_String(token->data.c_str(), strlen(token->data.c_str())))); HV *stash = (HV *)gv_stashpv("Compiler::Lexer::Token", sizeof("Compiler::Lexer::Token") + 1); return sv_bless(new_Ref(hash), stash); }
static METHOD knh__String_get(Ctx *ctx, knh_sfp_t *sfp) { knh_bytes_t base = knh_String_tobytes(sfp[0].s); if(knh_String_isASCII(sfp[0].s)) { size_t n = knh_array_index(ctx, p_int(sfp[1]), knh_String_strlen(sfp[0].s)); base.buf = base.buf + n; base.len = 1; KNH_RETURN(ctx, sfp, new_String(ctx, base, sfp[0].s)); } else { size_t off = knh_array_index(ctx, p_int(sfp[1]), knh_bytes_mlen(base)); knh_bytes_t sub = knh_bytes_mofflen(base, off, 1); KNH_RETURN(ctx, sfp, new_String(ctx, sub, sfp[0].s)); } }
void test_heap_dump() { char *expected, *result; User *u; String *s; Array *a; GC_SAVE_RP; gc(); s = new_String("tim"); u = new_User(103, s); a = (Array *) alloc_Array(2, ARRAY_POINTER); ((void **) a->elements)[0] = s; ((void **) a->elements)[1] = u; ADD_ROOT(s); ADD_ROOT(u); ADD_ROOT(a); expected = "heap2[116,1000]\n" "0000:String[32+4]=\"tim\"\n" "0036:User[32]->[0]\n" "0068:Array[32+2]->[0, 36]\n"; result = calloc(1000, sizeof(char)); heap_dump(result); // printf("\n%s", result); ASSERT_STR(expected, result); GC_RESTORE_RP; free(result); }
static METHOD knh__String_substring(Ctx *ctx, knh_sfp_t *sfp) { knh_bytes_t base = knh_String_tobytes(sfp[0].s); knh_bytes_t sub; if(knh_String_isASCII(sfp[0].s)) { size_t offset = IS_NULL(sfp[1].o) ? 0 : knh_array_index(ctx, sfp[1].ivalue, base.len); sub = knh_bytes_last(base, offset); if(IS_NOTNULL(sfp[2].o)) { size_t len = (size_t)sfp[2].ivalue; if(len < sub.len) sub = knh_bytes_first(sub, len); } } else { // multibytes size_t mlen = knh_bytes_mlen(base); size_t offset = IS_NULL(sfp[1].o) ? 0 : knh_array_index(ctx, sfp[1].ivalue, mlen); size_t length = IS_NULL(sfp[2].o) ? (mlen - offset) : (size_t)sfp[2].ivalue; sub = knh_bytes_mofflen(base, offset, length); } String *s; if(sub.len == 0) { s = TS_EMPTY; } else if(sub.len == base.len) { s = sfp[0].s; } else { s = new_String(ctx, sub, sfp[0].s); } KNH_RETURN(ctx, sfp, s); }
/* @method String XmlReader.lookupNameSpace(String ns) */ METHOD XmlReader_lookupNameSpace(Ctx *ctx, knh_sfp_t *sfp) { xmlTextReaderPtr reader = (xmlTextReaderPtr) p_cptr(sfp[0]); xmlChar* ns = (xmlChar*) p_char(sfp[1]); char* ret = (char*) xmlTextReaderLookupNamespace(reader,ns); KNH_RETURN(ctx,sfp,new_String(ctx,B(ret),NULL)); }
bool AttrList_setItem(AttrList l, String key, int v){ bool retVal = false; int idx = -1; size_t i; String tmpKey; idx = getIndex(l); if(idx >= 0){ if(!keyExists(l, key)){ if(attrListPool[idx].length == (attrListPool[idx].size - 1)){ growList(idx, (attrListPool[idx].size * 2)); } for(i = 0; i < attrListPool[idx].size; ++i){ if(attrListPool[idx].keys[i] == INVALID_STRING){ break; } } tmpKey = new_String(NULL); if(tmpKey != INVALID_STRING && String_copyString(tmpKey, key)){ attrListPool[idx].keys[i] = tmpKey; attrListPool[idx].data[i] = v; attrListPool[idx].length++; retVal = true; } } } return retVal; }
static int validate_header(void) { /* {{{ */ String * found; PDTColumn * expected; String * separator; String * found_fields; String * expected_fields; int valid; /* Initialize */ found = null_String; expected = null_PDTColumn; found_fields = null_String; expected_fields = null_String; separator = new_String("|", 1); valid = 1; void * column_join(Array * array, ArrayElement * element, void * target) { ((String *)target)->m->append((String *)target, ((PDTColumn *)element->data)->column_name); if(array->position < (array->length - 1)) { ((String *)target)->m->append((String *)target, separator); } return target; }
int main(int argc, char *argv[]) { String *str = new_String(5); set_String(str, "ololo"); for (int i = 0; i < 5; i++) printf("%c", str->str[i]); free_String(str); }
/* @method String XmlReader.getAttributeNo(Int numer) */ METHOD XmlReader_getAttributeNo(Ctx *ctx, knh_sfp_t *sfp) { xmlTextReaderPtr reader = (xmlTextReaderPtr) p_cptr(sfp[0]); int num = p_int(sfp[1]); char* ret = (char*) xmlTextReaderGetAttributeNo(reader,num); KNH_RETURN(ctx,sfp,new_String(ctx,B(ret),NULL)); }
int main(int argc, char* argv[]) { fs::path game_dir("C:\\my\\unity\\proj1\\proj1\\build\\1_Data"); fs::path managed_dir = game_dir / "Managed"; fs::path mono_dir = game_dir / "Mono"; fs::path dll_path = mono_dir / "_mono.dll"; HMODULE dll = LoadLibraryA(dll_path.string().c_str()); auto f = make_shared<mono_wrapper::functions_t>(mono_wrapper::load_mono_functions_from_dll(dll)); f->mono_set_dirs(managed_dir.string().c_str(), (game_dir / "Mono/etc").string().c_str()); { std::stringstream ss; ss << managed_dir.string() << ";" << mono_dir.string(); f->mono_set_assemblies_path(ss.str().c_str()); } MonoDomain* domain = f->mono_jit_init("My domain"); auto s = new_String(f, "Hello!"); auto s1 = s->ToUpper(); string s1_content = s1->to_utf8(); int32_t len = s->get_Length(); return 0; }
METHOD Xml_dump(Ctx *ctx, knh_sfp_t *sfp) { xmlDocPtr doc = (xmlDocPtr) p_cptr(sfp[0]); xmlChar* ret; int size; xmlDocDumpMemory(doc,&ret,&size); KNH_RETURN(ctx, sfp, new_String(ctx, B((char*)ret), NULL)); }
/* @method String XmlReader.getAttributeNs(String ns, String name) */ METHOD XmlReader_getAttributeNs(Ctx *ctx, knh_sfp_t *sfp) { xmlTextReaderPtr reader = (xmlTextReaderPtr) p_cptr(sfp[0]); xmlChar* ns = (xmlChar*) p_char(sfp[1]); xmlChar* name = (xmlChar*) p_char(sfp[2]); char* ret = (char*) xmlTextReaderGetAttributeNs(reader,ns,name); KNH_RETURN(ctx,sfp,new_String(ctx,B(ret),NULL)); }
METHOD XmlNode_getName(Ctx *ctx, knh_sfp_t *sfp) { xmlNodePtr node= (xmlNodePtr) p_cptr(sfp[0]); xmlChar* ret = (xmlChar *)""; if(node->name){ ret = (xmlChar *)node->name; } KNH_RETURN(ctx, sfp, new_String(ctx, B((char*)ret), NULL)); }
static METHOD knh__String_trim(Ctx *ctx, knh_sfp_t *sfp) { knh_bytes_t t = knh_String_tobytes(sfp[0].s); knh_bytes_t t2 = knh_bytes_trim(t); String *s = sfp[0].s; if(t.len > t2.len) { s = new_String(ctx, t2, s); } KNH_RETURN(ctx, sfp, s); }
METHOD XmlNode_getContent(Ctx *ctx, knh_sfp_t *sfp) { xmlNodePtr node= (xmlNodePtr) p_cptr(sfp[0]); xmlChar* ret = (xmlChar *)""; if(node->content){ ret = node->content; fprintf(stdout,"[%s]\n",(char*)node->content); } KNH_RETURN(ctx, sfp, new_String(ctx, B((char*)ret), NULL)); }
METHOD XmlNode_getAttr(Ctx *ctx, knh_sfp_t *sfp) { xmlNodePtr node= (xmlNodePtr) p_cptr(sfp[0]); xmlChar *name = (xmlChar *) p_char(sfp[1]); xmlChar *ret = (xmlChar *)""; if(node->properties){ ret = xmlGetProp(node,name); } KNH_RETURN(ctx, sfp, new_String(ctx, B((char*)ret), NULL)); }
/* @method String XmlReader.getAttribute() */ METHOD XmlReader_getAttribute(Ctx *ctx, knh_sfp_t *sfp) { xmlTextReaderPtr reader = (xmlTextReaderPtr) p_cptr(sfp[0]); xmlChar * ns = (xmlChar *) p_char(sfp[1]); char* ret = (char*) xmlTextReaderGetAttribute(reader,ns); if(ret == NULL){ ret = ""; } KNH_RETURN(ctx,sfp,new_String(ctx,B(ret),NULL)); }
/* @method String XmlReader.getQuoteChar() */ METHOD XmlReader_getQuoteChar(Ctx *ctx, knh_sfp_t *sfp) { xmlTextReaderPtr reader = (xmlTextReaderPtr) p_cptr(sfp[0]); int num = xmlTextReaderQuoteChar(reader); char* ret = " "; if(num == 34){ ret[0] = '"'; } else { ret[0] = '\0'; } KNH_RETURN(ctx,sfp,new_String(ctx,B(ret),NULL)); }
String *createGaugeQCDML ( gaugeConfigurationMetadata *gaugeConfiguration ) { char dummy[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><title>Dummy QCDML</title>"; size_t size = 4000; String* st = new_String ( size, "\n####### String ############\n" ); outStream* os = new_outStreamString ( st ); gaugeConfigurationMetadataEncode ( os, "gaugeConfiguration", gaugeConfiguration ); delete_outStream ( os ); }
void testString ( testStruct const* obj ) { size_t size = 2048; String* st = new_String ( size, "" ); outStream* os = new_outStreamString ( st ); printf ( "\noutput to String\n" ); testStructEncode ( os, "myTestStruct", obj ); printf ( "%s\n", st -> chars ); delete_String ( st ); delete_outStream ( os ); }
static METHOD knh__String_new(Ctx *ctx, knh_sfp_t *sfp) { String *s; if(IS_NULL(sfp[2].o)) { s = new_String(ctx, knh_Bytes_tobytes(sfp[1].ba), NULL); } else { BytesConv *bc = new_BytesConv__in(ctx, knh_String_tochar(sfp[2].s)); KNH_SETv(ctx, sfp[3].o, bc); s = new_String__bconv(ctx, knh_Bytes_tobytes(sfp[1].ba), bc); } KNH_RETURN(ctx, sfp, s); }
static METHOD knh__String_split(Ctx *ctx, knh_sfp_t *sfp) { Array *a = NULL; int istrim = IS_NULL(sfp[2].o) ? 0 : (int)sfp[2].ivalue; if(IS_NULL(sfp[1].o)) { a = knh_String_toCharArray(ctx, sfp[0].s, istrim); } else { knh_bytes_t delim = knh_String_tobytes(sfp[1].s); if(delim.len == 0) { a = knh_String_toCharArray(ctx, sfp[0].s, istrim); } else { knh_bytes_t base = knh_String_tobytes(sfp[0].s); a = new_Array(ctx, CLASS_String, 8); while(1) { knh_index_t loc = knh_bytes_indexOf(base, delim); if(loc == -1) { if(istrim) base = knh_bytes_trim(base); knh_Array_add(ctx, a, UP(new_String(ctx, base, sfp[0].s))); break; } else if(loc == 0) { knh_Array_add(ctx, a, UP(TS_EMPTY)); } else { knh_bytes_t t = knh_bytes_first(base, loc); if(istrim) t = knh_bytes_trim(t); knh_Array_add(ctx, a, UP(new_String(ctx, t, sfp[0].s))); } base.buf = base.buf + loc + delim.len; base.len = base.len - loc - delim.len; } } } KNH_ASSERT(a != NULL); KNH_RETURN(ctx, sfp, a); }
static METHOD knh__String_format(Ctx *ctx, knh_sfp_t *sfp) { knh_bytes_t fmt = knh_String_tobytes(sfp[0].s); knh_sfp_t *param = sfp + 1; int ac = knh_stack_argc(ctx, param); knh_bytes_t mt, expr, next; if(!knh_bytes_findMT(ctx, fmt, &mt, &expr, &next)) { KNH_RETURN(ctx, sfp, sfp[0].s); } knh_cwb_t cwbbuf, *cwb = knh_cwb_open(ctx, &cwbbuf); int count; for(count = 0; ; count++) { if(mt.buf > fmt.buf + 1) { fmt.len = (mt.buf - fmt.buf) - 1; knh_Bytes_write(ctx, cwb->ba, fmt); } int index = count; if(expr.len > 0) { knh_int_t num; if(knh_bytes_parseint(expr, &num)) { index = (int)num; } } if(0 <= index && index < ac) { knh_sfp_t *esp = KNH_LOCAL(ctx); KNH_SETv(ctx, esp[1].o, param[index].o); esp[1].data = param[index].data; Object *m = KNH_NULL; if(knh_bytes_isOptionalMT(mt)) m = UP(new_String(ctx, mt, NULL)); mt.buf = mt.buf - 1; mt.len++; /* 's' == > '%s' */ knh_methodn_t mn = knh_getmn(ctx, mt, METHODN__empty); knh_esp1_format(ctx, mn, cwb->w, m); } else { if(knh_Context_isDebug(ctx)) { KNH_THROW_OUTOFINDEX(ctx, index, ac); } } fmt.buf = next.buf; fmt.len = next.len; if(!knh_bytes_findMT(ctx, fmt, &mt, &expr, &next)) { break; } } if(fmt.len > 0) { knh_Bytes_write(ctx, cwb->ba, fmt); } KNH_RETURN(ctx, sfp, new_StringX__cwb(ctx, knh_Object_cid(sfp[0].o), cwb)); }
static Array *knh_String_toCharArray(Ctx *ctx, String *bs, int istrim) { knh_bytes_t base = knh_String_tobytes(bs); if(knh_String_isASCII(bs)) { size_t i, n = base.len; Array *a = new_Array(ctx, CLASS_String, n); for(i = 0; i < n; i++) { if(istrim && isspace(base.buf[i])) continue; knh_bytes_t sub = { base.buf + i, 1}; knh_Array_add(ctx, a, UP(new_String(ctx, sub, bs))); } return a; } else { size_t i, n = knh_bytes_mlen(base); Array *a = new_Array(ctx, CLASS_String, n); for(i = 0; i < n; i++) { if(istrim && isspace(base.buf[i])) continue; knh_bytes_t sub = knh_bytes_mofflen(base, n, 1); knh_Array_add(ctx, a, UP(new_String(ctx, sub, bs))); } return a; } }
static PDTColumn * PDTColumn_init(PDTColumn * self, char * column, size_t column_length) { /* Variables */ if(self != null_PDTColumn) { self->column_name = new_String(column, column_length); } else { self->column_name = null_String; } self->validations = null_Array; return self; }