/* * ---showResult--- * * prints the complex number result in the form: * result.real + result.imag(*)i * to the command line * results are truncated to a maximum of 3 rhs * digits * * @param result the result of a mathematical operation */ void showResult(complexNumber result) { char r_str[30]; char i_str[30]; _snprintf_s(r_str, 30, "%lf", result.real); _snprintf_s(i_str, 30, "%lf", result.imag); truncateString(r_str, MAXFLOATINGPOINT); truncateString(i_str, MAXFLOATINGPOINT); printf_s("%s + %si", r_str, i_str); //printf_s("\n\n%.4g + %.4gi", result.real, result.imag); }
void TrayMenu::changeName(QString name) { RTorrent * torrent = dynamic_cast<RTorrent *>(sender()); if(!torrent) return; torrents[torrent->getId()]->setTitle( truncateString(name,torrents[torrent->getId()]->font(),200) ); }
void TestTruncationOfComplexResultString() { double testTruncate = 6.4502000; char str[15]; sprintf(str, "%lf", testTruncate); assert(str[4] = '0'); truncateString(str, 3); //printf_s("%s\n", str); /*for(int i = 0; i < 15; i++) printf_s("%c", str[i]);*/ //printf_s("%d", str[4]); assert(str[5] = '0'); }
bool Favorite::truncateStrings() { struct stringMaxLength { char* str; size_t max; } stringList[] = {{ m_name, MAX_SIZE_NAME }, { m_shortName, MAX_SIZE_SHORTNAME }, { m_description, MAX_SIZE_DESCRIPTION }, { m_category, MAX_SIZE_CATEGORY }, { m_mapIconName, MAX_SIZE_MAPICONPATH }}; bool truncated = false; for(size_t i = 0; i < sizeof(stringList)/sizeof(*stringList); ++i){ truncated = truncateString(stringList[i].str, stringList[i].max) && truncated; } return truncated; } // Favorite::truncateStrings
QVariant ProfilePrintModel::data(const QModelIndex &index, int role) const { const int row = index.row(); const int col = index.column(); switch (role) { case Qt::DisplayRole: { struct DiveItem di; di.dive = dive; char buf[80]; const QString unknown = tr("unknown"); // dive# + date, depth, location, duration if (row == 0) { if (col == 0) return tr("Dive #%1 - %2").arg(dive->number).arg(di.displayDate()); if (col == 5) { QString unit = (get_units()->length == units::METERS) ? "m" : "ft"; return tr("Max depth: %1 %2").arg(di.displayDepth()).arg(unit); } } if (row == 1) { if (col == 0) return truncateString(dive->location, 32); if (col == 5) return QString(tr("Duration: %1 min")).arg(di.displayDuration()); } // cylinder headings if (row == 2) { if (col == 0) return tr("Cylinder"); if (col == 1) return tr("Gasmix"); if (col == 2) return tr("Gas Used"); } // cylinder data if (row > 2 && row < 10 && row - 3 < MAX_CYLINDERS) { cylinder_t *cyl = &dive->cylinder[row - 3]; if (cyl->type.description) { // how do we check if a cylinder is added? if (col == 0) { if (cyl->type.description[0] != '\0') return QString(cyl->type.description); return unknown; } if (col == 1) { get_gas_string(cyl->gasmix.o2.permille, cyl->gasmix.he.permille, buf, sizeof(buf)); return QString(buf); } if (col == 2) { return get_cylinder_used_gas_string(cyl, true); } } } // dive notes if (row == 10 && col == 0) return truncateString(dive->notes, 640); // sac, cns, otu - headings if (col == 3) { if (row == 2) return tr("SAC"); if (row == 4) return tr("Max. CNS"); if (row == 6) return tr("OTU"); } // sac, cns, otu - data if (col == 4) { if (row == 2) return di.displaySac(); if (row == 4) return QString::number(dive->maxcns); if (row == 6) return QString::number(dive->otu); } // weights heading if (row == 2 && col == 5) return tr("Weights"); // total weight if (row == 9) { weight_t tw = { total_weight(dive) }; if (tw.grams) { if (col == 5) return tr("Total weight"); if (col == 6) return get_weight_string(tw, true); } } // weight data if (row > 2 && row < 10 && row - 3 < MAX_WEIGHTSYSTEMS) { weightsystem_t *ws = &dive->weightsystem[row - 3]; if (ws->weight.grams) { if (col == 5) { if (ws->description && ws->description[0] != '\0') return QString(ws->description); return unknown; } if (col == 6) { return get_weight_string(ws->weight, true); } } } return QString(); } case Qt::FontRole: { QFont font; const int baseSize = 9; // dive # if (row == 0 && col == 0) { font.setBold(true); font.setPixelSize(baseSize + 1); return QVariant::fromValue(font); } // dive location if (row == 1 && col == 0) { font.setPixelSize(baseSize); font.setBold(true); return QVariant::fromValue(font); } // depth/duration if ((row == 0 || row == 1) && col == 5) { font.setPixelSize(baseSize); return QVariant::fromValue(font); } // notes if (row == 9 && col == 0) { font.setPixelSize(baseSize + 1); return QVariant::fromValue(font); } font.setPixelSize(baseSize); return QVariant::fromValue(font); } case Qt::TextAlignmentRole: { unsigned int align = Qt::AlignCenter; // dive #, location, notes if ((row < 2 || row == 10) && col == 0) align = Qt::AlignLeft | Qt::AlignVCenter; // depth, duration if (row < 2 && col == 5) align = Qt::AlignRight | Qt::AlignVCenter; return QVariant::fromValue(align); } } // switch (role) return QVariant(); }
String StringTruncator::rightTruncate(const String& string, float maxWidth, const Font& font, bool disableRoundingHacks) { return truncateString(string, maxWidth, font, rightTruncateToBuffer, disableRoundingHacks); }
/* ** Clean up after the execution of a shell command sub-process and present ** the output/errors to the user as requested in the initial issueCommand ** call. If "terminatedOnError" is true, don't bother trying to read the ** output, just close the i/o descriptors, free the memory, and restore the ** user interface state. */ static void finishCmdExecution(WindowInfo *window, int terminatedOnError) { shellCmdInfo *cmdData = window->shellCmdData; textBuffer *buf; int status, failure, errorReport, reselectStart, outTextLen, errTextLen; int resp, cancel = False, fromMacro = cmdData->fromMacro; char *outText, *errText = NULL; /* Cancel any pending i/o on the file descriptors */ if (cmdData->stdoutInputID != 0) XtRemoveInput(cmdData->stdoutInputID); if (cmdData->stdinInputID != 0) XtRemoveInput(cmdData->stdinInputID); if (cmdData->stderrInputID != 0) XtRemoveInput(cmdData->stderrInputID); /* Close any file descriptors remaining open */ close(cmdData->stdoutFD); if (cmdData->flags & ERROR_DIALOGS) close(cmdData->stderrFD); if (cmdData->inPtr != NULL) close(cmdData->stdinFD); /* Free the provided input text */ if (cmdData->input != NULL) XtFree(cmdData->input); /* Cancel pending timeouts */ if (cmdData->flushTimeoutID != 0) XtRemoveTimeOut(cmdData->flushTimeoutID); if (cmdData->bannerTimeoutID != 0) XtRemoveTimeOut(cmdData->bannerTimeoutID); /* Clean up waiting-for-shell-command-to-complete mode */ if (!cmdData->fromMacro) { EndWait(window->shell); SetSensitive(window, window->cancelShellItem, False); if (cmdData->bannerIsUp) ClearModeMessage(window); } /* If the process was killed or became inaccessable, give up */ if (terminatedOnError) { freeBufList(&cmdData->outBufs); freeBufList(&cmdData->errBufs); waitpid(cmdData->childPid, &status, 0); goto cmdDone; } /* Assemble the output from the process' stderr and stdout streams into null terminated strings, and free the buffer lists used to collect it */ outText = coalesceOutput(&cmdData->outBufs, &outTextLen); if (cmdData->flags & ERROR_DIALOGS) errText = coalesceOutput(&cmdData->errBufs, &errTextLen); /* Wait for the child process to complete and get its return status */ waitpid(cmdData->childPid, &status, 0); /* Present error and stderr-information dialogs. If a command returned error output, or if the process' exit status indicated failure, present the information to the user. */ if (cmdData->flags & ERROR_DIALOGS) { failure = WIFEXITED(status) && WEXITSTATUS(status) != 0; errorReport = *errText != '\0'; if (failure && errorReport) { removeTrailingNewlines(errText); truncateString(errText, DF_MAX_MSG_LENGTH); resp = DialogF(DF_WARN, window->shell, 2, "Warning", "%s", "Cancel", "Proceed", errText); cancel = resp == 1; } else if (failure) { truncateString(outText, DF_MAX_MSG_LENGTH-70); resp = DialogF(DF_WARN, window->shell, 2, "Command Failure", "Command reported failed exit status.\n" "Output from command:\n%s", "Cancel", "Proceed", outText); cancel = resp == 1; } else if (errorReport) { removeTrailingNewlines(errText); truncateString(errText, DF_MAX_MSG_LENGTH); resp = DialogF(DF_INF, window->shell, 2, "Information", "%s", "Proceed", "Cancel", errText); cancel = resp == 2; } XtFree(errText); if (cancel) { XtFree(outText); goto cmdDone; } } /* If output is to a dialog, present the dialog. Otherwise insert the (remaining) output in the text widget as requested, and move the insert point to the end */ if (cmdData->flags & OUTPUT_TO_DIALOG) { removeTrailingNewlines(outText); if (*outText != '\0') createOutputDialog(window->shell, outText); } else if (cmdData->flags & OUTPUT_TO_STRING) { ReturnShellCommandOutput(window,outText, WEXITSTATUS(status)); } else { buf = TextGetBuffer(cmdData->textW); if (!BufSubstituteNullChars(outText, outTextLen, buf)) { fprintf(stderr,"NEdit: Too much binary data in shell cmd output\n"); outText[0] = '\0'; } if (cmdData->flags & REPLACE_SELECTION) { reselectStart = buf->primary.rectangular ? -1 : buf->primary.start; BufReplaceSelected(buf, outText); TextSetCursorPos(cmdData->textW, buf->cursorPosHint); if (reselectStart != -1) BufSelect(buf, reselectStart, reselectStart + strlen(outText)); } else { safeBufReplace(buf, &cmdData->leftPos, &cmdData->rightPos, outText); TextSetCursorPos(cmdData->textW, cmdData->leftPos+strlen(outText)); } } /* If the command requires the file to be reloaded afterward, reload it */ if (cmdData->flags & RELOAD_FILE_AFTER) RevertToSaved(window); /* Command is complete, free data structure and continue macro execution */ XtFree(outText); cmdDone: XtFree((char *)cmdData); window->shellCmdData = NULL; if (fromMacro) ResumeMacroExecution(window); }
String StringTruncator::rightTruncate(const String& string, float maxWidth, const Font& font) { return truncateString(string, maxWidth, font, rightTruncateToBuffer); }
String StringTruncator::centerTruncate(const String& string, float maxWidth, const Font& font, EnableRoundingHacksOrNot enableRoundingHacks) { return truncateString(string, maxWidth, font, centerTruncateToBuffer, !enableRoundingHacks); }
String StringTruncator::rightClipToWord(const String& string, float maxWidth, const FontCascade& font, EnableRoundingHacksOrNot enableRoundingHacks, float& resultWidth, bool shouldInsertEllipsis, float customTruncationElementWidth, bool alwaysTruncate) { return truncateString(string, maxWidth, font, rightClipToWordBuffer, !enableRoundingHacks, &resultWidth, shouldInsertEllipsis, customTruncationElementWidth, alwaysTruncate); }
String StringTruncator::leftTruncate(const String& string, float maxWidth, const FontCascade& font, EnableRoundingHacksOrNot enableRoundingHacks, float& resultWidth, bool shouldInsertEllipsis, float customTruncationElementWidth) { return truncateString(string, maxWidth, font, leftTruncateToBuffer, !enableRoundingHacks, &resultWidth, shouldInsertEllipsis, customTruncationElementWidth); }
String StringTruncator::rightTruncate(const String& string, float maxWidth, const FontCascade& font, EnableRoundingHacksOrNot enableRoundingHacks) { return truncateString(string, maxWidth, font, rightTruncateToBuffer, !enableRoundingHacks); }