Esempio n. 1
0
//## String ResultSet.getString(String n);
static KMETHOD ResultSet_getString(KonohaContext *kctx, KonohaStack *sfp)
{
	kResultSet *rs = (kResultSet *)sfp[0].asObject;
	kString *res = TS_EMPTY;
	int idx = ResultSet_FindColumn(kctx, rs, sfp[1].asString);
	if(idx >= 0) {
		ktypeattr_t type = rs->column[idx].type;
		krbp_t *val = &rs->column[idx].val;
		if(type == KType_String) {
			res = val[0].asString;
		} else if(type == KType_Int) {
			KBuffer wb;
			KLIB KBuffer_Init(&(kctx->stack->cwb), &wb);
			KLIB KBuffer_printf(kctx, &wb, KFLOAT_FMT, val[0].floatValue);
			const char *text = KLIB KBuffer_text(kctx, &wb, 0);
			res = KLIB new_kString(kctx, OnStack, text, KBuffer_bytesize(&wb), 0);
			KLIB KBuffer_Free(&wb);
		} else if(KDefinedKonohaCommonModule() && type == KType_float) {
			KBuffer wb;
			KLIB KBuffer_Init(&(kctx->stack->cwb), &wb);
			KLIB KBuffer_printf(kctx, &wb, KFLOAT_FMT, val[0].floatValue);
			KLIB KBuffer_Free(&wb);
			const char *text = KLIB KBuffer_text(kctx, &wb, 0);
			res = KLIB new_kString(kctx, OnStack, text, KBuffer_bytesize(&wb), 0);
			KLIB KBuffer_Free(&wb);
		}
	}
	KReturn(res);
}
Esempio n. 2
0
//## String DIR.readFileName()
static KMETHOD DIR_readFileName(KonohaContext *kctx, KonohaStack *sfp)
{
	kDir *dir = (kDir *)sfp[0].asObject;
	if(dir->dirp != NULL) {
		KMakeTrace(trace, sfp);
		struct dirent entry, *result;
		int ret = readdir_r(dir->dirp, &entry, &result);
		if(result != NULL) {
			char *d_name = result->d_name;
			if(dir->readerIconv == ICONV_NULL) {
				KReturn(KLIB new_kString(kctx, OnStack, d_name, strlen(d_name), StringPolicy_SystemInfo));
			}
			else {
				KGrowingBuffer wb;
				KLIB Kwb_Init(&(kctx->stack->cwb), &wb);
				KLIB Kwb_iconv(kctx, &wb, dir->readerIconv, d_name, strlen(d_name), trace);
				KReturnWith(
					KLIB new_kString(kctx, OnStack, KLIB Kwb_top(kctx, &wb, 0), Kwb_bytesize(&wb), StringPolicy_SystemInfo),
					KLIB Kwb_Free(&wb)
				);
			}
		}
		if(ret == -1) {
			KTraceErrorPoint(trace, SystemFault, "readdir", LogErrno);
		}
		kDir_close(kctx, dir);
	}
	KReturn(KNULL(String));
}
Esempio n. 3
0
void test_kString(KonohaContext *kctx)
{
    intptr_t i;
    kString *s;
    for (i = 0; i < 100; ++i) {
        s = KLIB new_kString(kctx, GcUnsafe, "abcd", 4, 0);
        assert(strcmp(S_text(s), "abcd") == 0);
        assert(S_size(s) == 4);
        assert(kString_is(ASCII, s) == 1);
    }
    for (i = 0; i < 100; ++i) {
        s = KLIB new_kString(kctx, GcUnsafe, "abcd", 4, 0);
        assert(strcmp(S_text(s), "abcd") == 0);
        assert(S_size(s) == 4);
        assert(S_text(s) == (char*)s->inline_text);
    }
    for (i = 0; i < 100; ++i) {
        static const char *text = "12345678901234567890";
        s = KLIB new_kString(kctx, GcUnsafe, text, 20, StringPolicy_TEXT | StringPolicy_UTF8);
        assert(strcmp(S_text(s), text) == 0);
        assert(S_size(s) == 20);
        assert(S_text(s) == text);
        assert(kString_is(ASCII, s) == 0);
    }
}
Esempio n. 4
0
//## String Curl.receiveString();
static KMETHOD Curl_receiveString(KonohaContext *kctx, KonohaStack *sfp)
{
	kCurl* kcurl = (kCurl *)sfp[0].asObject;

	/* presets */
	struct ReceiveBuffer rbuf = {0};
	rbuf.kctx = kctx;
	KLIB KBuffer_Init(&(kctx->stack->cwb), &rbuf.wb);
	curl_easy_setopt(kcurl->curl, CURLOPT_WRITEFUNCTION, writeToBuffer);
	curl_easy_setopt(kcurl->curl, CURLOPT_WRITEDATA, &rbuf);

	/* perform */
	KMakeTrace(trace, sfp);
	CURLcode res;
	if(kcurl->headers != NULL) {
		curl_easy_setopt(kcurl->curl, CURLOPT_HTTPHEADER, kcurl->headers);
	}
	KTraceResponseCheckPoint(trace, 0, "curl_easy_perform",
		res = curl_easy_perform(kcurl->curl)
	);
	if(res != CURLE_OK) {
		int fault = diagnosisCurlFaultType(kctx, res, (kcurl->URLInfoNULL == NULL) ? 0 : kString_guessUserFault(kcurl->URLInfoNULL));
		KTraceErrorPoint(trace, fault, "curl_easy_perform", LogURL(kcurl), LogCurlStrError(res));
	}

	KReturnWith(
		KLIB new_kString(rbuf.kctx, OnStack, KLIB KBuffer_Stringfy(rbuf.kctx, &rbuf.wb, 0), KBuffer_bytesize(&rbuf.wb), 0),
		KLIB KBuffer_Free(&rbuf.wb)
	);
}
Esempio n. 5
0
//## @Native String XmlReader.lookupNameSpace(String ns);
static KMETHOD XmlReader_lookupNameSpace(KonohaContext *kctx, KonohaStack *sfp)
{
	xmlTextReaderPtr reader = getRawXmlReader(sfp[0]);
	xmlChar* ns = (xmlChar *)kString_text(sfp[1].asString);
	char* ret = (reader != NULL) ? (char *) xmlTextReaderLookupNamespace(reader,ns) : NULL;
	KReturn(KLIB new_kString(kctx, GcUnsafe, ret, strlen(ret), 0));
}
Esempio n. 6
0
// String Tagger.parse(String input)
static KMETHOD Tagger_Parse(KonohaContext *kctx, KonohaStack *sfp)
{
	mecab_t * mecab = ((struct _kTagger *)(sfp[0].asObject))->mecab;
	const char *input = kString_text(sfp[1].asString);
	const char* result = mecab_sparse_tostr(mecab, input);
	KReturn(KLIB new_kString(kctx, GcUnsafe, result, strlen(result), 0));
}
Esempio n. 7
0
static void kArray_executeRegExp(KonohaContext *kctx, kArray *resultArray, kRegExp *regex, kString *s0)
{
	int stringPolicy = kString_is(ASCII, s0) ? StringPolicy_ASCII : 0;
	if(IS_NOTNULL(regex) && S_size(regex->pattern) > 0) {
		const char *s = S_text(s0);  // necessary
		const char *base = s;
		const char *eos = base + S_size(s0);
		size_t i, nmatch = pcre_nmatchsize(kctx, regex->reg);
		kregmatch_t *p, pmatch[nmatch+1];
		int isGlobalOption = RegExp_isGlobal(regex);
		do {
			int res = pcre_regexec(kctx, regex->reg, s, nmatch, pmatch, regex->eflags);
			if(res != 0) {
				// FIXME
				//LOG_regex(kctx, sfp, res, regex, s);
				break;
			}
			for(p = pmatch, i = 0; i < nmatch; p++, i++) {
				if(p->rm_so == -1) break;
				KLIB new_kString(kctx, resultArray, s + (p->rm_so), ((p->rm_eo) - (p->rm_so)), stringPolicy);
			}
			if(isGlobalOption) {
				size_t eo = pmatch[0].rm_eo; // shift matched pattern
				s += (eo > 0) ? eo : 1;
				if(!(s < eos)) isGlobalOption = 0; // stop iteration
			}
		} while(isGlobalOption);
	}
}
Esempio n. 8
0
//## @Native String XmlReader.getAttributeNo(int number);
static KMETHOD XmlReader_getAttributeNo(KonohaContext *kctx, KonohaStack *sfp)
{
	xmlTextReaderPtr reader = getRawXmlReader(sfp[0]);
	int num = (int)(sfp[1].intValue);
	char* ret = (reader != NULL) ? (char *) xmlTextReaderGetAttributeNo(reader, num) : NULL;
	KReturn(KLIB new_kString(kctx, GcUnsafe, ret, strlen(ret), 0));
}
Esempio n. 9
0
// String Tagger.NBestParse(int n, String input)
static KMETHOD Tagger_NBestParse(KonohaContext *kctx, KonohaStack *sfp)
{
	struct _kTagger *mecab = (struct _kTagger *)sfp[0].asObject;
	kint_t ival = sfp[1].intValue;
	const char *input = kString_text(sfp[2].asString);
	const char* result = mecab_nbest_sparse_tostr(mecab->mecab, ival, input);
	KReturn(KLIB new_kString(kctx, GcUnsafe, result, strlen(result), 0));
}
Esempio n. 10
0
//## @Native String XmlReader.getAttributeNs(String ns, String name);
static KMETHOD XmlReader_getAttributeNs(KonohaContext *kctx, KonohaStack *sfp)
{
	xmlTextReaderPtr reader = getRawXmlReader(sfp[0]);
	xmlChar* ns = (xmlChar *)kString_text(sfp[1].asString);
	xmlChar* name = (xmlChar *)kString_text(sfp[2].asString);
	char* ret = (reader != NULL) ? (char *) xmlTextReaderGetAttributeNs(reader,ns,name) : NULL;
	KReturn(KLIB new_kString(kctx, GcUnsafe, ret, strlen(ret), 0));
}
Esempio n. 11
0
// ## String Request.getArgs();
static KMETHOD Request_getArgs(KonohaContext *kctx, KonohaStack *sfp)
{
    kRequest *self = (kRequest *) sfp[0].asObject;
    if(self->r->args == NULL) {
        KReturn(KNULL(String));
    }
    KReturn(KLIB new_kString(kctx, OnStack, self->r->args, strlen(self->r->args), 0));
}
Esempio n. 12
0
//## String JSON.getString(String key);
static KMETHOD kJSON_getString(KonohaContext *kctx, KonohaStack *sfp)
{
	JSON obj = ((kJSON *)sfp[0].asObject)->json;
	const char *key = S_text(sfp[1].asString);
	size_t len;
	const char *text = JSON_getString(obj, key, &len);
	KReturn(KLIB new_kString(kctx, text, len, 0));
}
Esempio n. 13
0
//## String Date.toLocaleString();
static KMETHOD Date_toLocaleString(KonohaContext *kctx, KonohaStack *sfp)
{
	time_t tv_sec = (sfp[0].asDate)->tv.tv_sec;
	struct tm lt;
	localtime_r(&tv_sec, &lt);
	char str[MAX_STR_SIZE];
	size_t len = strftime(str, MAX_STR_SIZE, "%a %b %d %Y %H:%M:%S (%Z)", &lt);
	KReturn(KLIB new_kString(kctx, OnStack, str, len, 0));
}
Esempio n. 14
0
//## String Date.toGMTString();
static KMETHOD Date_toGMTString(KonohaContext *kctx, KonohaStack *sfp)
{
	time_t tv_sec = (sfp[0].asDate)->tv.tv_sec;
	struct tm utc;
	gmtime_r(&tv_sec, &utc);
	char str[MAX_STR_SIZE];
	size_t len = strftime(str, MAX_STR_SIZE, "%a, %d %b %Y %H:%M:%S %Z", &utc);
	KReturn(KLIB new_kString(kctx, OnStack, str, len, 0));
}
Esempio n. 15
0
static void String_setNextResult(KonohaContext *kctx, KonohaStack* sfp)
{
	kIterator *itr = sfp[0].asIterator;
	kString *s = (kString *)itr->source;
	const char *t = S_text(s) + itr->current_pos;
	size_t charsize = utf8len(t[0]);
	itr->current_pos += charsize;
	KReturn(KLIB new_kString(kctx, OnStack, t, charsize, (charsize == 1) ? StringPolicy_ASCII : StringPolicy_UTF8));
}
Esempio n. 16
0
//## String System.getenv(String name)
static KMETHOD System_getenv(KonohaContext *kctx, KonohaStack *sfp)
{
	const char *name = kString_text(sfp[1].asString);
	char *ret = getenv(name);
	if(ret == NULL) {
		KReturn(KNULL(String));
	}
	KReturn(KLIB new_kString(kctx, OnStack, ret, strlen(ret), 0));
}
Esempio n. 17
0
//## String Log.get(String key)
static KMETHOD Log_get_(KonohaContext *kctx, KonohaStack *sfp)
{
	kRawPtr *self = (kRawPtr *) sfp[0].asObject;
	struct Log *log = (struct Log *) self->rawptr;
	char *key  = (char *) S_text(sfp[1].asString);
	int   klen = S_size(sfp[1].asString);
	int   vlen;
	char *data = Log_get(log, key, klen, &vlen);
	RETURN_(KLIB new_kString(kctx, data, vlen, SPOL_ASCII|SPOL_POOL));
}
Esempio n. 18
0
//## @Const String File.scriptPath(String path);
static KMETHOD File_scriptPath(KonohaContext *kctx, KonohaStack *sfp)
{
	char scriptPathBuf[K_PATHMAX];
	const char *scriptPath = PLATAPI formatTransparentPath(scriptPathBuf, sizeof(scriptPathBuf), KFileLine_textFileName(sfp[K_RTNIDX].calledFileLine), kString_text(sfp[1].asString));
	kStringVar *resultValue = (kStringVar *)KLIB new_kString(kctx, OnStack, scriptPath, strlen(scriptPath), 0);
	if(kString_Is(Literal, sfp[1].asString)) {
		kString_Set(Literal, resultValue, true);
	}
	KReturn(resultValue);
}
Esempio n. 19
0
//## String Json.dump();
static KMETHOD Json_dump(KonohaContext *kctx, KonohaStack *sfp)
{
	json_t* obj = ((struct _kJson *)sfp[0].asObject)->obj;
	CHECK_JSON(obj, KReturnDefaultObjectValue());
	char* data = json_dumps(obj, JSON_ENSURE_ASCII);
	if(data == NULL) {
		KReturn(KNULL(String));
	}
	KReturn(KLIB new_kString(kctx, data, strlen(data), 0));
}
Esempio n. 20
0
//## String Person.say();
static KMETHOD Person_say(KonohaContext *kctx, KonohaStack *sfp)
{
	struct Person *p = (struct Person *) sfp[0].asObject;
	kString *name = p->name;
	/* When you want to operate with a raw string, please use S_text() macro
	 * to acquire the pointer of a raw string. */
	const char *text = S_text(name);
	char *buf = (char *)alloca(16 + S_size(name));
	sprintf(buf, "hello , %s!", text);
	KReturn(KLIB new_kString(kctx, OnStack, buf, strlen(buf), StringPolicy_TEXT));
}
Esempio n. 21
0
//## String Date.toISOString();
static KMETHOD Date_toISOString(KonohaContext *kctx, KonohaStack *sfp)
{
	struct kDateVar *d = sfp[0].asDate;
	time_t tv_sec = d->tv.tv_sec;
	struct tm utc;
	gmtime_r(&tv_sec, &utc);
	char str[MAX_STR_SIZE];
	size_t len = strftime(str, MAX_STR_SIZE, "%Y-%m-%dT%H:%M:%S", &utc);
	len += snprintf(str+len, MAX_STR_SIZE, ".%03dZ", (int)(d->tv.tv_usec / 1000));
	KReturn(KLIB new_kString(kctx, OnStack, str, len, 0));
}
Esempio n. 22
0
static void kArray_split(KonohaContext *kctx, kArray *resultArray, kString *str, kRegExp *regex, size_t limit)
{
	int stringPolicy = kString_is(ASCII, str) ? StringPolicy_ASCII : 0;
	if(IS_NOTNULL(regex) && S_size(regex->pattern) > 0) {
		const char *s = S_text(str);  // necessary
		const char *eos = s + S_size(str);
		kregmatch_t pmatch[2];
		int res = 0;
		while(s < eos && res == 0) {
			res = pcre_regexec(kctx, regex->reg, s, 1, pmatch, regex->eflags);
			if(res != 0) break;
			size_t len = pmatch[0].rm_eo;
			if(len > 0) {
				KLIB new_kString(kctx, resultArray, s, pmatch[0].rm_so, stringPolicy);
				s += len;
			}
			if(!(kArray_size(resultArray) + 1 < limit)) {
				return;
			}
		}
		if(s < eos) {
			KLIB new_kString(kctx, resultArray, s, eos - s, stringPolicy); // append remaining string to array
		}
	}
	else {
		const unsigned char *s = (const unsigned char *)S_text(str);
		size_t i, n = S_size(str);
		if(kString_is(ASCII, str)) {
			for(i = 0; i < n; i++) {
				KLIB new_kString(kctx, resultArray, (const char *)s + i, 1, StringPolicy_ASCII);
			}
		}
		else {
			for(i = 0; i < n; i++) {
				int len = utf8len(s[i]);
				KLIB new_kString(kctx, resultArray, (const char *)s + i, len, len == 1 ? StringPolicy_ASCII: StringPolicy_UTF8);
				i += len;
			}
		}
	}
}
Esempio n. 23
0
//## String System.ttyname(int fd);
static KMETHOD System_ttyname(KonohaContext *kctx, KonohaStack *sfp)
{
	int fd = sfp[1].intValue;
	char buf[K_PAGESIZE];
	int ret = ttyname_r(fd, buf, sizeof(buf));
	if(ret != 0) {
		KMakeTrace(trace, sfp);
		int fault = KLIB DiagnosisFaultType(kctx, SystemError, trace);
		KTraceErrorPoint(trace, fault, "ttyname", LogUint("fd", fd), LogErrno);
	}
	KReturn(KLIB new_kString(kctx, OnStack, buf, strlen(buf), 0));
}
Esempio n. 24
0
//## @Native String XmlReader.getQuoteChar();
static KMETHOD XmlReader_getQuoteChar(KonohaContext *kctx, KonohaStack *sfp)
{
	xmlTextReaderPtr reader = getRawXmlReader(sfp[0]);
	char buf[4] = {0};
	const char* ret = NULL;
	if(reader != NULL) {
		int ch = xmlTextReaderQuoteChar(reader);
		buf[0] = ch;
		ret = (const char *)buf;
	}
	KReturn(KLIB new_kString(kctx, GcUnsafe, ret, strlen(ret), 0));
}
Esempio n. 25
0
static KMETHOD kMD5_Final(KonohaContext *kctx, KonohaStack *sfp)
{
	unsigned char MD[MD5_DIGEST_LENGTH];
	MD5state_st *c = RawPtr(sfp[0].asObject);
	int ret_ = MD5_Final(MD, c);
	int i;
	char MD_S[MD5_DIGEST_LENGTH*2+1];
	for (i = 0; i < MD5_DIGEST_LENGTH; i++) {
		snprintf(MD_S+i*2, MD5_DIGEST_LENGTH*2+1, "%02x", MD[i]);
	}
	KReturn(KLIB new_kString(kctx, MD_S, MD5_DIGEST_LENGTH*2, StringPolicy_ASCII));
}
Esempio n. 26
0
static KMETHOD kSHA1_Final(KonohaContext *kctx, KonohaStack *sfp)
{
	unsigned char SHA[SHA_DIGEST_LENGTH];
	SHAstate_st *c = RawPtr(sfp[0].asObject);
	int ret_ = SHA1_Final(SHA, c);
	int i;
	char SHA_S[SHA_DIGEST_LENGTH*2+1];
	for (i = 0; i < SHA_DIGEST_LENGTH; i++) {
		snprintf(SHA_S+i*2, SHA_DIGEST_LENGTH*2+1, "%02x", SHA[i]);
	}
	KReturn(KLIB new_kString(kctx, SHA_S, SHA_DIGEST_LENGTH*2, StringPolicy_ASCII));
}
Esempio n. 27
0
kString* knh_getPropertyNULL(CTX, kbytes_t key)
{
	if(knh_bytes_startsWith_(key, STEXT("env."))) {
		CWB_t cwbbuf, *cwb = CWB_open(_ctx, &cwbbuf);
		CWB_nzenvkey(_ctx, cwb, knh_bytes_last(key, 4));
		char *v = knh_getenv(CWB_totext(_ctx, cwb));
		CWB_close(_ctx, cwb);
		if(v == NULL) return NULL;
		return new_kString(v, knh_strlen(v), SPOL_ASCII|SPOL_POOL);
	}
	return (kString*)knh_DictMap_getNULL(_ctx,  ctx->share->props, key);
}
Esempio n. 28
0
//## void Console.readLine(String prompt)
static KMETHOD Console_readLine(KonohaContext *kctx, KonohaStack *sfp)
{
	kString *s;
	char *p = CONSOLEAPI InputUserText(kctx, kString_text(sfp[1].asString), 0);
	if(p != NULL) {
		s = KLIB new_kString(kctx, OnStack, p, strlen(p), 0);
		free(p);
	}
	else {
		s = KNULL(String);
	}
	KReturn(s);
}
Esempio n. 29
0
static KClassVar* kNameSpace_DefineClassName(KonohaContext *kctx, kNameSpace *ns, const char *text, size_t len)
{
	KDEFINE_CLASS defNewClass = {0};
	defNewClass.cflag       = 0;
	defNewClass.typeId      = KTypeAttr_NewId;
	defNewClass.baseTypeId  = KType_Object;
	defNewClass.superTypeId = KType_Object;
	defNewClass.init = Object_InitToMakeDefaultValueAsNull;

	kString *name = KLIB new_kString(kctx, GcUnsafe, text, len, StringPolicy_TEXT);
	KBaseTrace(trace);
	return (KClassVar *) KLIB kNameSpace_DefineClass(kctx, ns, name, &defNewClass, trace);
}
Esempio n. 30
0
static kString* kConsole_inputUserPassword(KonohaContext *kctx, const char *message)
{
	kString *s;
	char *p = CONSOLEAPI InputUserPassword(kctx, message);
	if(p != NULL) {
		s = KLIB new_kString(kctx, OnStack, p, strlen(p), 0);
		free(p);
	}
	else {
		s = KNULL(String);
	}
	return s;
}