void Text::AddText(const std::wstring& text, unsigned long style, OS::Font *font) { size_t start,pos; if (font==NULL) { font=defaultFont; } start=0; pos=0; while (pos<text.length()) { if (text[pos]==L'\n') { AddTextToPart(text.substr(start,pos-start),style,font); CloseLine(); start=pos+1; } pos++; } if (start<text.length()) { AddTextToPart(text.substr(start,pos-start),style,font); } if (calced) { CalcTextSize(); if (visible) { Redraw(); } } }
void AddByte(unsigned char byt) { if (remainingBytesOnLine == 0) { CloseLine(); remainingBytesOnLine = 8; putchar(' '); } printf(" 0x%02x,", byt); ++totalBytes; --remainingBytesOnLine; }
BOOL FAR MergeFile(LPSTR FileName, int view) { HCURSOR hSaveCursor; char line[MAX_USER_LINE + 1]; int offset; //Current offset of storage in block int lineLen; // length of line BYTE prevLength; //Previous line size LPVIEWREC v = &Views[view]; WORD res; LPDOCREC d =&Docs[v->Doc]; LPLINEREC pCurLine, pLastLine; LPBLOCKDEF pCurBlock, pNewBlock; long y; Assert( v->Doc >= 0); if ((hFileDoc = _lopen(FileName, OF_READ)) == HFILE_ERROR) return ErrorBox(ERR_File_Open, (LPSTR)FileName); if ((pszBufferDoc = DocAlloc(DISK_BLOCK_SIZE)) == NULL) { ErrorBox(SYS_Allocate_Memory); goto error1; } dwBytesReadDoc = DISK_BLOCK_SIZE; dwOffsetDoc = DISK_BLOCK_SIZE; lineLen = 0; //Delete selected text if any if (v->BlockStatus) { long XR, YR; GetBlockCoord (view, &(v->X), &(v->Y), &XR, &YR); DeleteStream(view, v->X, v->Y, XR, YR, FALSE); } //Set the Hour glass cursor hSaveCursor = SetCursor(LoadCursor(NULL, IDC_WAIT)); //Insert first line res = LoadLine(line, &lineLen, d->NbLines); if (res == END_OF_FILE || res == END_OF_LINE) { //Add a CR+LF if (res == END_OF_LINE) { line[lineLen] = CR; line[lineLen + 1] = LF; lineLen += 2; } if (!InsertBlock(v->Doc, v->X, v->Y, (WORD) lineLen, line)) goto error2; } else { if (res == ERR_File_Read || res == ERR_Not_A_Text_File) ErrorBox(res, (LPSTR)FileName); goto error2; } if (res != END_OF_FILE) { //Get current line status (we just inserted it) y = v->Y; if (!FirstLine(v->Doc, &pCurLine, &y, &pCurBlock)) return FALSE; //Get next line (just after the line we just inserted) if (!NextLine(v->Doc, &pCurLine, &y, &pCurBlock)) return FALSE; //Set offset to StoreLine start offset = (LPSTR)pCurLine - (LPSTR)pCurBlock->Data; prevLength = pCurLine->PrevLength; //Split block in 2 blocks by first allocating a new block and then //copying right side of block in new block if (!AllocateBlock(pCurBlock, pCurBlock->NextBlock, &pNewBlock)) return FALSE; pLastLine = (LPLINEREC)(pCurBlock->Data + pCurBlock->LastLineOffset); memmove(pNewBlock->Data, (LPSTR)pCurLine, (LPSTR)pLastLine - (LPSTR)pCurLine + pLastLine->Length); //Set new old block len and new new block len pCurBlock->LastLineOffset = (LPSTR)pCurLine - (LPSTR)pCurBlock->Data - pCurLine->PrevLength; pNewBlock->LastLineOffset = (LPSTR)pLastLine - (LPSTR)pCurLine; //Backward links next block with new one if (pCurBlock->NextBlock == NULL) d->LastBlock = pNewBlock; else (pCurBlock->NextBlock)->PrevBlock = pNewBlock; //Forward link current block with new block pCurBlock->NextBlock = pNewBlock; CloseLine(v->Doc, &pCurLine, y, &pCurBlock); //Read and store all lines in new blocks res = LoadLine(line, &lineLen, d->NbLines); while (res == END_OF_LINE) { //Truncate a file too large if (d->NbLines >= MAX_LINE_NUMBER - 1) { ErrorBox(ERR_Truncate_Doc); res = END_OF_FILE; break; } if (!StoreLine(line, lineLen, &offset, &prevLength, &pCurBlock)) { res = END_ABORT; break; } res = LoadLine(line, &lineLen, ++d->NbLines); } //Take decisions switch (res) { case END_OF_FILE: //Store last line if (StoreLine(line, lineLen, &offset, &prevLength, &pCurBlock)) { d->NbLines++; //Forward link of last allocated block with new block pCurBlock->NextBlock = pNewBlock; //Backward link of new block with last allocated block pNewBlock->PrevBlock = pCurBlock; ((LPLINEREC)(pNewBlock->Data))->PrevLength = (BYTE)(lineLen + LHD); //Free memory if (!DocFree(pszBufferDoc)) InternalErrorBox(SYS_Free_Memory); //Restore cursor SetCursor(hSaveCursor); //Check syntax if C if (d->language == C_LANGUAGE) { d->lineTop = 0; d->lineBottom = d->NbLines; CheckSyntax(v->Doc); } SetVerticalScrollBar(view, TRUE); PosXY(view, v->X, v->Y, FALSE); InvalidateRect(v->hwndClient, (LPRECT)NULL, FALSE); CloseHandle (hFileDoc); return TRUE; } else goto abort; case ERR_File_Read: case ERR_Not_A_Text_File: ErrorBox(res, (LPSTR)FileName); //Fall through case END_ABORT: { abort: SetCursor(hSaveCursor); break; } default: Assert(FALSE); break; } } else InvalidateRect(v->hwndClient, (LPRECT)NULL, FALSE); error2: { SetCursor (hSaveCursor); if (!DocFree(pszBufferDoc)) InternalErrorBox(SYS_Free_Memory); } error1: { CloseHandle (hFileDoc); } return FALSE; } /* MergeFile() */
//Save document with (possibly) a new file name BOOL FAR SaveDocument(int doc, LPSTR FileName) { LPLINEREC pl; LPBLOCKDEF pb; long y; register WORD i; WORD len; HCURSOR hSaveCursor; LPDOCREC d = &Docs[doc]; //Set the Hour glass cursor hSaveCursor = SetCursor(LoadCursor(NULL, IDC_WAIT)); //Create file if (HFILE_ERROR == (hFileDoc = _lcreat(FileName, 0)) ) { ErrorBox(ERR_File_Create, (LPSTR)FileName); goto error0; } //Allocate space for Disk Buffer if ((pszBufferDoc = DocAlloc(DISK_BLOCK_SIZE)) == NULL) { ErrorBox(SYS_Allocate_Memory); goto error1; } dwOffsetDoc = 0; y = 0; //Get first line if (!FirstLine (doc, &pl, &y, &pb)) return FALSE; //Save each line of file while (TRUE) { //Remove trailing blanks len = (WORD) (pl->Length - LHD); { TCHAR *pch1; pch1 = pl->Text + (pl->Length - LHD); while (pch1 > pl->Text) { pch1 = CharPrev(pl->Text, pch1); if (*pch1 != ' ' && *pch1 != TAB) { break; } len --; } } //Write line i = 0; while (i < len) { PutChar(FileName, pl->Text[i]); i++; } if (y >= d->NbLines) { if (dwOffsetDoc && !Flush(FileName)) goto error2; break; } else { PutChar (FileName, CR); PutChar (FileName, LF); if (!NextLine (doc, &pl, &y, &pb)) goto error2; } } CloseHandle (hFileDoc); //Update internal file date GetSystemTimeAsFileTime(&d->time); CloseLine(doc, &pl, y, &pb); d->ismodified = FALSE; RefreshWindowsTitle(doc); SetCursor (hSaveCursor); return TRUE; error2: CloseLine(doc, &pl, y, &pb); if (!DocFree(pszBufferDoc)) InternalErrorBox(SYS_Free_Memory); error1: CloseHandle (hFileDoc); error0: SetCursor (hSaveCursor); return FALSE; } /* SaveDocument() */