Example #1
0
//## String DIR.readPath()
static KMETHOD DIR_readPath(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, delim[2] = {'/', 0};
			KGrowingBuffer wb;
			KLIB Kwb_Init(&(kctx->stack->cwb), &wb);
			KLIB Kwb_Write(kctx, &wb, S_text(dir->PathInfoNULL), S_size(dir->PathInfoNULL));
			KLIB Kwb_Write(kctx, &wb, delim, 1);
			if(dir->readerIconv != ICONV_NULL) {
				KLIB Kwb_Write(kctx, &wb, d_name, strlen(d_name));
			}
			else {
				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));
}
Example #2
0
static void dumpMethod(KonohaContext *kctx, KonohaStack *sfp, kMethod *mtd)
{
    KGrowingBuffer wb;
    KLIB Kwb_Init(&(kctx->stack->cwb), &wb);
    kMethod_WriteToBuffer(kctx, mtd, &wb);
    PLATAPI printf_i("%s\n", KLIB Kwb_top(kctx, &wb, 1));
    KLIB Kwb_Free(&wb);
    return;
}
Example #3
0
//## @Const method String String.replace(RegExp searchvalue, String newvalue);
static KMETHOD String_replace(KonohaContext *kctx, KonohaStack *sfp)
{
	kString *s0 = sfp[0].asString;
	kRegExp *re = sfp[1].asRegExp;
	const char* fmttext = S_text(sfp[2].asString);
	size_t fmtlen = S_size(sfp[2].asString);
	kString *s = s0;
	if(IS_NOTNULL(re) && S_size(re->pattern) > 0) {
		KGrowingBuffer wb;
		KLIB Kwb_Init(&(kctx->stack->cwb), &wb);
		const char *str = S_text(s0);  // necessary
		const char *base = str;
		const char *eos = str + S_size(s0); // end of str
		kregmatch_t pmatch[KREGEXP_MATCHSIZE+1];
		int isGlobalOption = RegExp_isGlobal(re);
		do {
			if(str >= eos) break;
			int res = pcre_regexec(kctx, re->reg, str, KREGEXP_MATCHSIZE, pmatch, re->eflags);
			if(res != 0) {
				// TODO
				//LOG_regex(kctx, sfp, res, re, str);
				break;
			}
			size_t len = pmatch[0].rm_eo;
			if(pmatch[0].rm_so > 0) {
				KLIB Kwb_write(kctx, &wb, str, pmatch[0].rm_so);
			}
			size_t matched = knh_regexp_Matched(pmatch, KREGEXP_MATCHSIZE);
			if(len > 0) {
				Kwb_writeRegexFormat(kctx, &wb, fmttext, fmtlen, base, pmatch, matched);
				str += len;
			} else {
				if(str == base) { // 0-length match at head of string
					Kwb_writeRegexFormat(kctx, &wb, fmttext, fmtlen, base, pmatch, matched);
				}
				break;
			}
		} while(isGlobalOption);
		KLIB Kwb_write(kctx, &wb, str, strlen(str)); // write out remaining string
		s = Kwb_newString(kctx, OnStack, &wb); // close cwb
		KLIB Kwb_Free(&wb);
	}
	KReturn(s);
}