KNHAPI(void) knh_OutputStream_writeLine(Ctx *ctx, knh_OutputStream_t *w, knh_bytes_t t, knh_bool_t isNEWLINE) { if(knh_OutputStream_isBOL(w)) { knh_write_BOL(ctx, w); } if(t.len > 0) { if(DP(w)->encNULL != NULL && !knh_bytes_isASCII(t)) { if(knh_bytes_in(ctx->bufa->bu, t.ubuf)) { KNH_TODO("write cwb->buf with encoding"); } else { knh_cwb_t cwbbuf, *cwb = knh_cwb_open(ctx, &cwbbuf); knh_StringEncoder_t *c = DP(w)->encNULL; c->dspi->enc(ctx, c->conv, t, cwb->ba); knh_write(ctx, w, knh_cwb_tobytes(cwb)); knh_cwb_close(cwb); } } else { knh_OutputStream_write(ctx, w, t); } } if(isNEWLINE) { knh_write_EOL(ctx, w); } }
knh_String_t* knh_path_newString(CTX ctx, knh_path_t *ph, int hasScheme) { knh_uchar_t *ubuf = P_ubuf(ph); if(ph->isRealPath) { int hasUTF8 = 0; size_t i; for(i = ph->pbody; i < ph->plen; i++) { int ch = ubuf[i]; if(ch == '\\') { ubuf[i] = '/'; } if(ch > 127) hasUTF8 = 1; } if(hasUTF8) { KNH_TODO("multibytes file path"); } } knh_bytes_t t = {{P_text(ph)}, ph->plen}; if(!hasScheme) { t.text = t.text + ph->pbody; t.len = t.len - ph->pbody; } DBG_P("ph='%s', size=%d", P_text(ph), ph->plen); return new_String_(ctx, CLASS_String, t, NULL); }
const char* knh_ospath(CTX ctx, knh_path_t *ph) { knh_uchar_t *ubuf = P_ubuf(ph); DBG_ASSERT(ubuf[ph->plen] == 0); if(!ph->isRealPath) { int hasUTF8 = 0; size_t i; for(i = ph->pbody; i < ph->plen; i++) { int ch = ubuf[i]; if(ch == '/' || ch == '\\') { ubuf[i] = K_FILESEPARATOR; } if(ch > 127) hasUTF8 = 1; } if(hasUTF8) { KNH_TODO("multibytes file path"); } { knh_cwb_t cwbbuf, *cwb = knh_cwb_copy(ctx, &cwbbuf, ph, 0); const char *p = knh_realpath(ctx, knh_cwb_tochar(ctx, cwb), ph); knh_cwb_close(cwb); return p; // p is NULL if not found; } } return P_text(ph) + ph->pbody; }
kString *knh_buff_newRealPathString(CTX, kBytes *ba, size_t pos) { char buf[K_PATHMAX], *path = (char*)knh_Bytes_ensureZero(_ctx, ba) + pos; #if defined(K_USING_WINDOWS_) char *ptr = _fullpath(buf, path, K_PATHMAX); #elif defined(K_USING_POSIX_) char *ptr = realpath(path, buf); #else char *ptr = NULL; KNH_TODO("realpath in your new environment"); #endif kString *s = new_String(_ctx, (const char*)ptr); if(ptr != buf && ptr != NULL) { free(ptr); } return s; }
static void shell_restart(CTX ctx) { // kNameSpace *ns = new_NameSpace(ctx, NULL); // DBG_ASSERT(ns->b->aliasDictMapNULL == NULL); // ctx->wshare->sysAliasDictMapNULL = DP(ctx->share->rootns)->aliasDictMapNULL; // DP(ctx->share->rootns)->aliasDictMapNULL = NULL; // KNH_SETv(ctx, ((kshare_t*)ctx->share)->rootns, ns); // KNH_SETv(ctx, ((kcontext_t*)ctx)->script, new_(Script)); // { // kGammaBuilder *newgma = new_(GammaBuilder); // KNH_SETv(ctx, ((kcontext_t*)ctx)->gma, newgma); // KNH_INITv(DP(newgma)->symbolDictMap, new_DictMap0(ctx, 256, 0/*isCaseMap*/, "GammaBuilder.symbolDictMap")); // KNH_INITv(DP(newgma)->constPools, new_Array0(ctx, 0)); // KNH_INITv(DP(newgma)->script, ctx->script); // } KNH_TODO("restart"); }
const char* knh_realpath(CTX ctx, const char *path, knh_path_t *ph) { char *buf = P_buf(ph); #if defined(K_USING_WINDOWS) char *ptr = _fullpath(buf, path, K_PATHMAX); #elif defined(K_USING_POSIX_) char *ptr = realpath(path, buf); #else char *ptr = NULL; KNH_TODO("realpath in your new environment"); #endif if(ptr != buf) { if(ptr != NULL) free(ptr); buf[0] = 0; return NULL; } ph->pbody = 0; ph->plen = knh_strlen(buf); ph->isRealPath = 1; return ptr; }