void SimpleStream::Dump(Signal &sig, const void *buff, size_t bytes, bool upperFlag) { int iCol = 0; String strHex, strASCII; const UChar *p = reinterpret_cast<const UChar *>(buff); for (size_t i = 0; i < bytes; i++, p++) { UChar ch = *p; char buff[8]; if (upperFlag) { ::sprintf(buff, (iCol > 0)? " %02X" : "%02X", ch); } else { ::sprintf(buff, (iCol > 0)? " %02x" : "%02x", ch); } strHex += buff; strASCII += (0x20 <= ch && ch < 0x7f)? ch : '.'; iCol++; if (iCol == 16) { String strLine = strHex; strLine += " "; strLine += strASCII; Println(sig, strLine.c_str()); if (sig.IsSignalled()) return; strHex.clear(); strASCII.clear(); iCol = 0; } } if (iCol > 0) { String strLine = strHex; for ( ; iCol < 16; iCol++) strLine += " "; strLine += " "; strLine += strASCII; Println(sig, strLine.c_str()); } }
bool DiffLine::FeedIterator(Environment &env, Signal &sig, size_t iSeq, Iterator *pIterator) { Sequence &seq = GetSequence(iSeq); Value value; while (pIterator->Next(env, value)) { seq.push_back(value.ToString()); } return !sig.IsSignalled(); }
bool SimpleStream::ReadLines(Signal &sig, StringList &strList, bool includeEOLFlag) { for (;;) { String str; if (!ReadLine(sig, str, includeEOLFlag)) break; strList.push_back(str); } return !sig.IsSignalled(); }
void SimpleStream::Printf(Signal &sig, const char *format, ...) { va_list ap; va_start(ap, format); String str = Formatter::FormatV(sig, format, ap); va_end(ap); if (sig.IsSignalled()) return; Print(sig, str.c_str()); }
bool DiffLine::FeedStream(Signal &sig, size_t iSeq, Stream &src) { Sequence &seq = GetSequence(iSeq); bool includeEOLFlag = false; for (;;) { String str; if (!src.ReadLine(sig, str, includeEOLFlag)) break; seq.push_back(str); } return !sig.IsSignalled(); }
bool SimpleStream::ReadLine(Signal &sig, String &str, bool includeEOLFlag) { int ch = GetChar(sig); if (ch < 0) return false; for ( ; ch >= 0; ch = GetChar(sig)) { if (ch == '\n') { if (includeEOLFlag) str += ch; break; } str += ch; } return !sig.IsSignalled(); }
size_t Stream::Peek(Signal &sig, void *buff, size_t len) { if (len == 0) return 0; if (_peek.buff == nullptr) { _peek.buff = new char [len]; _peek.bytes = DoRead(sig, _peek.buff, len); _peek.offsetRead = 0; if (sig.IsSignalled()) return 0; } else if (_peek.bytes < len) { char *buffNew = new char [len]; ::memcpy(buffNew, _peek.buff, _peek.bytes); size_t bytesAdd = DoRead(sig, buffNew + _peek.bytes, len - _peek.bytes); if (sig.IsSignalled()) return 0; delete[] _peek.buff; _peek.buff = buffNew; _peek.bytes += bytesAdd; _peek.offsetRead = 0; } size_t bytesToPeek = ChooseMin(_peek.bytes, len); ::memcpy(buff, _peek.buff, bytesToPeek); return bytesToPeek; }
bool DiffLine::PrintHunk(Signal &sig, SimpleStream &stream, const Hunk &hunk) const { const EditList &edits = GetEditList(); EditList::const_iterator pEdit = edits.begin() + hunk.idxEditBegin; EditList::const_iterator pEditEnd = edits.begin() + hunk.idxEditEnd; if (hunk.format == FORMAT_Normal) { stream.Printf(sig, "%s\n", hunk.TextizeRange_Normal().c_str()); for ( ; pEdit != pEditEnd; pEdit++) { stream.Println(sig, TextizeEdit_Normal(*pEdit).c_str()); if (sig.IsSignalled()) return false; } } else if (hunk.format == FORMAT_Context) { } else if (hunk.format == FORMAT_Unified) { stream.Printf(sig, "@@ %s @@\n", hunk.TextizeRange_Unified().c_str()); if (sig.IsSignalled()) return false; for ( ; pEdit != pEditEnd; pEdit++) { stream.Println(sig, TextizeEdit_Unified(*pEdit).c_str()); if (sig.IsSignalled()) return false; } } return true; }
bool Stream::Compare(Signal &sig, Stream &stream) { if (!CheckReadable(sig) || !stream.CheckReadable(sig)) return false; const size_t bytesBuff = 1024 * 16; AutoPtr<Memory> pMemory(new MemoryHeap(bytesBuff * 2)); void *buff1 = pMemory->GetPointer(0); void *buff2 = pMemory->GetPointer(bytesBuff); bool sameFlag = false; for (;;) { size_t bytesRead1 = Read(sig, buff1, bytesBuff); if (sig.IsSignalled()) return false; size_t bytesRead2 = stream.Read(sig, buff2, bytesBuff); if (sig.IsSignalled()) return false; if (bytesRead1 != bytesRead2) { break; } else if (bytesRead1 == 0) { sameFlag = true; break; } else if (::memcmp(buff1, buff2, bytesRead1) != 0) { break; } } return sameFlag; }
Value Object_interp::ConvFromTclObj(Environment &env, Signal &sig, Tcl_Obj *objPtr) { Tcl_ObjType *typePtr = objPtr->typePtr; if (typePtr == nullptr) { return Value(objPtr->bytes, objPtr->length); } else if (typePtr == ObjType_boolean) { int value; ::Tcl_GetBooleanFromObj(_interp, objPtr, &value); return Value(value? true : false); } else if (typePtr == ObjType_bytearray) { int length; unsigned char *binary = ::Tcl_GetByteArrayFromObj(objPtr, &length); return Value(new Object_binary(env, Binary(reinterpret_cast<char *>(binary), length), true)); } else if (typePtr == ObjType_double) { double value; ::Tcl_GetDoubleFromObj(_interp, objPtr, &value); return Value(value); } else if (typePtr == ObjType_int) { int value; ::Tcl_GetIntFromObj(_interp, objPtr, &value); return Value(value); } else if (typePtr == ObjType_list) { int length; ::Tcl_ListObjLength(_interp, objPtr, &length); Value result; Object_list *pObjList = result.InitAsList(env); pObjList->Reserve(length); for (int i = 0; i < length; i++) { Tcl_Obj *objElemPtr; ::Tcl_ListObjIndex(_interp, objPtr, i, &objElemPtr); pObjList->Add(ConvFromTclObj(env, sig, objElemPtr)); if (sig.IsSignalled()) return Value::Nil; } return result; } else if (typePtr == ObjType_string) { int length; char *value = ::Tcl_GetStringFromObj(objPtr, &length); return Value(value); } return Value(objPtr->bytes, objPtr->length); }
//----------------------------------------------------------------------------- // utilities //----------------------------------------------------------------------------- bool DoGLSection(Environment &env, Signal &sig, Argument &arg, Image *pImage) { #if GURA_USE_MSWIN_DIB PIXELFORMATDESCRIPTOR pfd = { sizeof(PIXELFORMATDESCRIPTOR), 1, PFD_DRAW_TO_BITMAP | PFD_SUPPORT_OPENGL, PFD_TYPE_RGBA, static_cast<BYTE>(pImage->GetBitsPerPixel()), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, PFD_MAIN_PLANE, 0, 0, 0, 0 }; HDC hdc = ::CreateCompatibleDC(nullptr); HBITMAP hBmp = pImage->GetHBITMAP(); HBITMAP hBmpOld = reinterpret_cast<HBITMAP>(::SelectObject(hdc, hBmp)); int iPixelFormat = ::ChoosePixelFormat(hdc, &pfd); ::SetPixelFormat(hdc, iPixelFormat, &pfd); HGLRC hglrc = ::wglCreateContext(hdc); ::wglMakeCurrent(hdc, hglrc); const Expr_Block *pExprBlock = arg.GetBlockCooked(env); if (!sig.IsSignalled()) { pExprBlock->Exec(env); } ::wglMakeCurrent(nullptr, nullptr); ::wglDeleteContext(hglrc); ::SelectObject(hdc, hBmpOld); ::DeleteDC(hdc); return true; #elif defined(GURA_ON_DARWIN) //***************** // not working yet. //***************** GLsizei width = static_cast<GLsizei>(pImage->GetWidth()); GLsizei height = static_cast<GLsizei>(pImage->GetHeight()); CGLError errCode = kCGLNoError; GLuint texOut = 0; CGLContextObj ctx = nullptr; CGLContextObj ctxOrg = CGLGetCurrentContext(); CGLPixelFormatAttribute attributes[4] = { kCGLPFAAccelerated, kCGLPFAOpenGLProfile, (CGLPixelFormatAttribute)kCGLOGLPVersion_3_2_Core, (CGLPixelFormatAttribute)0 }; CGLPixelFormatObj pixelFormat = nullptr; GLint numPixelFormats = 0; errCode = CGLChoosePixelFormat(attributes, &pixelFormat, &numPixelFormats); errCode = CGLCreateContext(pixelFormat, nullptr, &ctx); errCode = CGLSetCurrentContext(ctx); glGenTextures(1, &texOut); glBindTexture(GL_TEXTURE_2D, texOut); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_FLOAT, nullptr); GLuint frameBuffer = 0; glGenFramebuffers(1, &frameBuffer); glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texOut, 0); GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); if (status == GL_FRAMEBUFFER_COMPLETE) { const Expr_Block *pExprBlock = arg.GetBlockCooked(env); if (!sig.IsSignalled()) { pExprBlock->Exec(env); } } do { GLenum format = GetImageFormat(env, pImage); if (sig.IsSignalled()) return false; glBindTexture(GL_TEXTURE_2D, texOut); glGetTexImage(GL_TEXTURE_2D, 0, format, GL_UNSIGNED_BYTE, pImage->GetBuffer()); } while (0); glDeleteTextures(1, &texOut); glBindFramebuffer(GL_FRAMEBUFFER, 0); CGLSetCurrentContext(ctxOrg); CGLDestroyContext(ctx); return true; #else #if 0 int width = static_cast<int>(pImage->GetWidth()); int height = static_cast<int>(pImage->GetHeight()); GLXFBConfig config; Pixmap pixmap = ::XCreatePixmap(nullptr, d, width, height, 32); GLXPixmap xid = ::glXCreatePixmap(nullptr, config, pixmap, nullptr); GLXContext ctx; ::glXMakeCurrent(nullptr, xid, ctx); const Expr_Block *pExprBlock = arg.GetBlockCooked(env); if (!sig.IsSignalled()) { pExprBlock->Exec(env); } ::glReadPixels(0, 0, width, height, GL_BGRA_EXT, GL_UNSIGNED_BYTE, pImage->GetBuffer()); #endif #endif return true; }
void SimpleStream::PrintFmt(Signal &sig, const char *format, const ValueList &valList) { String str = Formatter::FormatValueList(sig, format, valList); if (sig.IsSignalled()) return; Print(sig, str.c_str()); }
bool Formatter::DoFormat(Signal &sig, const char *format, Source &source) { bool eatNextFlag; const char *formatp = format; Flags flags; enum { STAT_Start, STAT_FlagsPre, STAT_Flags, STAT_FlagsAfterWhite, STAT_PrecisionPre, STAT_Precision, STAT_Padding, } stat = STAT_Start; for (;;) { char ch = *formatp; eatNextFlag = true; if (ch == '\0') { break; } else if (stat == STAT_Start) { if (ch == '%') { stat = STAT_FlagsPre; } else if (ch == '\n') { for (const char *p = _lineSep; *p != '\0'; p++) { if (!PutChar(sig, *p)) return false; } } else { if (!PutChar(sig, ch)) return false; } } else if (stat == STAT_FlagsPre) { if (ch == '%') { if (!PutChar(sig, ch)) return false; stat = STAT_Start; } else { if (source.IsEnd()) { SetError_NotEnoughArguments(sig); break; } // initialize flags flags.upperCaseFlag = false; flags.leftAlignFlag = false; flags.sharpFlag = false; flags.fieldMinWidth = 0; flags.precision = PREC_Default; flags.plusMode = PLUSMODE_None; flags.charPadding = ' '; eatNextFlag = false; stat = STAT_Flags; } } else if (stat == STAT_Flags) { if (ch == '#') { flags.sharpFlag = true; } else if (ch == '0') { if (!flags.leftAlignFlag) { flags.charPadding = '0'; } } else if (ch == '-') { flags.leftAlignFlag = true; flags.charPadding = ' '; } else if (ch == ' ') { if (flags.plusMode == PLUSMODE_None) { flags.plusMode = PLUSMODE_Space; } stat = STAT_FlagsAfterWhite; } else if (ch == '+') { flags.plusMode = PLUSMODE_Plus; } else if (ch == '*') { Value value = source.GetInt(); if (!value.Is_number()) { sig.SetError(ERR_ValueError, "number is expected for * specifier"); break; } flags.fieldMinWidth = static_cast<int>(value.GetNumber()); if (flags.fieldMinWidth < 0) { flags.leftAlignFlag = true; flags.fieldMinWidth = -flags.fieldMinWidth; } if (source.IsEnd()) { SetError_NotEnoughArguments(sig); break; } } else if ('0' < ch && ch <= '9') { eatNextFlag = false; stat = STAT_Padding; } else if (ch == '.') { stat = STAT_PrecisionPre; } else if (ch == 'l') { // just ignore it } else if (ch == 'd' || ch == 'i') { Value value = source.GetInt(); if (!value.GetClass()->Format_d(this, flags, value)) break; stat = STAT_Start; } else if (ch == 'u') { Value value = source.GetInt(); if (!value.GetClass()->Format_u(this, flags, value)) break; stat = STAT_Start; } else if (ch == 'b') { Value value = source.GetInt(); if (!value.GetClass()->Format_b(this, flags, value)) break; stat = STAT_Start; } else if (ch == 'o') { Value value = source.GetInt(); if (!value.GetClass()->Format_o(this, flags, value)) break; stat = STAT_Start; } else if (ch == 'x' || ch == 'X') { Value value = source.GetInt(); flags.upperCaseFlag = (ch == 'X'); if (!value.GetClass()->Format_x(this, flags, value)) break; stat = STAT_Start; } else if (ch == 'e' || ch == 'E') { Value value = source.GetDouble(); flags.upperCaseFlag = (ch == 'E'); if (!value.GetClass()->Format_e(this, flags, value)) break; stat = STAT_Start; } else if (ch == 'f' || ch == 'F') { Value value = source.GetDouble(); if (!value.GetClass()->Format_f(this, flags, value)) break; stat = STAT_Start; } else if (ch == 'g' || ch == 'G') { Value value = source.GetDouble(); flags.upperCaseFlag = (ch == 'G'); if (!value.GetClass()->Format_g(this, flags, value)) break; stat = STAT_Start; } else if (ch == 's') { Value value = source.GetString(); if (!value.GetClass()->Format_s(this, flags, value)) break; stat = STAT_Start; } else if (ch == 'c') { Value value = source.GetInt(); if (!value.GetClass()->Format_c(this, flags, value)) break; stat = STAT_Start; } else { SetError_WrongFormat(sig); break; } } else if (stat == STAT_FlagsAfterWhite) { if (ch == ' ') { SetError_WrongFormat(sig); break; } else { eatNextFlag = false; stat = STAT_Flags; } } else if (stat == STAT_Padding) { if ('0' <= ch && ch <= '9') { flags.fieldMinWidth = flags.fieldMinWidth * 10 + (ch - '0'); } else { eatNextFlag = false; stat = STAT_Flags; } } else if (stat == STAT_PrecisionPre) { if (ch == '*') { Value value = source.GetInt(); if (!value.Is_number()) { sig.SetError(ERR_ValueError, "number is expected for * specifier"); break; } flags.precision = static_cast<int>(value.GetNumber()); if (flags.precision < 0) flags.precision = PREC_Default; if (source.IsEnd()) { SetError_NotEnoughArguments(sig); break; } stat = STAT_Flags; } else if ('0' <= ch && ch <= '9') { flags.precision = 0; eatNextFlag = false; stat = STAT_Precision; } else { flags.precision = PREC_Null; eatNextFlag = false; stat = STAT_Flags; } } else if (stat == STAT_Precision) { if ('0' <= ch && ch <= '9') { flags.precision = flags.precision * 10 + (ch - '0'); } else { eatNextFlag = false; stat = STAT_Flags; } } if (eatNextFlag) formatp++; } return !sig.IsSignalled(); }