struct LineNode *GetLines(const char *FilePath) { FILE *file = NULL; struct LineNode *head = NULL; struct LineNode *tail = NULL; char *currentLine = NULL; size_t lineLength = 0; wordexp_t expanded; memset(&expanded, 0, sizeof(wordexp_t)); //Expand the path based on any shell characters given. wordexp(FilePath, &expanded, 0); check(expanded.we_wordv[0] != NULL, "Unable to expand file path for %s", FilePath); file = fopen(expanded.we_wordv[0], "r"); check(file != NULL, "Unable to open file: %s.", FilePath); //Get the first line and set it to the head of the list. check(getline(¤tLine, &lineLength, file) != -1, "Unable to read line, or EOF."); head = GetNode(currentLine, 1); check(head != NULL, "Unable to get the first line of the file."); tail = head; //We have to set currentLine to NULL, otherwise //we're reusing the same memory for each line... currentLine = NULL; //Get the rest of the lines... while(getline(¤tLine, &lineLength, file) != -1) { int LineNumber = tail->LineNumber + 1; struct LineNode *newNode = GetNode(currentLine, LineNumber); check(newNode != NULL, "Unable to get line %d.", LineNumber); tail->Next = newNode; tail = newNode; currentLine = NULL; } if(file) fclose(file); return head; error: //We clean up whatever has been constructed of the linked list... DeleteLines(head); if(file) fclose(file); return NULL; }
int _cdecl wmain(int argc, LPCWSTR argv[]) { g_reverse = (argc == 2 && 0 == _wcsicmp(argv[1], L"-reverse")); line *first = ReadLines(); LPWSTR *lines = nullptr; int count = 0; for (auto current = first; current != nullptr; current = current->next) { count++; } if (count == 0) { goto Cleanup; } lines = new LPWSTR[count]; if (lines == nullptr) { ERR(L"Out of memory"); goto Cleanup; } auto current = first; for (int i = 0; i < count; i++) { lines[i] = current->text; current = current->next; } qsort(lines, count, sizeof(lines[0]), shellsort); for (int i = 0; i < count; i++) { LOG(L"%s", lines[i]); } Cleanup: DeleteLines(first); delete [] lines; return 0; }
void CScreenDevice::Write (char chChar) { switch (m_nState) { case ScreenStateStart: switch (chChar) { case '\b': CursorLeft (); break; case '\t': Tabulator (); break; case '\n': NewLine (); break; case '\r': CarriageReturn (); break; case '\x1b': m_nState = ScreenStateEscape; break; default: DisplayChar (chChar); break; } break; case ScreenStateEscape: switch (chChar) { case 'M': ReverseScroll (); m_nState = ScreenStateStart; break; case '[': m_nState = ScreenStateBracket; break; default: m_nState = ScreenStateStart; break; } break; case ScreenStateBracket: switch (chChar) { case '?': m_nState = ScreenStateQuestionMark; break; case 'A': CursorUp (); m_nState = ScreenStateStart; break; case 'B': CursorDown (); m_nState = ScreenStateStart; break; case 'C': CursorRight (); m_nState = ScreenStateStart; break; case 'H': CursorHome (); m_nState = ScreenStateStart; break; case 'J': ClearDisplayEnd (); m_nState = ScreenStateStart; break; case 'K': ClearLineEnd (); m_nState = ScreenStateStart; break; case 'L': InsertLines (1); m_nState = ScreenStateStart; break; case 'M': DeleteLines (1); m_nState = ScreenStateStart; break; case 'P': DeleteChars (1); m_nState = ScreenStateStart; break; default: if ('0' <= chChar && chChar <= '9') { m_nParam1 = chChar - '0'; m_nState = ScreenStateNumber1; } else { m_nState = ScreenStateStart; } break; } break; case ScreenStateNumber1: switch (chChar) { case ';': m_nState = ScreenStateSemicolon; break; case 'L': InsertLines (m_nParam1); m_nState = ScreenStateStart; break; case 'M': DeleteLines (m_nParam1); m_nState = ScreenStateStart; break; case 'P': DeleteChars (m_nParam1); m_nState = ScreenStateStart; break; case 'X': EraseChars (m_nParam1); m_nState = ScreenStateStart; break; case 'h': case 'l': if (m_nParam1 == 4) { InsertMode (chChar == 'h'); } m_nState = ScreenStateStart; break; case 'm': SetStandoutMode (m_nParam1); m_nState = ScreenStateStart; break; default: if ('0' <= chChar && chChar <= '9') { m_nParam1 *= 10; m_nParam1 += chChar - '0'; if (m_nParam1 > 99) { m_nState = ScreenStateStart; } } else { m_nState = ScreenStateStart; } break; } break; case ScreenStateSemicolon: if ('0' <= chChar && chChar <= '9') { m_nParam2 = chChar - '0'; m_nState = ScreenStateNumber2; } else { m_nState = ScreenStateStart; } break; case ScreenStateQuestionMark: if ('0' <= chChar && chChar <= '9') { m_nParam1 = chChar - '0'; m_nState = ScreenStateNumber3; } else { m_nState = ScreenStateStart; } break; case ScreenStateNumber2: switch (chChar) { case 'H': CursorMove (m_nParam1, m_nParam2); m_nState = ScreenStateStart; break; case 'r': SetScrollRegion (m_nParam1, m_nParam2); m_nState = ScreenStateStart; break; default: if ('0' <= chChar && chChar <= '9') { m_nParam2 *= 10; m_nParam2 += chChar - '0'; if (m_nParam2 > 199) { m_nState = ScreenStateStart; } } else { m_nState = ScreenStateStart; } break; } break; case ScreenStateNumber3: switch (chChar) { case 'h': case 'l': if (m_nParam1 == 25) { SetCursorMode (chChar == 'h'); } m_nState = ScreenStateStart; break; default: if ('0' <= chChar && chChar <= '9') { m_nParam1 *= 10; m_nParam1 += chChar - '0'; if (m_nParam1 > 99) { m_nState = ScreenStateStart; } } else { m_nState = ScreenStateStart; } break; } break; default: m_nState = ScreenStateStart; break; } }
Bool32 RemoveLines(PRSPreProcessImage Image, puchar * lppDIB) { Handle hccom = *Image->phCCOM; Handle hcpage = Image->hCPAGE; Handle *hLinesCCOM = Image->phLinesCCOM; puchar hDIB = NULL; Bool32 rc = TRUE; *hLinesCCOM = NULL; CCOM_comp *victim[100]; int32_t nvict,i; Bool32 yes_victim = FALSE; // // Удалим линии // if(!LDPUMA_Skip(ObvKillLines)||(LDPUMA_Skip(hNewLine)&&LDPUMA_Skip(Image->hDebugCancelVerifyLines))) rc=DeleteLines(hcpage,Image->phCLINE,PUMA_IMAGE_DELLINE); else { if(rc && !RLINE_DeleteLines(hcpage,PUMA_IMAGE_DELLINE)) { SetReturnCode_rstuff(RLINE_GetReturnCode()); rc = FALSE; } if (rc && LDPUMA_Skip(NotKillPointed) && LDPUMA_Skip(Image->hDebugCancelSearchDotLines)) rc = DeleteDotLines(Image->phCLINE, PUMA_IMAGE_DELLINE); } LDPUMA_Skip(hPrep2); // // Получим изображение с удаленными линиями // if(rc && !CIMAGE_ReadDIB((puchar)PUMA_IMAGE_DELLINE,(Handle*)&hDIB,TRUE)) { SetReturnCode_rstuff(CIMAGE_GetReturnCode()); rc = FALSE; } if(hDIB) { // // Удалим компоненты и выделим их заново. // *lppDIB = (puchar)hDIB; if(rc) { //if( CCOM_GetContainerVolume((CCOM_handle)*Image->phCCOM)>30000 ) // CCOM_CompressContatiner((CCOM_handle)*Image->phCCOM); if( CCOM_GetContainerVolume((CCOM_handle)*Image->phCCOM)<60000 && MyGetZher ((void**)victim, &nvict, 100, hcpage) && nvict ) yes_victim = TRUE; if( !yes_victim ) { CCOM_DeleteContainer((CCOM_handle)*Image->phCCOM); *Image->phCCOM = 0; } if(!ExtractComponents(FALSE, hLinesCCOM, (puchar)PUMA_IMAGE_DELLINE, Image)) { rc = FALSE; } else { PAGEINFO inf = {0}; GetPageInfo(Image->hCPAGE,&inf); strcpy((char*)inf.szImageName,PUMA_IMAGE_DELLINE); inf.Images|=IMAGE_DELLINE; SetPageInfo(Image->hCPAGE,inf); } if(rc) { *Image->phCCOM = (Handle)REXCGetContainer(); if(*Image->phCCOM == 0) { SetReturnCode_rstuff(REXC_GetReturnCode()); rc = FALSE; } hccom = *Image->phCCOM; if( *hLinesCCOM ) { // // Refersh CCOM // CCOM_comp *exa=CCOM_GetFirst((CCOM_handle)*hLinesCCOM,NULL); if( yes_victim ) { /* Rect16 rect1; uint32_t key = 111; for(i=0;i<nvict;i++) { exa = victim[i]; rect1.top = exa->upper; rect1.left = exa->left; rect1.bottom = exa->upper+exa->h; rect1.right = exa->left+exa->w; LDPUMA_DrawRect(NULL, &rect1, 0, 23635, 1, key); } if(!LDPUMA_Skip(hShowCheckLetters)) { LDPUMA_Console("Puma_Коробки жертв \n"); LDPUMA_WaitUserInput(NULL, NULL); LDPUMA_DeleteRects(NULL, key); } */ for(i=0;i<nvict;i++) { exa = victim[i]; if( remove_overlayed(exa,(CCOM_handle)*Image->phCCOM) ) { CCOM_comp *dup=CCOM_New((CCOM_handle)*Image->phCCOM,exa->upper,exa->left,exa->w,exa->h); if( dup ) { CCOM_Store(dup,0, exa->size_linerep, exa->linerep, exa->nl, exa->begs, exa->ends, exa->vers, NULL); dup->scale= exa->scale; dup->type = exa->type; dup->cs = exa->cs; } } } } CCOM_DeleteContainer((CCOM_handle)*hLinesCCOM); } } else LDPUMA_Console("Пропущен этап выделения компонент после удаления линий.\n"); } } return rc; }
line *ReadLines() { line *firstLine = nullptr; line *currentLine = nullptr; chunk *firstChunk = nullptr; chunk *currentChunk = nullptr; size_t currentLen = 0; bool isEof = false; while (!isEof) { bool isEndOfLine = false; // allocate a chunk if (firstChunk == nullptr) { firstChunk = new chunk; currentChunk = firstChunk; } else { currentChunk->next = new chunk; currentChunk = currentChunk->next; } if (currentChunk == nullptr) { ERR(L"Out of memory"); goto Error; } if (fgetws(currentChunk->text, _countof(currentChunk->text), stdin)) { // is this the end of a line? size_t len = wcslen(currentChunk->text); currentLen += len; if (len > 0 && currentChunk->text[len - 1] == L'\n') { // trim the newline currentChunk->text[len - 1] = L'\0'; currentLen--; isEndOfLine = true; } } else { int error = ferror(stdin); if (error != 0) { ERR(L"STDIN error: %d", error); goto Error; } else if (feof(stdin)) { isEof = true; } else { ERR(L"fgetws returned null but STDIN is neither EOF nor in error"); goto Error; } } if (currentLen > 0 && (isEndOfLine || isEof)) { // allocate a line if (firstLine == nullptr) { firstLine = new line; currentLine = firstLine; } else { currentLine->next = new line; currentLine = currentLine->next; } if (currentLine == nullptr) { ERR(L"Out of memory"); goto Error; } currentLen++; // add terminating null currentLine->text = new WCHAR[currentLen]; if (currentLine->text == nullptr) { ERR(L"Out of memory"); goto Error; } auto s = ¤tLine->text[0]; for (auto p = firstChunk; p != nullptr; p = p->next) { auto len = wcslen(p->text); auto err = wcscpy_s(s, currentLen, p->text); if (err) { ERR(L"wcscpy_s failed: %u", err); goto Error; } s += len; currentLen -= len; } currentLen--; // should be 0 now // free all the chunks we used to create this line DeleteChunks(firstChunk); firstChunk = nullptr; } } // while return firstLine; Error: DeleteChunks(firstChunk); DeleteLines(firstLine); return nullptr; }
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { MSG msg; int dwRefresh; LARGE_INTEGER LargeInt; if (InitInstance(hInstance) == FALSE) { return FALSE; } //Init random seed QueryPerformanceCounter(&LargeInt); srand(LargeInt.LowPart); //////////////////////////////////////// InitializeCriticalSection(&crsCurPiece); ClearBoard(); dwRefresh = GetTickCount(); PushRandomPiece(); DrawBoard(); UpdateScreen(); while (1) { if (PeekMessageA(&msg, NULL, 0, 0, PM_NOREMOVE) == TRUE) { if (GetMessageA(&msg, NULL, 0, 0) == FALSE) { return 0; } TranslateMessage(&msg); DispatchMessage(&msg); } else { if (GetTickCount() >= (DWORD)(dwRefresh + FallingPieceSpeed)) { DrawBoard(); UpdateScreen(); dwRefresh = GetTickCount(); EnterCriticalSection(&crsCurPiece); if (IsActionPossible(CurPiece.Piece, CurPiece.PosX, CurPiece.PosY + 1) == TRUE) { CurPiece.PosY++; } else { if (SaveCurPiece() == TRUE) { MessageBoxA(NULL, "Game Over!", NULL, MB_OK); ExitProcess(0); } DeleteLines(); PushRandomPiece(); } LeaveCriticalSection(&crsCurPiece); } Sleep(100); //give that CPU some rest } } return 0; }