int MemBlock::Copy(const char* pDestBuffer, const char* pSrcBuffer, size_t iSize) { ssize_t iDestOffset = pDestBuffer - _begin; if ( pDestBuffer < _begin || pDestBuffer > _end ) { return -1; } ssize_t iSrcOffset = pSrcBuffer - _begin; size_t iDestSize = iDestOffset + iSize; ssize_t iIncSize = iDestSize - Size(); if ( iIncSize < 0 ) { iIncSize = 0; } if ( iDestSize > TotalSpace() ) { if ( Resize(iDestSize) != 0 ) { return -1; } } if ( iSrcOffset >= 0 && (size_t)iSrcOffset < Size() ) { memmove(_begin+iDestOffset, _begin+iSrcOffset, iSize); } else { memcpy(_begin+iDestOffset, pSrcBuffer, iSize); } return AddSpace(iIncSize); }
char FindToken (char in) { char c = in; while (c == 0 || c == ' ' || c == '\t') { c = getchar (); AddSpace (c); if (ECHO) { putchar (c); } } return c; }
struct TokenStruct NextToken (char *c) { *c = FindToken (*c); struct TokenStruct ret;// = malloc (sizeof (struct TokenStruct)); ret.Token = malloc (sizeof (char) * 2); ret.Token[0] = *c; ret.type = TokenType (*c); ret.priority = OpPriority (*c); ret.index = line_index; int length = 1; if (!ret.type) { if (*c == '+' || *c == '-' || *c == '*' || *c == '/' || *c == '%' || *c == '^' || *c == '=' || *c == ')' || *c == '(' || *c == ',') { // Grab next character if we have a valid token *c = FindToken (0); } ret.Token[1] = '\0'; #ifdef DEBUG if (ret.Token[0] != '\n') { printf (" Op/Invalid Token %i: '%s';\n", ret.index, ret.Token); } else { printf (" Op/Invalid Token %i: '\\n';\n", ret.index); } sleep (1); #endif return ret; } *c = getchar (); AddSpace (*c); while (TokenType (*c) == ret.type) { ret.Token[length++] = *c; if (ECHO) { putchar (*c); } ret.Token = realloc (ret.Token, sizeof (char) * (length+1)); *c = getchar (); AddSpace (*c); } if (ECHO) { putchar (*c); } //line_index += length; ret.Token[length] = '\0'; *c = FindToken (*c); #ifdef DEBUG printf (" Var/Num Token %i: %s;\n", ret.index, ret.Token); sleep (1); #endif return ret; }
bool CMaterialLib::AddToArray(eArrayType p_Type, void *p_pSrc) { switch(p_Type) { case MATERIALLIB_ARRTYPE_MATERIAL: if(m_lMaterialArraySize == m_lMaterialCount) if(!AddSpace(p_Type)) return(false); m_pMaterialArray[m_lMaterialCount] = *(rMaterial *)p_pSrc; m_lMaterialCount ++; m_pCurMaterial = &m_pMaterialArray[m_lMaterialCount - 1]; break; } return(true); }
void Maze::Generate(unsigned size){ m_size = size; srand((unsigned)time(0)); m_wall_list.clear(); data.clear(); //black out whole maze. for (unsigned i = 0; i < (m_size*m_size); ++i) data.push_back(BLACK_SPACE); //add first wall and space data[at_(1, (m_size - 1))] = SPACE; AddWall(at_(1, (m_size - 2))); while (m_wall_list.size() != 0){ unsigned wall_index = (m_wall_list.size() == 1)? 0:rand() % (m_wall_list.size()-1); position wall_pos = m_wall_list[wall_index]; position opp_side = get_opp_side(wall_pos); if (opp_side == 0){ m_wall_list.erase(m_wall_list.begin() + wall_index); continue; } if (data[opp_side] == BLACK_SPACE && !In_wall_list(opp_side)){ data[wall_pos] = SPACE; m_wall_list.erase(m_wall_list.begin() + wall_index); AddSpace(opp_side); if (data[opp_side] == SPACE){ AddWall(opp_side + 1); AddWall(opp_side - 1); AddWall(opp_side + m_size); AddWall(opp_side - m_size); } } else m_wall_list.erase(m_wall_list.begin() + wall_index); } AddExit(); }
/******************************************************************************* * Slot function. Translates command object recevied from the parser thread * to appropriate function call. * ****************************************************************************/ void DialogBox::ExecuteCommand(DialogCommand command) { QWidget* widget=NULL; if(empty) { ClearDialog(); empty=false; } switch(command.command & DialogCommand::command_mask) { case DialogCommand::add: if(command.command & DialogCommand::option_space & DialogCommand::option_mask) { // Seems sscanf %d in some versions of standard C library has a bug // returning 32k on sero-size strings if(command.GetText()[0]) { int size; sscanf(command.GetText(), "%d", &size); AddSpace(size); } else AddSpace(); break; } if(command.command & DialogCommand::option_stretch & DialogCommand::option_mask) { AddStretch(); break; } switch(command.control & ~DialogCommand::property_mask) { case DialogCommand::label: AddLabel(command.GetTitle(), command.GetName(), command.control & DialogCommand::property_picture & DialogCommand::property_mask ? DialogBox::pixmap : command.control & DialogCommand::property_animation & DialogCommand::property_mask ? DialogBox::movie : DialogBox::text); break; case DialogCommand::groupbox: AddGroupbox(command.GetTitle(), command.GetName(), command.control & DialogCommand::property_vertical & DialogCommand::property_mask, command.control & DialogCommand::property_checkable & DialogCommand::property_mask, command.control & DialogCommand::property_checked & DialogCommand::property_mask); break; case DialogCommand::frame: AddFrame(command.GetTitle(), command.control & DialogCommand::property_vertical & DialogCommand::property_mask, command.control); break; case DialogCommand::pushbutton: AddPushbutton(command.GetTitle(), command.GetName(), command.control & DialogCommand::property_apply & DialogCommand::property_mask, command.control & DialogCommand::property_exit & DialogCommand::property_mask, command.control & DialogCommand::property_default & DialogCommand::property_mask); break; case DialogCommand::checkbox: AddCheckbox(command.GetTitle(), command.GetName(), command.control & DialogCommand::property_checked & DialogCommand::property_mask); break; case DialogCommand::radiobutton: AddRadiobutton(command.GetTitle(), command.GetName(), command.control & DialogCommand::property_checked & DialogCommand::property_mask); break; case DialogCommand::textbox: AddTextbox(command.GetTitle(), command.GetName(), command.GetText(), command.GetAuxText(), command.control & DialogCommand::property_password & DialogCommand::property_mask); break; case DialogCommand::listbox: AddListbox(command.GetTitle(), command.GetName(), command.control & DialogCommand::property_activation & DialogCommand::property_mask, command.control & DialogCommand::property_selection & DialogCommand::property_mask); break; case DialogCommand::combobox: AddCombobox(command.GetTitle(), command.GetName(), command.control & DialogCommand::property_editable & DialogCommand::property_mask, command.control & DialogCommand::property_selection & DialogCommand::property_mask); break; case DialogCommand::item: AddItem(command.GetTitle(), command.GetName(), command.control & DialogCommand::property_current & DialogCommand::property_mask); break; case DialogCommand::separator: AddSeparator(command.GetTitle(), command.control & DialogCommand::property_vertical & DialogCommand::property_mask, command.control); break; case DialogCommand::progressbar: AddProgressbar(command.GetTitle(), command.control & DialogCommand::property_vertical & DialogCommand::property_mask, command.control & DialogCommand::property_busy & DialogCommand::property_mask); break; case DialogCommand::slider: { int min(0), max(100); if(command.GetName()[0]) sscanf(command.GetName(), "%d", &min); if(command.GetText()[0]) sscanf(command.GetText(), "%d", &max); AddSlider(command.GetTitle(), command.control & DialogCommand::property_vertical & DialogCommand::property_mask, min, max); } break; case DialogCommand::textview: AddTextview(command.GetTitle(), command.GetName()); break; case DialogCommand::tabs: AddTabs(command.GetTitle(), command.control); break; case DialogCommand::page: AddPage(command.GetTitle(), command.GetName(), command.GetText(), command.control & DialogCommand::property_current & DialogCommand::property_mask); break; } break; case DialogCommand::clear: Clear(command.GetName()); break; case DialogCommand::end: switch(command.control & ~DialogCommand::property_mask) { case DialogCommand::groupbox: case DialogCommand::frame: EndGroup(); break; case DialogCommand::listbox: case DialogCommand::combobox: EndList(); break; case DialogCommand::tabs: EndTabs(); break; case DialogCommand::page: EndPage(); break; case DialogCommand::widget_mask: // none type mentioned if(current_view) EndList(); else if(group_layout) EndGroup(); else if(current_tab_widget) { if(current_tab_widget->indexOf(current_layout->parentWidget())==-1) EndTabs(); else EndPage(); } break; } break; case DialogCommand::step: if(command.command & DialogCommand::option_vertical & DialogCommand::option_mask) StepVertical(); else StepHorizontal(); break; case DialogCommand::set: if(command.GetName()[0]) { if(!(widget=FindWidget(command.GetName()))) break; } else widget=this; if(command.command & DialogCommand::option_enabled & DialogCommand::option_mask) SetEnabled(widget, true); if(command.command & DialogCommand::option_focus & DialogCommand::option_mask) { QTimer::singleShot(0, widget, SLOT(setFocus())); // select text for QLineEdit objects // selectedText property is not writable and this must be done in the class specific way if(QWidget* proxywidget=widget->focusProxy()) { switch(WidgetType(proxywidget)) { case DialogCommand::combobox: proxywidget=((QComboBox*)proxywidget)->lineEdit(); break; case DialogCommand::textbox: break; default: proxywidget=NULL; break; } if(proxywidget) ((QLineEdit*)proxywidget)->selectAll(); } } // see http://doc.qt.io/qt-4.8/stylesheet.html for reference if(command.command & DialogCommand::option_stylesheet & DialogCommand::option_mask) { widget->setStyleSheet(command.GetText()); if(QWidget* proxywidget=widget->focusProxy()) proxywidget->setStyleSheet(command.GetText()); } if(command.command & DialogCommand::option_visible & DialogCommand::option_mask) { widget->show(); if(QWidget* proxywidget=widget->focusProxy()) proxywidget->show(); } if(command.control) SetOptions(widget, command.control, command.control, command.GetText()); // setting of some properties (calls like show and hide) generates events which are optimised next // or sets widget attributes which might impact next calls // to avoid races and to ensure the command is executed as expected we process all events that // have been generated: QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents | QEventLoop::ExcludeSocketNotifiers); break; case DialogCommand::unset: if(command.GetName()[0]) { if(!(widget=FindWidget(command.GetName()))) break; } else widget=this; if(command.command & DialogCommand::option_enabled & DialogCommand::option_mask) SetEnabled(widget, false); // see http://doc.qt.io/qt-4.8/stylesheet.html for reference if(command.command & DialogCommand::option_stylesheet & DialogCommand::option_mask) { // rarely it was seen this fails (unset stylesheet or set it to empty string) // hopefully this was caused by the race which is now fixed // (queued signaling between threads and optimisation of queued GUI events) widget->setStyleSheet(QString()); if(QWidget* proxywidget=widget->focusProxy()) proxywidget->setStyleSheet(QString()); } if(command.command & DialogCommand::option_visible & DialogCommand::option_mask) { widget->hide(); if(QWidget* proxywidget=widget->focusProxy()) proxywidget->hide(); } if(command.control) SetOptions(widget, 0, command.control, NULL); // setting of some properties (calls like show and hide) generates events which are optimised next // or sets widget attributes which might impact next calls // to avoid races and to ensure the command is executed as expected we process all events that // have been generated: QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents | QEventLoop::ExcludeSocketNotifiers); break; case DialogCommand::remove: RemoveWidget(command.GetName()); break; case DialogCommand::position: Position(command.GetText(), command.command & DialogCommand::option_behind & DialogCommand::option_mask, command.command & DialogCommand::option_onto & DialogCommand::option_mask); break; case DialogCommand::query: Report(); break; case DialogCommand::noop: default: ; } // clean up after possible FindWidget call chosen_view=NULL; chosen_row_flag=false; }