bool CSVModelWriter::write() { QFile file(filename); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) return false; QTextStream out(&file); int numRows = 0; if (model) { numRows = model->rowCount(); } for (int i = 0; i < columns.size(); ++i) { if (i != 0) { writeSep(out); } writeValue(out, columns[i].title); } writeNewline(out); for (int j = 0; j < numRows; ++j) { for (int i = 0; i < columns.size(); ++i) { if (i != 0) { writeSep(out); } QVariant data = model->index(j, columns[i].column).data(columns[i].role); writeValue(out, data.toString()); } writeNewline(out); } file.close(); return file.error() == QFile::NoError; }
void profileWrite (GC_state s, GC_profileData p, const char *fileName) { FILE *f; const char* kind; if (DEBUG_PROFILE) fprintf (stderr, "profileWrite("FMTPTR",%s)\n", (uintptr_t)p, fileName); f = fopen_safe (fileName, "wb"); writeString (f, "MLton prof\n"); switch (s->profiling.kind) { case PROFILE_ALLOC: kind = "alloc\n"; break; case PROFILE_COUNT: kind = "count\n"; break; case PROFILE_NONE: die ("impossible PROFILE_NONE"); // break; case PROFILE_TIME_FIELD: kind = "time\n"; break; case PROFILE_TIME_LABEL: kind = "time\n"; break; default: kind = ""; assert (FALSE); } writeString (f, kind); writeString (f, s->profiling.stack ? "stack\n" : "current\n"); writeUint32X (f, s->magic); writeNewline (f); writeUintmaxU (f, p->total); writeString (f, " "); writeUintmaxU (f, p->totalGC); writeNewline (f); writeUint32U (f, s->sourceMaps.sourcesLength); writeNewline (f); for (GC_sourceIndex i = 0; i < s->sourceMaps.sourcesLength; i++) writeProfileCount (s, f, p, (GC_profileMasterIndex)i); writeUint32U (f, s->sourceMaps.sourceNamesLength); writeNewline (f); for (GC_sourceNameIndex i = 0; i < s->sourceMaps.sourceNamesLength; i++) writeProfileCount (s, f, p, (GC_profileMasterIndex)(i + s->sourceMaps.sourcesLength)); fclose_safe (f); }
void LuaTableWriter::prepareNewLine() { if (m_valueWritten) { write(m_valueSeparator); m_valueWritten = false; } writeNewline(); }
void TextOutputStream::writeNewlines(int numLines) { for (int i = 0; i < numLines; ++i) { writeNewline(); } }
void TextWriter::writeNodeKids(const Node* Nd, bool EmbeddedInParent) { // Write out with number of kids specified to be on same line, // with remaining kids on separate (indented) lines. int Type = int(Nd->getType()); int Count = 0; int KidsSameLine = KidCountSameLine[int(Type)]; int NumKids = Nd->getNumKids(); if (NumKids <= MaxKidCountSameLine[int(Type)]) KidsSameLine = MaxKidCountSameLine[int(Type)]; Node* LastKid = Nd->getLastKid(); int HasHiddenSeq = HasHiddenSeqSet.count(Type); bool ForceNewline = false; for (auto* Kid : *Nd) { if (HasHiddenSeq && Kid == LastKid && isa<SequenceNode>(LastKid)) { writeNewline(); writeNode(Kid, true, HasHiddenSeq); return; } ++Count; if (ForceNewline) { writeNode(Kid, true); continue; } if (NeverSameLine.count(Kid->getType())) { if (!(Count == 1 && EmbeddedInParent)) writeNewline(); ForceNewline = true; writeNode(Kid, true); continue; } if (Count < KidsSameLine) { writeSpace(); writeNode(Kid, false); continue; } if (Count == KidsSameLine) { writeSpace(); ForceNewline = Count < NumKids; writeNode(Kid, ForceNewline); continue; } ForceNewline = true; writeNode(Kid, true); } }
void LuaTableWriter::writeEndTable() { --m_indent; if (m_valueWritten) writeNewline(); write('}'); m_newLine = false; m_valueWritten = true; }
void LuaTableWriter::prepareNewValue() { if (!m_valueWritten) { writeNewline(); } else { write(m_valueSeparator); write(' '); } }
void writeProfileCount (GC_state s, FILE *f, GC_profileData p, GC_profileMasterIndex i) { writeUintmaxU (f, p->countTop[i]); if (s->profiling.stack) { GC_profileStack ps; ps = &(p->stack[i]); writeString (f, " "); writeUintmaxU (f, ps->ticks); writeString (f, " "); writeUintmaxU (f, ps->ticksGC); } writeNewline (f); }
void CustomXmlDataWriter::beginOpenTag(const char* tag) { if (tagOpening_) { stream_ << ">"; writeNewline(); } writeIndent(); stream_ << "<" << tag; tagOpening_ = true; hasChildElements_ = false; indent_ += 1; }
void CustomXmlDataWriter::closeTag(const char* tag) { indent_ -= 1; if (tagOpening_) { stream_ << "/>"; tagOpening_ = false; } else { if (hasChildElements_) { writeIndent(); } stream_ << "</" << tag << ">"; } writeNewline(); hasChildElements_ = true; }
void TextOutput::wordWrapIndentAppend(const std::string& str) { // TODO: keep track of the last space character we saw so we don't // have to always search. if ((option.wordWrap == Settings::WRAP_NONE) || (currentColumn + (int)str.size() <= option.numColumns)) { // No word-wrapping is needed // Add one character at a time. // TODO: optimize for strings without newlines to add multiple // characters. for (uint32 i = 0; i < str.size(); ++i) { indentAppend(str[i]); } return; } // Number of columns to wrap against int cols = option.numColumns - indentSpaces; // Copy forward until we exceed the column size, // and then back up and try to insert newlines as needed. for (uint32 i = 0; i < str.size(); ++i) { indentAppend(str[i]); if ((str[i] == '\r') && (i + 1 < str.size()) && (str[i + 1] == '\n')) { // \r\n, we need to hit the \n to enter word wrapping. ++i; indentAppend(str[i]); } if (currentColumn >= cols) { debugAssertM(str[i] != '\n' && str[i] != '\r', "Should never enter word-wrapping on a newline character"); // True when we're allowed to treat a space as a space. bool unquotedSpace = option.allowWordWrapInsideDoubleQuotes || ! inDQuote; // Cases: // // 1. Currently in a series of spaces that ends with a newline // strip all spaces and let the newline // flow through. // // 2. Currently in a series of spaces that does not end with a newline // strip all spaces and replace them with single newline // // 3. Not in a series of spaces // search backwards for a space, then execute case 2. // Index of most recent space uint32 lastSpace = data.size() - 1; // How far back we had to look for a space uint32 k = 0; uint32 maxLookBackward = currentColumn - indentSpaces; // Search backwards (from current character), looking for a space. while ((k < maxLookBackward) && (lastSpace > 0) && (! ((data[lastSpace] == ' ') && unquotedSpace))) { --lastSpace; ++k; if ((data[lastSpace] == '\"') && !option.allowWordWrapInsideDoubleQuotes) { unquotedSpace = ! unquotedSpace; } } if (k == maxLookBackward) { // We couldn't find a series of spaces if (option.wordWrap == Settings::WRAP_ALWAYS) { // Strip the last character we wrote, force a newline, // and replace the last character; data.pop(); writeNewline(); indentAppend(str[i]); } else { // Must be Settings::WRAP_WITHOUT_BREAKING // // Don't write the newline; we'll come back to // the word wrap code after writing another character } } else { // We found a series of spaces. If they continue // to the new string, strip spaces off both. Otherwise // strip spaces from data only and insert a newline. // Find the start of the spaces. firstSpace is the index of the // first non-space, looking backwards from lastSpace. uint32 firstSpace = lastSpace; while ((k < maxLookBackward) && (firstSpace > 0) && (data[firstSpace] == ' ')) { --firstSpace; ++k; } if (k == maxLookBackward) { ++firstSpace; } if (lastSpace == (uint32)data.size() - 1) { // Spaces continued up to the new string data.resize(firstSpace + 1); writeNewline(); // Delete the spaces from the new string while ((i < str.size() - 1) && (str[i + 1] == ' ')) { ++i; } } else { // Spaces were somewhere in the middle of the old string. // replace them with a newline. // Copy over the characters that should be saved Array<char> temp; for (uint32 j = lastSpace + 1; j < (uint32)data.size(); ++j) { char c = data[j]; if (c == '\"') { // Undo changes to quoting (they will be re-done // when we paste these characters back on). inDQuote = !inDQuote; } temp.append(c); } // Remove those characters and replace with a newline. data.resize(firstSpace + 1); writeNewline(); // Write them back for (uint32 j = 0; j < (uint32)temp.size(); ++j) { indentAppend(temp[j]); } // We are now free to continue adding from the // new string, which may or may not begin with spaces. } // if spaces included new string } // if hit indent } // if line exceeded } // iterate over str }