//## String File.readLine(); static KMETHOD File_readLine(KonohaContext *kctx, KonohaStack *sfp) { kFile *file = (kFile *)sfp[0].asObject; if(file->fp != NULL) { KBuffer wb; KLIB KBuffer_Init(&(kctx->stack->cwb), &wb); int ch, pos = 0, hasUTF8 = false, bufferCount = 0, policy = StringPolicy_ASCII; char buffer[K_PAGESIZE]; KMakeTrace(trace, sfp); while((ch = TRACE_fgetc(kctx, file, trace)) != EOF) { //DBG_P("ch='%c', pos=%d", ch, pos); if(ch == '\r') continue; if(ch == '\n') { if(bufferCount == 0 && (!hasUTF8 || file->readerIconv == ICONV_NULL)) { KReturn(KLIB new_kString(kctx, OnStack, buffer, pos, policy)); } break; } if(ch > 127) { hasUTF8 = true; policy = StringPolicy_UTF8; } buffer[pos] = ch; pos++; if(!(pos < K_PAGESIZE)) { if(hasUTF8 && file->readerIconv != ICONV_NULL) { KLIB KBuffer_iconv(kctx, &wb, file->readerIconv, buffer, pos, trace); } else { KLIB KBuffer_Write(kctx, &wb, buffer, pos); } bufferCount++; hasUTF8 = false; pos = 0; } } if(pos > 0) { if(hasUTF8 && file->readerIconv != ICONV_NULL) { KLIB KBuffer_iconv(kctx, &wb, file->readerIconv, buffer, pos, trace); } else { KLIB KBuffer_Write(kctx, &wb, buffer, pos); } } kFile_CheckEOF(kctx, file, trace); KReturn(KLIB KBuffer_Stringfy(kctx, &wb, OnStack, policy | StringPolicy_FreeKBuffer)); } else { KReturn(KNULL(String)); } }
//## 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}; KBuffer wb; KLIB KBuffer_Init(&(kctx->stack->cwb), &wb); KLIB KBuffer_Write(kctx, &wb, kString_text(dir->PathInfoNULL), kString_size(dir->PathInfoNULL)); KLIB KBuffer_Write(kctx, &wb, delim, 1); if(dir->readerIconv != ICONV_NULL) { KLIB KBuffer_Write(kctx, &wb, d_name, strlen(d_name)); } else { KLIB KBuffer_iconv(kctx, &wb, dir->readerIconv, d_name, strlen(d_name), trace); } KReturn(KLIB KBuffer_Stringfy(kctx, &wb, OnStack, StringPolicy_FreeKBuffer)); } if(ret == -1) { KTraceErrorPoint(trace, SystemFault, "readdir", LogErrno); } kDir_close(kctx, dir); } KReturn(KNULL(String)); }
//## 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 { KBuffer wb; KLIB KBuffer_Init(&(kctx->stack->cwb), &wb); KLIB KBuffer_iconv(kctx, &wb, dir->readerIconv, d_name, strlen(d_name), trace); KReturn(KLIB KBuffer_Stringfy(kctx, &wb, OnStack, StringPolicy_FreeKBuffer)); } } if(ret == -1) { KTraceErrorPoint(trace, SystemFault, "readdir", LogErrno); } kDir_close(kctx, dir); } KReturn(KNULL(String)); }
static void KBuffer_convertCharset(KonohaContext *kctx, KBuffer* wb, const char *targetCharset, const char *sourceCharset, const char *sourceBuf, size_t sourceSize, KTraceInfo *trace) { uintptr_t conv = I18NAPI iconv_open_i(kctx, targetCharset, sourceCharset, trace); if(conv != ICONV_NULL) { KLIB KBuffer_iconv(kctx, wb, conv, sourceBuf, sourceSize, trace); I18NAPI iconv_close_i(kctx, conv); } }
//## void File.print(String line); static KMETHOD File_print(KonohaContext *kctx, KonohaStack *sfp) { kFile *file = sfp[0].asFile; kString *line = sfp[1].asString; KMakeTrace(trace, sfp); if(file->writerIconv == ICONV_NULL || kString_Is(ASCII, line)) { TRACE_fwrite(kctx, file, kString_text(line), kString_size(line), trace); } else { KBuffer wb; KLIB KBuffer_Init(&(kctx->stack->cwb), &wb); KLIB KBuffer_iconv(kctx, &wb, file->writerIconv, kString_text(line), kString_size(line), trace); TRACE_fwrite(kctx, file, KLIB KBuffer_text(kctx, &wb, NonZero), KBuffer_bytesize(&wb), trace); KLIB KBuffer_Free(&wb); } }