PARROT_CANNOT_RETURN_NULL STRING * Parrot_file_getcwd(PARROT_INTERP) { DWORD len = GetCurrentDirectoryW(0, NULL); STRING *result; char *c_str; if (!len) THROW("getcwd"); c_str = mem_gc_allocate_n_typed(interp, (len + 1) * 2, char); len = GetCurrentDirectoryW(len, (LPWSTR)c_str); if (!len) { mem_gc_free(interp, c_str); THROW("getcwd"); } result = Parrot_str_new_init(interp, c_str, len * 2, Parrot_utf16_encoding_ptr, 0); mem_gc_free(interp, c_str); return result; }
PARROT_CANNOT_RETURN_NULL PARROT_WARN_UNUSED_RESULT static STRING * getchr_va(PARROT_INTERP, SHIM(INTVAL size), ARGIN(SPRINTF_OBJ *obj)) { ASSERT_ARGS(getchr_va) va_list *arg = (va_list *)(obj->data); /* char promoted to int */ char ch = (char)va_arg(*arg, int); return Parrot_str_new_init(interp, &ch, 1, Parrot_latin1_encoding_ptr, 0); }
/* Gets the current value for an attribute. */ static PMC * get_attribute_boxed(PARROT_INTERP, STable *st, void *data, PMC *class_handle, STRING *name, INTVAL hint) { CStructREPRData *repr_data = (CStructREPRData *)st->REPR_data; CStructBody *body = (CStructBody *)data; INTVAL slot; /* Look up slot, then offset and compute address. */ slot = hint >= 0 ? hint : try_get_slot(interp, repr_data, class_handle, name); if (slot >= 0) { INTVAL type = repr_data->attribute_locations[slot] & CSTRUCT_ATTR_MASK; INTVAL real_slot = repr_data->attribute_locations[slot] >> CSTRUCT_ATTR_SHIFT; if(type == CSTRUCT_ATTR_IN_STRUCT) Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION, "CStruct Can't perform boxed get on flattened attributes yet"); else { PMC *obj = body->child_objs[real_slot]; PMC *typeobj = repr_data->member_types[slot]; if(!obj) { void *cobj = get_ptr_at_offset(body->cstruct, repr_data->struct_offsets[slot]); if(cobj) { if(type == CSTRUCT_ATTR_CARRAY) { obj = make_carray_result(interp, typeobj, cobj); } else if(type == CSTRUCT_ATTR_CSTRUCT) { obj = make_cstruct_result(interp, typeobj, cobj); } else if(type == CSTRUCT_ATTR_CPTR) { obj = make_cpointer_result(interp, typeobj, cobj); } else if(type == CSTRUCT_ATTR_STRING) { char *cstr = (char *) cobj; STRING *str = Parrot_str_new_init(interp, cstr, strlen(cstr), Parrot_utf8_encoding_ptr, 0); obj = REPR(typeobj)->allocate(interp, STABLE(typeobj)); REPR(obj)->initialize(interp, STABLE(obj), OBJECT_BODY(obj)); REPR(obj)->box_funcs->set_str(interp, STABLE(obj), OBJECT_BODY(obj), str); PARROT_GC_WRITE_BARRIER(interp, obj); } body->child_objs[real_slot] = obj; } else { obj = typeobj; } } return obj; } }
PARROT_CANNOT_RETURN_NULL static STRING * ascii_to_encoding(PARROT_INTERP, ARGIN(const STRING *src)) { ASSERT_ARGS(ascii_to_encoding) STRING *dest; if (STRING_max_bytes_per_codepoint(src) == 1) { unsigned char * const src_buf = (unsigned char *)src->strstart; UINTVAL offs; for (offs = 0; offs < src->strlen; ++offs) { UINTVAL c = src_buf[offs]; if (c >= 0x80) Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_LOSSY_CONVERSION, "lossy conversion to ascii"); } dest = Parrot_str_clone(interp, src); dest->encoding = Parrot_ascii_encoding_ptr; } else { String_iter iter; unsigned char *p; const UINTVAL len = src->strlen; dest = Parrot_str_new_init(interp, NULL, len, Parrot_ascii_encoding_ptr, 0); p = (unsigned char *)dest->strstart; STRING_ITER_INIT(interp, &iter); while (iter.charpos < len) { const UINTVAL c = STRING_iter_get_and_advance(interp, src, &iter); if (c >= 0x80) Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_LOSSY_CONVERSION, "can't convert unicode string to ascii"); *p++ = c; } dest->bufused = len; dest->strlen = len; } return dest; }
/* This is what Parrot_str_new_from_cstring does but that isn't exported. */ static STRING * new_from_cstring(PARROT_INTERP, const char *buffer, STRING *encodingname) { STRING *result = STRINGNULL; if (buffer) { const STR_VTABLE *encoding = STRING_IS_NULL(encodingname) ? Parrot_platform_encoding_ptr : Parrot_find_encoding_by_string(interp, encodingname); if (encoding == NULL) Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_ENCODING, "Invalid encoding"); else { int size = strlen(buffer); result = Parrot_str_new_init(interp, buffer, size, encoding, 0); } } return result; }
PARROT_WARN_UNUSED_RESULT PARROT_CANNOT_RETURN_NULL static STRING * ucs4_to_encoding(PARROT_INTERP, ARGIN(const STRING *src)) { ASSERT_ARGS(ucs4_to_encoding) const UINTVAL len = src->strlen; UINTVAL i; STRING *res; utf32_t *ptr; if (src->encoding == Parrot_ucs4_encoding_ptr) return Parrot_str_copy(interp, src); res = Parrot_str_new_init(interp, NULL, len * 4, Parrot_ucs4_encoding_ptr, 0); ptr = (utf32_t *)res->strstart; if (STRING_max_bytes_per_codepoint(src) == 1) { const unsigned char *s = (unsigned char *)src->strstart; for (i = 0; i < len; i++) { ptr[i] = s[i]; } } else { String_iter iter; STRING_ITER_INIT(interp, &iter); while (iter.charpos < len) { i = iter.charpos; ptr[i] = STRING_iter_get_and_advance(interp, src, &iter); } } res->strlen = len; res->bufused = len * 4; return res; }
static void parrot_set_config_hash_interpreter(PARROT_INTERP) { ASSERT_ARGS(parrot_set_config_hash_interpreter) PMC *iglobals = interp->iglobals; PMC *config_hash = NULL; if (parrot_config_size_stored > 1) { STRING * const config_string = Parrot_str_new_init(interp, (const char *)parrot_config_stored, parrot_config_size_stored, Parrot_default_encoding_ptr, PObj_external_FLAG|PObj_constant_FLAG); config_hash = Parrot_thaw(interp, config_string); } else { config_hash = Parrot_pmc_new(interp, enum_class_Hash); } VTABLE_set_pmc_keyed_int(interp, iglobals, (INTVAL) IGLOBALS_CONFIG_HASH, config_hash); }
Create an entry in the C<nci_method_table> for the given NCI method of PMC class C<type>. =cut */ PARROT_EXPORT void register_nci_method(PARROT_INTERP, const int type, ARGIN(void *func), ARGIN(const char *name), ARGIN(const char *proto)) { ASSERT_ARGS(register_nci_method) PMC * const method = Parrot_pmc_new(interp, enum_class_NCI); STRING * const method_name = Parrot_str_new_init(interp, name, strlen(name), Parrot_default_encoding_ptr, PObj_constant_FLAG|PObj_external_FLAG); /* create call func */ VTABLE_set_pointer_keyed_str(interp, method, Parrot_str_new_init(interp, proto, strlen(proto), Parrot_default_encoding_ptr, PObj_constant_FLAG|PObj_external_FLAG), func); /* insert it into namespace */ VTABLE_set_pmc_keyed_str(interp, interp->vtables[type]->_namespace, method_name, method); } /* =item C<void register_native_pcc_method_in_ns(PARROT_INTERP, const int type,
Deserializes the PMC contained in C<fpmc> buffer of C<length> and stores it in C<pmc>. This function returns a true value if this call is successful and false value otherwise. =cut */ PARROT_API Parrot_Int Parrot_api_pmc_deserialize_bytes(ARGIN(Parrot_PMC interp_pmc), ARGIN(const unsigned char *fpmc), Parrot_Int length, ARGOUT(Parrot_PMC *pmc)) { ASSERT_ARGS(Parrot_api_pmc_deserialize_bytes) EMBED_API_CALLIN(interp_pmc, interp) STRING * const fpmc_str = Parrot_str_new_init(interp, (const char *)fpmc, length, Parrot_binary_encoding_ptr, PObj_external_FLAG); *pmc = Parrot_thaw(interp, fpmc_str); EMBED_API_CALLOUT(interp_pmc, interp); } /* =item C<Parrot_Int Parrot_api_pmc_null(Parrot_PMC interp_pmc, Parrot_PMC *pmctonull)> Nullify C<pmctonull> PMC. This function returns a true value if this call is successful and false value otherwise. =cut */