int main (int argc, char const * argv []) { static char const * optv [] = { "counts the lines, words, chars and bytes in a text file", PUTOPTV_S_FUNNEL, "W:lwcbt", "W s\tcharset is (s) [" CHRLIB_WORDSET "]", "l\tdisplay line count", "w\tdisplay word count", "c\tdisplay char count", "b\tdisplay byte count", "t\tdisplay totals", (char const *) (0) }; struct wc total = { 0, 0, 0, 0, "total" }; static char words [UCHAR_MAX + 1]; static char table [UCHAR_MAX + 1]; flag_t flags = (flag_t) (0); signed c; total._name = "total"; memset (table, 0, sizeof (table)); charset (CHRLIB_WORDSET, (char) (0), words, sizeof (words)); chrdef (table, words); while (~ (c = getoptv (argc, argv, optv))) { switch (c) { case 'W': memset (table, 0, sizeof (table)); charset (optarg, (char) (0), words, sizeof (words)); chrdef (table, words); break; case 'l': _setbits (flags, WC_B_NLINE); break; case 'w': _setbits (flags, WC_B_NWORD); break; case 'c': _setbits (flags, WC_B_NCHAR); break; case 'b': _setbits (flags, WC_B_NBYTE); break; case 't': _setbits (flags, WC_B_TOTAL); break; default: break; } } if (_allclr (flags, WC_B_NLINE | WC_B_NWORD | WC_B_NCHAR | WC_B_NBYTE)) { _setbits (flags, WC_B_NLINE | WC_B_NWORD | WC_B_NCHAR | WC_B_NBYTE); } argc -= optind; argv += optind; if (! argc) { function ("stdin", table, & total, flags); } while ((argc) && (* argv)) { if (efreopen (* argv, "rb", stdin)) { function (* argv, table, & total, flags); } argc--; argv++; } show (& total, flags); exit (0); }
bool nuiTextLayout::LayoutParagraph(int32 start, int32 length) { //printf("new paragraph: %d + %d\n", start, length); float spacewidth = 0; float tabwidth = 0; { nuiFontBase* pFont = mStyle.GetFont(); nuiGlyphInfo glyphinfo; uint32 space = pFont->GetGlyphIndex(32); pFont->GetGlyphInfo(glyphinfo, space, nuiFontBase::eGlyphNative); spacewidth = glyphinfo.AdvanceX; tabwidth = 4 * spacewidth; } mpParagraphs.push_back(new Paragraph()); nuiTextLine* pLine = new nuiTextLine(*this, 0, 0); mpParagraphs.back()->push_back(pLine); // Split the paragraph into ranges: nuiTextRangeList ranges; nuiSplitText(mUnicode, ranges, nuiST_ScriptChange, start, length, &Split); { nuiTextRangeList::iterator it = ranges.begin(); nuiTextRangeList::iterator end = ranges.end(); int32 origin = start; int32 i = 0; while (it != end) { const nuiTextRange& range(*it); int32 len = range.mLength; int32 pos = origin; //printf("\trange %d (%d - %d) (%s - %s)\n", i, pos, len, nuiGetUnicodeScriptName(range.mScript).GetChars(), nuiGetUnicodeRangeName(range.mRange).GetChars()); std::set<nglUChar>& charset(mCharsets[range.mScript]); { while (pos < origin + len) { nglUChar ch = mUnicode[pos++]; if (ucisprint(ch) && ch > 32) charset.insert(ch); } } origin += len; ++it; i++; } } { auto styleit = mStyleChanges.begin(); auto styleend = mStyleChanges.end(); nuiTextStyle style = mStyle; nuiTextRangeList::iterator it = ranges.begin(); nuiTextRangeList::iterator end = ranges.end(); uint32 i = 0; uint32 pos = start; int32 oldp = -1; while (it != end) { const nuiTextRange& range(*it); uint32 len = range.mLength; //printf("\trange %d (%d - %d) (%s - %s)\n", i, pos, len, nuiGetUnicodeScriptName(range.mScript).GetChars(), nuiGetUnicodeRangeName(range.mRange).GetChars()); nglUChar ch = mUnicode[pos]; nuiTextRun* pRun = NULL; while (len > 0 && styleit != styleend) { if (ch < 32) { int32 tabs = 0; int32 spaces = 0; for (uint32 i = pos; i < pos + len && mUnicode[i] < 32; i++) { if (mUnicode[i] == 9) tabs++; else spaces++; } pRun = new nuiTextRun(*this, pos, len, spacewidth * (float)spaces + tabwidth * (float)tabs, 0.0f); if (styleit != styleend) { int32 p = styleit->first; NGL_ASSERT(oldp <= p); oldp = p; if ((p >= pos) && (p < pos + len)) { style = styleit->second; ++styleit; } } pos += len; len = 0; } else { if (styleit != styleend) { int32 p = styleit->first; NGL_ASSERT(oldp <= p); oldp = p; if ((p >= pos) && (p < pos + len)) { // Change the style now: int32 newpos = styleit->first; int32 s = newpos - pos; if (s > 0) { //printf ("Cutting run to change style (%d - %d)\n", pos, s); pRun = new nuiTextRun(*this, range.mScript, pos, s, style); pLine->AddRun(pRun); pRun = NULL; } style = styleit->second; pos += s; len -= s; ++styleit; } else break; } } } if (len > 0) { pRun = new nuiTextRun(*this, range.mScript, pos, len, style); pos += len; len = 0; } if (pRun) pLine->AddRun(pRun); ++i; ++it; } } return true; }
// ***************************************************************************** // Main int main(int argc, char* const argv[]) try { if (argc != 2) { std::cout << "Usage: " << argv[0] << " file\n"; return 1; } Exiv2::ExifData exifData; int rc = exifData.read(argv[1]); if (rc) { std::string error = Exiv2::ExifData::strError(rc, argv[1]); throw Exiv2::Error(error); } /* There are two pitfalls that we need to consider when setting the Exif user comment (Exif.Photo.UserComment) of an image: First, the type of the Exif user comment tag is "undefined" (and not ASCII) according to the Exif standard. This means that in Exiv2, we have to deal with a DataValue (and not an AsciiValue). DataValue has the usual two read methods, however, the one taking a const std::string& buf expects the string to contain a series of integers (e.g., "0 1 2") and not a text string. Thus, we need to use the read function that reads the value from a character buffer of a given length. Second, the Exif comment field starts with an eight character area that identifies the used character set. For ASCII, these eight characters are "ASCII\0\0\0". The actual comment follows after this code. Note: There is a more simple Exif tag for the title of an image. It is a 20 byte string (type ASCII) and does not store two-byte characters. (Image.OtherTags.ImageDescription) */ // Initialise a data value with the character set and comment std::string charset("ASCII\0\0\0", 8); std::string comment("A comment added to the Exif metadata through Exiv2"); Exiv2::DataValue value; value.read(reinterpret_cast<const Exiv2::byte*>((charset + comment).data()), 8 + static_cast<long>(comment.size())); // Set the Exif comment Exiv2::ExifKey key("Exif.Photo.UserComment"); Exiv2::ExifData::iterator pos = exifData.findKey(key); if (pos != exifData.end()) { // Use the existing Exif UserComment metadatum if there is one pos->setValue(&value); } else { // Otherwise add a new UserComment metadatum exifData.add(key, &value); pos = exifData.findKey(key); } // Now we should have a valid iterator in any case. We use the metadatum // output operator to print the formatted value std::cout << "Writing user comment '" << *pos << "' back to the image\n"; rc = exifData.write(argv[1]); if (rc) { std::string error = Exiv2::ExifData::strError(rc, argv[1]); throw Exiv2::Error(error); } return rc; } catch (Exiv2::Error& e) { std::cout << "Caught Exiv2 exception '" << e << "'\n"; return -1; }
void parameter::parse(const parsingContext& ctx, const std::vector <valueChunk>& chunks) { bool foundCharsetChunk = false; charset ch(charsets::US_ASCII); std::ostringstream value; value.imbue(std::locale::classic()); for (std::vector <valueChunk>::size_type i = 0 ; i < chunks.size() ; ++i) { const valueChunk& chunk = chunks[i]; // Decode following data if (chunk.encoded) { const string::size_type len = chunk.data.length(); string::size_type pos = 0; // If this is the first encoded chunk, extract charset // and language information if (!foundCharsetChunk) { // Eg. "us-ascii'en'This%20is%20even%20more%20" string::size_type q = chunk.data.find_first_of('\''); if (q != string::npos) { const string chs = chunk.data.substr(0, q); if (!chs.empty()) ch = charset(chs); ++q; pos = q; } q = chunk.data.find_first_of('\'', pos); if (q != string::npos) { // Ignore language ++q; pos = q; } foundCharsetChunk = true; } for (string::size_type i = pos ; i < len ; ++i) { const string::value_type c = chunk.data[i]; if (c == '%' && i + 2 < len) { unsigned int v = 0; // First char switch (chunk.data[i + 1]) { case 'a': case 'A': v += 10; break; case 'b': case 'B': v += 11; break; case 'c': case 'C': v += 12; break; case 'd': case 'D': v += 13; break; case 'e': case 'E': v += 14; break; case 'f': case 'F': v += 15; break; default: // assume 0-9 v += (chunk.data[i + 1] - '0'); break; } v *= 16; // Second char switch (chunk.data[i + 2]) { case 'a': case 'A': v += 10; break; case 'b': case 'B': v += 11; break; case 'c': case 'C': v += 12; break; case 'd': case 'D': v += 13; break; case 'e': case 'E': v += 14; break; case 'f': case 'F': v += 15; break; default: // assume 0-9 v += (chunk.data[i + 2] - '0'); break; } value << static_cast <string::value_type>(v); i += 2; // skip next 2 chars } else { value << c; } } } // Simply copy data, as it is not encoded else { // This syntax is non-standard (expressly prohibited // by RFC-2047), but is used by Mozilla: // // Content-Type: image/png; // name="=?us-ascii?Q?Logo_VMime=2Epng?=" // Using 'vmime::text' to parse the data is safe even // if the data is not encoded, because it can recover // from parsing errors. vmime::text t; t.parse(ctx, chunk.data); if (t.getWordCount() != 0) { value << t.getWholeBuffer(); if (!foundCharsetChunk) { // This is still wrong. Each word can have it's own charset, and can // be mixed (eg. iso-8859-1 and iso-2022-jp), but very unlikely. Real // fix is to have parameters store a vmime::text instead of a // vmime::word in m_value. But that changes the interface. for (size_t i = 0 ; i < t.getWordCount() ; ++i) { if (t.getWordAt(i)->getCharset() != ch && ch == charsets::US_ASCII) { ch = t.getWordAt(i)->getCharset(); break; } } } } } } m_value->setBuffer(value.str()); m_value->setCharset(ch); }
static void write_normal(const char *buf, int size) { if (size == 0) return; charset_func charset = console->charset == 0? console->g0_charset: console->g1_charset; WCHAR data[1024]; int len = 0, displen = 0; int i = 0; while (i < size) { if (console->at_right_margin && console->wraparound_mode) crnl(); /* Write to line end at most */ int line_remain = min(size - i, console->width - console->x); len = 0; displen = 0; int seqlen = -1; while (displen < line_remain && i < size) { console->utf8_buf[console->utf8_buf_size++] = buf[i++]; if (console->utf8_buf_size == 1) seqlen = utf8_get_sequence_len(console->utf8_buf[0]); if (seqlen < 0) console->utf8_buf_size = 0; if (seqlen == console->utf8_buf_size) { uint32_t codepoint = utf8_decode(console->utf8_buf); if (codepoint >= 0 && codepoint <= 0x10FFFF) { /* TODO: Handle non BMP characters (not supported by conhost) */ int l = wcwidth(codepoint); if (l > 0) { if (displen + l > line_remain && console->wraparound_mode) { i--; console->utf8_buf_size--; break; } displen += l; data[len++] = charset(codepoint); } } console->utf8_buf_size = 0; } } if (console->insert_mode && console->x + displen < console->width) scroll(console->x, console->width - 1, console->y, console->y, displen, 0); DWORD chars_written; WriteConsoleW(console->out, data, len, &chars_written, NULL); console->x += displen; if (console->x == console->width) { console->x--; console->at_right_margin = 1; } } }
nsresult txMozillaXMLOutput::createResultDocument(const nsSubstring& aName, PRInt32 aNsID, nsIDOMDocument* aSourceDocument, nsIDOMDocument* aResultDocument) { nsresult rv; if (!aResultDocument) { // Create the document if (mOutputFormat.mMethod == eHTMLOutput) { rv = NS_NewHTMLDocument(getter_AddRefs(mDocument)); NS_ENSURE_SUCCESS(rv, rv); } else { // We should check the root name/namespace here and create the // appropriate document rv = NS_NewXMLDocument(getter_AddRefs(mDocument)); NS_ENSURE_SUCCESS(rv, rv); } // This should really be handled by nsIDocument::BeginLoad mDocument->SetReadyStateInternal(nsIDocument::READYSTATE_LOADING); nsCOMPtr<nsIDocument> source = do_QueryInterface(aSourceDocument); NS_ENSURE_STATE(source); PRBool hasHadScriptObject = PR_FALSE; nsIScriptGlobalObject* sgo = source->GetScriptHandlingObject(hasHadScriptObject); NS_ENSURE_STATE(sgo || !hasHadScriptObject); mDocument->SetScriptHandlingObject(sgo); } else { mDocument = do_QueryInterface(aResultDocument); } mCurrentNode = mDocument; mNodeInfoManager = mDocument->NodeInfoManager(); // Reset and set up the document URIUtils::ResetWithSource(mDocument, aSourceDocument); // Set the charset if (!mOutputFormat.mEncoding.IsEmpty()) { NS_LossyConvertUTF16toASCII charset(mOutputFormat.mEncoding); nsCAutoString canonicalCharset; nsCOMPtr<nsICharsetAlias> calias = do_GetService("@mozilla.org/intl/charsetalias;1"); if (calias && NS_SUCCEEDED(calias->GetPreferred(charset, canonicalCharset))) { mDocument->SetDocumentCharacterSetSource(kCharsetFromOtherComponent); mDocument->SetDocumentCharacterSet(canonicalCharset); } } // Set the mime-type if (!mOutputFormat.mMediaType.IsEmpty()) { mDocument->SetContentType(mOutputFormat.mMediaType); } else if (mOutputFormat.mMethod == eHTMLOutput) { mDocument->SetContentType(NS_LITERAL_STRING("text/html")); } else { mDocument->SetContentType(NS_LITERAL_STRING("application/xml")); } if (mOutputFormat.mMethod == eXMLOutput && mOutputFormat.mOmitXMLDeclaration != eTrue) { PRInt32 standalone; if (mOutputFormat.mStandalone == eNotSet) { standalone = -1; } else if (mOutputFormat.mStandalone == eFalse) { standalone = 0; } else { standalone = 1; } // Could use mOutputFormat.mVersion.get() when we support // versions > 1.0. static const PRUnichar kOneDotZero[] = { '1', '.', '0', '\0' }; mDocument->SetXMLDeclaration(kOneDotZero, mOutputFormat.mEncoding.get(), standalone); } // Set up script loader of the result document. nsScriptLoader *loader = mDocument->ScriptLoader(); if (mNotifier) { loader->AddObserver(mNotifier); } else { // Don't load scripts, we can't notify the caller when they're loaded. loader->SetEnabled(PR_FALSE); } if (mNotifier) { rv = mNotifier->SetOutputDocument(mDocument); NS_ENSURE_SUCCESS(rv, rv); } // Do this after calling OnDocumentCreated to ensure that the // PresShell/PresContext has been hooked up and get notified. nsCOMPtr<nsIHTMLDocument> htmlDoc = do_QueryInterface(mDocument); if (htmlDoc) { htmlDoc->SetCompatibilityMode(eCompatibility_FullStandards); } // Add a doc-type if requested if (!mOutputFormat.mSystemId.IsEmpty()) { nsAutoString qName; if (mOutputFormat.mMethod == eHTMLOutput) { qName.AssignLiteral("html"); } else { qName.Assign(aName); } nsCOMPtr<nsIDOMDocumentType> documentType; nsresult rv = nsContentUtils::CheckQName(qName); if (NS_SUCCEEDED(rv)) { nsCOMPtr<nsIAtom> doctypeName = do_GetAtom(qName); if (!doctypeName) { return NS_ERROR_OUT_OF_MEMORY; } // Indicate that there is no internal subset (not just an empty one) nsAutoString voidString; voidString.SetIsVoid(PR_TRUE); rv = NS_NewDOMDocumentType(getter_AddRefs(documentType), mNodeInfoManager, nsnull, doctypeName, nsnull, nsnull, mOutputFormat.mPublicId, mOutputFormat.mSystemId, voidString); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIContent> docType = do_QueryInterface(documentType); rv = mDocument->AppendChildTo(docType, PR_TRUE); NS_ENSURE_SUCCESS(rv, rv); } } return NS_OK; }
static qse_rex_node_t* comp_atom (comp_t* com) { qse_rex_node_t* atom; if (!IS_ESC(com)) { switch (com->c.value) { case QSE_T('('): atom = comp_group (com); if (atom == QSE_NULL) return QSE_NULL; break; case QSE_T('.'): atom = newnode (com, QSE_REX_NODE_ANY); if (atom == QSE_NULL) return QSE_NULL; if (getc_esc(com) <= -1) return QSE_NULL; break; case QSE_T('^'): atom = newnode (com, QSE_REX_NODE_BOL); if (atom == QSE_NULL) return QSE_NULL; if (getc_esc(com) <= -1) return QSE_NULL; break; case QSE_T('$'): atom = newnode (com, QSE_REX_NODE_EOL); if (atom == QSE_NULL) return QSE_NULL; if (getc_esc(com) <= -1) return QSE_NULL; break; case QSE_T('['): atom = newnode (com, QSE_REX_NODE_CSET); if (atom == QSE_NULL) return QSE_NULL; if (getc_esc(com) <= -1) return QSE_NULL; if (charset(com, atom) <= -1) return QSE_NULL; break; default: if (com->rex->option & QSE_REX_STRICT) { /* check if a special charcter is at the * position that requires a normal character. */ switch (com->c.value) { case QSE_T('{'): /* { is a normal charcter when bound is disabled */ if (com->rex->option & QSE_REX_NOBOUND) break; case QSE_T(')'): case QSE_T('?'): case QSE_T('*'): case QSE_T('+'): /* it's at the wrong postion */ com->rex->errnum = QSE_REX_ESPCAWP; return QSE_NULL; } } goto normal_char; } } else { normal_char: /* normal character */ atom = newcharnode (com, com->c.value); if (atom == QSE_NULL) return QSE_NULL; if (getc_esc(com) <= -1) return QSE_NULL; } atom->occ.min = 1; atom->occ.max = 1; if (!IS_ESC(com)) { /* handle the occurrence specifier, if any */ if (comp_occ (com, atom) == QSE_NULL) return QSE_NULL; } return atom; }
void txMozillaTextOutput::createResultDocument(nsIDOMDocument* aSourceDocument, nsIDOMDocument* aResultDocument) { nsresult rv = NS_OK; /* * Create an XHTML document to hold the text. * * <html> * <head /> * <body> * <pre> * The text comes here * </pre> * <body> * </html> * * Except if we are transforming into a non-displayed document we create * the following DOM * * <transformiix:result> * The text comes here * </transformiix:result> */ nsCOMPtr<nsIDocument> doc; if (!aResultDocument) { // Create the document doc = do_CreateInstance(kXMLDocumentCID, &rv); NS_ASSERTION(NS_SUCCEEDED(rv), "Couldn't create document"); nsCOMPtr<nsIDocument_MOZILLA_1_8_BRANCH3> source = do_QueryInterface(aSourceDocument); if (!source) { return; } PRBool hasHadScriptObject = PR_FALSE; nsIScriptGlobalObject* sgo = source->GetScriptHandlingObject(hasHadScriptObject); if (!sgo && hasHadScriptObject) { return; } nsCOMPtr<nsIDocument_MOZILLA_1_8_BRANCH3> doc18 = do_QueryInterface(doc); if (!doc18) { return; } doc18->SetScriptHandlingObject(sgo); mDocument = do_QueryInterface(doc); } else { mDocument = aResultDocument; doc = do_QueryInterface(aResultDocument); NS_ASSERTION(doc, "Couldn't QI to nsIDocument"); } if (!doc) { return; } NS_ASSERTION(mDocument, "Need document"); nsCOMPtr<nsIDOMNSDocument> nsDoc = do_QueryInterface(mDocument); if (nsDoc) { nsDoc->SetTitle(EmptyString()); } // Reset and set up document nsCOMPtr<nsIDocument> sourceDoc = do_QueryInterface(aSourceDocument); nsIPrincipal* sourcePrincipal = sourceDoc->GetPrincipal(); if (!sourcePrincipal) { return; } nsCOMPtr<nsILoadGroup> loadGroup = sourceDoc->GetDocumentLoadGroup(); nsCOMPtr<nsIChannel> channel = sourceDoc->GetChannel(); if (!channel) { // Need to synthesize one if (NS_FAILED(NS_NewChannel(getter_AddRefs(channel), sourceDoc->GetDocumentURI(), nsnull, loadGroup))) { return; } channel->SetOwner(sourcePrincipal); } // Copy the channel and loadgroup from the source document. doc->Reset(channel, loadGroup); doc->SetPrincipal(sourcePrincipal); doc->SetBaseURI(sourceDoc->GetBaseURI()); // Set the charset if (!mOutputFormat.mEncoding.IsEmpty()) { NS_LossyConvertUTF16toASCII charset(mOutputFormat.mEncoding); nsCAutoString canonicalCharset; nsCOMPtr<nsICharsetAlias> calias = do_GetService("@mozilla.org/intl/charsetalias;1"); if (calias && NS_SUCCEEDED(calias->GetPreferred(charset, canonicalCharset))) { doc->SetDocumentCharacterSet(canonicalCharset); doc->SetDocumentCharacterSetSource(kCharsetFromOtherComponent); } } else { doc->SetDocumentCharacterSet(sourceDoc->GetDocumentCharacterSet()); doc->SetDocumentCharacterSetSource( sourceDoc->GetDocumentCharacterSetSource()); } // Notify the contentsink that the document is created nsCOMPtr<nsITransformObserver> observer = do_QueryReferent(mObserver); if (observer) { observer->OnDocumentCreated(mDocument); } // Create the content // When transforming into a non-displayed document (i.e. when there is no // observer) we only create a transformiix:result root element. // Don't do this when called through nsIXSLTProcessorObsolete (i.e. when // aResultDocument is set) for compability reasons nsCOMPtr<nsIDOMNode> textContainer; if (!aResultDocument && !observer) { nsCOMPtr<nsIDOMElement> docElement; mDocument->CreateElementNS(NS_LITERAL_STRING(kTXNameSpaceURI), NS_LITERAL_STRING(kTXWrapper), getter_AddRefs(docElement)); NS_ASSERTION(docElement, "Failed to create wrapper element"); if (!docElement) { return; } rv = mDocument->AppendChild(docElement, getter_AddRefs(textContainer)); NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to append the wrapper element"); if (NS_FAILED(rv)) { return; } } else { nsCOMPtr<nsIDOMElement> element, docElement; nsCOMPtr<nsIDOMNode> parent, pre; NS_NAMED_LITERAL_STRING(XHTML_NSURI, "http://www.w3.org/1999/xhtml"); mDocument->CreateElementNS(XHTML_NSURI, NS_LITERAL_STRING("html"), getter_AddRefs(docElement)); nsCOMPtr<nsIContent> rootContent = do_QueryInterface(docElement); NS_ASSERTION(rootContent, "Need root element"); if (!rootContent) { return; } // XXXbz what to do on failure here? rv = doc->SetRootContent(rootContent); if (NS_FAILED(rv)) { NS_ERROR("Failed to set root content"); return; } mDocument->CreateElementNS(XHTML_NSURI, NS_LITERAL_STRING("head"), getter_AddRefs(element)); NS_ASSERTION(element, "Failed to create head element"); if (!element) { return; } rv = docElement->AppendChild(element, getter_AddRefs(parent)); NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to append the head element"); if (NS_FAILED(rv)) { return; } mDocument->CreateElementNS(XHTML_NSURI, NS_LITERAL_STRING("body"), getter_AddRefs(element)); NS_ASSERTION(element, "Failed to create body element"); if (!element) { return; } rv = docElement->AppendChild(element, getter_AddRefs(parent)); NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to append the body element"); if (NS_FAILED(rv)) { return; } mDocument->CreateElementNS(XHTML_NSURI, NS_LITERAL_STRING("pre"), getter_AddRefs(element)); NS_ASSERTION(element, "Failed to create pre element"); if (!element) { return; } rv = parent->AppendChild(element, getter_AddRefs(pre)); NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to append the pre element"); if (NS_FAILED(rv)) { return; } nsCOMPtr<nsIDOMHTMLElement> htmlElement = do_QueryInterface(pre); htmlElement->SetId(NS_LITERAL_STRING("transformiixResult")); NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to append the id"); textContainer = pre; } nsCOMPtr<nsIDOMText> textNode; mDocument->CreateTextNode(EmptyString(), getter_AddRefs(textNode)); NS_ASSERTION(textNode, "Failed to create the text node"); if (!textNode) { return; } nsCOMPtr<nsIDOMNode> dummy; rv = textContainer->AppendChild(textNode, getter_AddRefs(dummy)); NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to append the text node"); if (NS_FAILED(rv)) { return; } mTextNode = textNode; }
std::string digidoc::util::String::convertUTF8(const std::string& str_in, bool to_UTF) { #if defined(__APPLE__) return str_in; #else std::string charset("ISO8859-1"); const char *env_lang = getenv("LANG"); if(env_lang && strcmp(env_lang, "C") != 0) { charset = env_lang; size_t locale_start = charset.rfind("."); if(locale_start != std::string::npos) charset = charset.substr(locale_start+1); } // no conversion needed for UTF-8 if((charset.compare("UTF-8") == 0) || (charset.compare("utf-8") == 0)) return str_in; iconv_t ic_descr = iconv_t(-1); try { ic_descr = to_UTF ? iconv_open("UTF-8", charset.c_str()) : iconv_open(charset.c_str(), "UTF-8"); } catch(std::exception &) {} if( ic_descr == iconv_t(-1)) { INFO("Could not get the iconv descriptor for converting to/from charset %s, " "continuing without conversion.", charset.c_str()); return str_in; } ICONV_CONST char* inptr = (ICONV_CONST char*)str_in.c_str(); size_t inleft = str_in.size(); std::string out; char outbuf[64]; char* outptr; size_t outleft; while(inleft > 0) { outbuf[0] = '\0'; outptr = (char *)outbuf; outleft = sizeof(outbuf) - sizeof(outbuf[0]); size_t result = iconv(ic_descr, &inptr, &inleft, &outptr, &outleft); if(result == size_t(-1)) { switch(errno) { case E2BIG: break; case EILSEQ: case EINVAL: default: iconv_close(ic_descr); return str_in; break; } } *outptr = '\0'; out += outbuf; } iconv_close(ic_descr); return out; #endif }
nsresult nsXMLDocument::StartDocumentLoad(const char* aCommand, nsIChannel* aChannel, nsILoadGroup* aLoadGroup, nsISupports* aContainer, nsIStreamListener **aDocListener, bool aReset, nsIContentSink* aSink) { nsresult rv = nsDocument::StartDocumentLoad(aCommand, aChannel, aLoadGroup, aContainer, aDocListener, aReset, aSink); if (NS_FAILED(rv)) return rv; if (nsCRT::strcmp("loadAsInteractiveData", aCommand) == 0) { mLoadedAsInteractiveData = true; aCommand = kLoadAsData; // XBL, for example, needs scripts and styles } int32_t charsetSource = kCharsetFromDocTypeDefault; nsAutoCString charset(NS_LITERAL_CSTRING("UTF-8")); TryChannelCharset(aChannel, charsetSource, charset, nullptr); nsCOMPtr<nsIURI> aUrl; rv = aChannel->GetURI(getter_AddRefs(aUrl)); if (NS_FAILED(rv)) return rv; static NS_DEFINE_CID(kCParserCID, NS_PARSER_CID); mParser = do_CreateInstance(kCParserCID, &rv); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIXMLContentSink> sink; if (aSink) { sink = do_QueryInterface(aSink); } else { nsCOMPtr<nsIDocShell> docShell; if (aContainer) { docShell = do_QueryInterface(aContainer); NS_ENSURE_TRUE(docShell, NS_ERROR_FAILURE); } rv = NS_NewXMLContentSink(getter_AddRefs(sink), this, aUrl, docShell, aChannel); NS_ENSURE_SUCCESS(rv, rv); } // Set the parser as the stream listener for the document loader... rv = CallQueryInterface(mParser, aDocListener); NS_ENSURE_SUCCESS(rv, rv); NS_ASSERTION(mChannel, "How can we not have a channel here?"); mChannelIsPending = true; SetDocumentCharacterSet(charset); mParser->SetDocumentCharset(charset, charsetSource); mParser->SetCommand(aCommand); mParser->SetContentSink(sink); mParser->Parse(aUrl, nullptr, (void *)this); return NS_OK; }
void define_charset () { charset (0, 255, 0); charset (0xf0, 0xf7, UF0); charset (0xe0, 0xef, UE0); charset (0xc0, 0xdf, UC0); charset (0x80, 0xbf, U80); charset (0x21, 0x7e, U00); charset ("0123456789abcdefABCDEF", HEX); charset ('U', EUU); charset ('u', ELU); charset ('\t', WS); charset (' ', WS); charset ('\r', LF); charset ('\n', LF); charset ('\\', QESC); charset ('\'', QS); charset ('"', QQ); }
void mailbox::generate(utility::outputStream& os, const string::size_type maxLineLength, const string::size_type curLinePos, string::size_type* newLinePos) const { if (m_name.isEmpty()) { bool newLine = false; // No display name is specified, only email address. if (curLinePos /* + 2 */ + m_email.length() > maxLineLength) { os << NEW_LINE_SEQUENCE; newLine = true; } //os << "<" << m_email << ">"; os << m_email; if (newLinePos) { *newLinePos = curLinePos + m_email.length() /* + 2 */; if (newLine) *newLinePos += 1; } } else { // We have to encode the name: // - if it contains characters in a charset different from "US-ASCII", // - and/or if it contains one or more of these special chars: // SPACE TAB " ; , < > ( ) @ / ? . = : // Check whether there are words that are not "US-ASCII" // and/or contain the special chars. bool forceEncode = false; for (int w = 0 ; !forceEncode && w != m_name.getWordCount() ; ++w) { if (m_name.getWordAt(w)->getCharset() == charset(charsets::US_ASCII)) { const string& buffer = m_name.getWordAt(w)->getBuffer(); for (string::const_iterator c = buffer.begin() ; !forceEncode && c != buffer.end() ; ++c) { switch (*c) { case ' ': case '\t': case ';': case ',': case '<': case '>': case '(': case ')': case '@': case '/': case '?': case '.': case '=': case ':': case '"': forceEncode = true; break; } } } else { forceEncode = true; } } string::size_type pos = curLinePos; bool newLine = true; m_name.encodeAndFold(os, maxLineLength, pos, &pos, forceEncode ? text::FORCE_ENCODING : 0); if (pos + m_email.length() + 3 > maxLineLength) { os << NEW_LINE_SEQUENCE; newLine = true; } os << " <" << m_email << ">"; if (newLinePos) { *newLinePos = pos + m_email.length() + 3; if (newLine) *newLinePos += NEW_LINE_SEQUENCE.length(); } } }
charselect::charselect(char cslots, vector<maplechar>* chars) { app.getimgcache()->clearcache(ict_login); app.getimgcache()->setmode(ict_login); nl::nx::view_file("UI"); node login = nl::nx::nodes["UI"].resolve("Login.img"); node charsel = login.resolve("CharSelect"); sprites.push_back(sprite(animation(login.resolve("Title/worldsel")), vector2d())); sprites.push_back(sprite(animation(login.resolve("Common/frame")), vector2d(400, 290))); sprites.push_back(sprite(animation(login.resolve("Common/selectWorld")), vector2d(580, 42))); sprites.push_back(sprite(animation(charsel.resolve("selectedWorld/icon/15")), vector2d(580, 42))); sprites.push_back(sprite(animation(charsel.resolve("selectedWorld/name/15")), vector2d(580, 42))); sprites.push_back(sprite(animation(charsel.resolve("selectedWorld/ch/" + to_string(app.getui()->getfield()->getchannel()))), vector2d(580, 42))); sprites.push_back(sprite(animation(charsel.resolve("charInfo")), vector2d(662, 355))); buttons.insert(make_pair(BT_ARBEIT, button(charsel.resolve("arbeit"), 580, 115))); buttons.insert(make_pair(BT_CHARCARD, button(charsel.resolve("characterCard"), 665, 115))); buttons.insert(make_pair(BT_NEWCHAR, button(charsel.resolve("BtNew"), 200, 495))); buttons.insert(make_pair(BT_DELCHAR, button(charsel.resolve("BtDelete"), 320, 495))); buttons.insert(make_pair(BT_SELCHAR, button(charsel.resolve("BtSelect"), 586, 427))); for (char i = chars->size(); i < cslots; i++) { sprites.push_back(sprite(animation(charsel.resolve("buyCharacter")), vector2d(130 + (120 * (i % 4)), 250 + (200 * (i > 3))))); } node nametagnode = charsel["nameTag"]; pair<vector<texture>, vector<texture>> nttextures; nttextures.first.push_back(texture(nametagnode.resolve("0/0"))); nttextures.first.push_back(texture(nametagnode.resolve("0/1"))); nttextures.first.push_back(texture(nametagnode.resolve("0/2"))); nttextures.second.push_back(texture(nametagnode.resolve("1/0"))); nttextures.second.push_back(texture(nametagnode.resolve("1/1"))); nttextures.second.push_back(texture(nametagnode.resolve("1/2"))); for (char i = 0; i < chars->size(); i++) { nametag charname = nametag(app.getfonts()->getfont(DWF_CENTER), TXC_WHITE, nttextures, chars->at(i).getstats()->name, vector2d(55 + (120 * (i % 4)), 250 + (200 * (i > 3))), (i == 0)); nametags.push_back(charname); buttons.insert(make_pair(BT_CHAR0 + i, button(105 + (120 * (i % 4)), 170 + (200 * (i > 3)), 50, 80))); buttons[BT_CHAR0].setstate("pressed"); } app.getimgcache()->unlock(); app.getimgcache()->setmode(ict_sys); lvset = charset(charsel.resolve("lv")); statset = charset(nl::nx::nodes["UI"].resolve("StatusBar2.img/mainBar/gauge/number")); nl::nx::unview_file("UI"); nl::nx::view_file("Character"); for (char i = 0; i < chars->size(); i++) { maplelook* plook = chars->at(i).getlook(); app.getlookfactory()->loadcharlook(plook); maplelook look = chars->at(i).copylook(); look.setposition(vector2d(130 + (120 * (i % 4)), 250 + (200 * (i > 3)))); look.setfleft(false); if (i == 0) { look.setstate("walk1"); selected = i; } looks.push_back(look); stats.push_back(chars->at(i).getstats()); } nl::nx::unview_file("Character"); app.getimgcache()->unlock(); position = vector2d(0, 0); dimensions = vector2d(800, 600); active = true; visible = true; }
WT_Result WT_Font::serialize(WT_File & file) const { WD_CHECK (file.dump_delayed_drawable()); file.desired_rendition().blockref(); WD_CHECK(file.desired_rendition().sync(file, WT_Rendition::BlockRef_Bit)); file.desired_rendition().font_extension(); WD_CHECK(file.desired_rendition().sync(file, WT_Rendition::Font_Extension_Bit)); // BPM: this could be improved when time allows... if(file.heuristics().apply_transform() && !m_rotation_checked && file.heuristics().transform().rotation()) { // The first time we start up the file, if the application wants an unrotated // font, and we need to flip landscape to portrait, then the above test will think // that the rotation doesn't need to be output the first time. Here we force it... ((WT_Font &)(*this)).m_fields_defined |= FONT_ROTATION_BIT; ((WT_Font *)this)->m_rotation_checked = WD_True; // Cast from const to alterable. } if (file.heuristics().allow_binary_data()) { // Binary WD_CHECK (file.write((WT_Byte) 0x06)); // CTRL-F WD_CHECK (file.write(m_fields_defined)); } else { // ASCII Output WD_CHECK (file.write_tab_level()); WD_CHECK (file.write("(Font")); } if (m_fields_defined & FONT_NAME_BIT) WD_CHECK (font_name().serialize(*this, file)); if (m_fields_defined & FONT_CHARSET_BIT) WD_CHECK (charset().serialize(*this, file)); if (m_fields_defined & FONT_PITCH_BIT) WD_CHECK (pitch().serialize(*this, file)); if (m_fields_defined & FONT_FAMILY_BIT) WD_CHECK (family().serialize(*this, file)); if (m_fields_defined & (FONT_STYLE_BIT)) WD_CHECK (style().serialize(*this, file)); if (m_fields_defined & FONT_HEIGHT_BIT) WD_CHECK (height().serialize(*this,file)); if (m_fields_defined & FONT_ROTATION_BIT) WD_CHECK (rotation().serialize(*this,file)); if (m_fields_defined & FONT_WIDTH_SCALE_BIT) WD_CHECK (width_scale().serialize(*this,file)); if (m_fields_defined & FONT_SPACING_BIT) WD_CHECK (spacing().serialize(*this,file)); if (m_fields_defined & FONT_OBLIQUE_BIT) WD_CHECK (oblique().serialize(*this,file)); if (m_fields_defined & FONT_FLAGS_BIT) WD_CHECK (flags().serialize(*this,file)); if (!file.heuristics().allow_binary_data()) WD_CHECK (file.write((WT_Byte)')')); return WT_Result::Success; }
nsresult txMozillaXMLOutput::createResultDocument(const nsAString& aName, PRInt32 aNsID, nsIDOMDocument* aSourceDocument, nsIDOMDocument* aResultDocument) { nsresult rv; nsCOMPtr<nsIDocument> doc; if (!aResultDocument) { // Create the document if (mOutputFormat.mMethod == eHTMLOutput) { doc = do_CreateInstance(kHTMLDocumentCID, &rv); NS_ENSURE_SUCCESS(rv, rv); mDocumentIsHTML = PR_TRUE; } else { // We should check the root name/namespace here and create the // appropriate document doc = do_CreateInstance(kXMLDocumentCID, &rv); NS_ENSURE_SUCCESS(rv, rv); mDocumentIsHTML = PR_FALSE; } nsCOMPtr<nsIDocument_MOZILLA_1_8_BRANCH3> source = do_QueryInterface(aSourceDocument); NS_ENSURE_STATE(source); PRBool hasHadScriptObject = PR_FALSE; nsIScriptGlobalObject* sgo = source->GetScriptHandlingObject(hasHadScriptObject); NS_ENSURE_STATE(sgo || !hasHadScriptObject); nsCOMPtr<nsIDocument_MOZILLA_1_8_BRANCH3> doc18 = do_QueryInterface(doc); NS_ENSURE_STATE(doc18); doc18->SetScriptHandlingObject(sgo); mDocument = do_QueryInterface(doc); } else { mDocument = aResultDocument; doc = do_QueryInterface(aResultDocument); nsCOMPtr<nsIDocument> doc = do_QueryInterface(aResultDocument); mDocumentIsHTML = doc && !doc->IsCaseSensitive(); } mCurrentNode = mDocument; // Reset and set up the document URIUtils::ResetWithSource(doc, aSourceDocument); // Set the charset if (!mOutputFormat.mEncoding.IsEmpty()) { NS_LossyConvertUTF16toASCII charset(mOutputFormat.mEncoding); nsCAutoString canonicalCharset; nsCOMPtr<nsICharsetAlias> calias = do_GetService("@mozilla.org/intl/charsetalias;1"); if (calias && NS_SUCCEEDED(calias->GetPreferred(charset, canonicalCharset))) { doc->SetDocumentCharacterSet(canonicalCharset); doc->SetDocumentCharacterSetSource(kCharsetFromOtherComponent); } } // Set the mime-type if (!mOutputFormat.mMediaType.IsEmpty()) { doc->SetContentType(mOutputFormat.mMediaType); } else if (mOutputFormat.mMethod == eHTMLOutput) { doc->SetContentType(NS_LITERAL_STRING("text/html")); } else { doc->SetContentType(NS_LITERAL_STRING("application/xml")); } // Set up script loader of the result document. nsIScriptLoader *loader = doc->GetScriptLoader(); if (loader) { if (mNotifier) { loader->AddObserver(mNotifier); } else { // Don't load scripts, we can't notify the caller when they're loaded. loader->SetEnabled(PR_FALSE); } } if (mNotifier) { mNotifier->SetOutputDocument(mDocument); } // Do this after calling OnDocumentCreated to ensure that the // PresShell/PresContext has been hooked up and get notified. nsCOMPtr<nsIHTMLDocument> htmlDoc = do_QueryInterface(doc); if (htmlDoc) { htmlDoc->SetCompatibilityMode(eCompatibility_FullStandards); } // Add a doc-type if requested if (!mOutputFormat.mSystemId.IsEmpty()) { nsCOMPtr<nsIDOMDOMImplementation> implementation; rv = aSourceDocument->GetImplementation(getter_AddRefs(implementation)); NS_ENSURE_SUCCESS(rv, rv); nsAutoString qName; if (mOutputFormat.mMethod == eHTMLOutput) { qName.AssignLiteral("html"); } else { qName.Assign(aName); } nsCOMPtr<nsIDOMDocumentType> documentType; rv = implementation->CreateDocumentType(qName, mOutputFormat.mPublicId, mOutputFormat.mSystemId, getter_AddRefs(documentType)); NS_ASSERTION(NS_SUCCEEDED(rv), "Can't create doctype"); nsCOMPtr<nsIDOMNode> tmp; mDocument->AppendChild(documentType, getter_AddRefs(tmp)); } return NS_OK; }
bool nuiTextLayout::LayoutParagraph(int32 start, int32 length) { //printf("new paragraph: %d + %d\n", start, length); float spacewidth = 0; { nuiFontBase* pFont = mStyle.GetFont(); nuiGlyphInfo glyphinfo; uint32 space = pFont->GetGlyphIndex(32); pFont->GetGlyphInfo(glyphinfo, space, nuiFontBase::eGlyphNative); spacewidth = glyphinfo.AdvanceX; } mpParagraphs.push_back(new Paragraph()); nuiTextLine* pLine = new nuiTextLine(*this, 0, 0); mpParagraphs.back()->push_back(pLine); // Split the paragraph into ranges: nuiTextRangeList ranges; nuiSplitText(mUnicode, ranges, nuiST_ScriptChange, start, length, &Split); { nuiTextRangeList::iterator it = ranges.begin(); nuiTextRangeList::iterator end = ranges.end(); int32 origin = start; int32 i = 0; while (it != end) { const nuiTextRange& range(*it); int32 len = range.mLength; int32 pos = origin; //printf("\trange %d (%d - %d) (%s - %s)\n", i, pos, len, nuiGetUnicodeScriptName(range.mScript).GetChars(), nuiGetUnicodeRangeName(range.mRange).GetChars()); std::set<nglUChar>& charset(mCharsets[range.mScript]); { while (pos < origin + len) { nglUChar ch = mUnicode[pos++]; if (ucisprint(ch) && ch > 32) charset.insert(ch); } } origin += len; ++it; i++; } } { nuiTextRangeList::iterator it = ranges.begin(); nuiTextRangeList::iterator end = ranges.end(); uint32 i = 0; uint32 pos = start; while (it != end) { const nuiTextRange& range(*it); uint32 len = range.mLength; //printf("\trange %d (%d - %d) (%s - %s)\n", i, pos, len, nuiGetUnicodeScriptName(range.mScript).GetChars(), nuiGetUnicodeRangeName(range.mRange).GetChars()); nglUChar ch = mUnicode[pos]; nuiTextRun* pRun = NULL; if (ch < 32) { int32 tabs = 0; for (uint32 i = pos; i < pos + range.mLength; i++) { if (mUnicode[i] == 9) tabs++; } if (tabs > 0) { pRun = new nuiTextRun(*this, pos, len, spacewidth * (float)tabs, 0.0f); } } else { pRun = new nuiTextRun(*this, range.mScript, pos, len, mStyle); } if (pRun) pLine->AddRun(pRun); pos += len; ++i; ++it; } } return true; }
NumerUpDown::NumerUpDown ( widget &parent_, const string &label, double val, double min, double max, const nana::string &DefFileName/*=STR("NumUpDown.VertCenter.lay.txt")*/, double step/*=1*/, unsigned width/*=6*/, unsigned decimals/*=2*/ ) : CompoWidget( parent_, label, DefFileName), _label(*this,label), _val(val), _min(min), _max(max), _step(step), _decimals(decimals), _width(width) { _num.multi_lines(false); display(); _label.text_align(align::right ); InitMyLayout(); SelectClickableWidget( *this); SelectClickableWidget( _label); SelectClickableWidget( _num); _up.events().click ([&](){add( _step); }); _up.events().dbl_click([&](){add( _step); }); _down.events().click ([&](){add(-_step); }); _down.events().dbl_click([&](){add(-_step); }); //_num.make_event <events::focus>([&](const eventinfo& ei) // { // std::cerr<< "\nBefore " << (ei.focus.getting ? "geting ":"lossing ") << "Focus: , NumerUpDown: "; // std::wcerr<< _Titel << std::endl; // //if (!ei.focus.getting) // // validate_edit( ); // }); _num.events().focus([&](const arg_focus& ei) { std::cerr << std::endl<< (ei.getting ? "geting ":"lossing ") << "Focus: , NumerUpDown: "; std::wcerr<< _label.caption() << std::endl; if ( !ei.getting ) { std::cerr << "And validating: " << _val << "Cap:" << std::string(charset(_num.caption ())); validate_edit (); } }); //_num.make_event <events::focus>([&](const eventinfo& ei) // { // std::cerr<< "After " << (ei.focus.getting ? "geting ":"lossing ") << "Focus: , NumerUpDown: "; // std::wcerr<< _Titel << std::endl; // //if (!ei.focus.getting) // // validate_edit( ); // }); }
nsresult nsMsgSearchOfflineMail::Search (bool *aDone) { nsresult err = NS_OK; NS_ENSURE_ARG(aDone); nsresult dbErr = NS_OK; nsCOMPtr<nsIMsgDBHdr> msgDBHdr; nsMsgSearchBoolExpression *expressionTree = nsnull; const PRUint32 kTimeSliceInMS = 200; *aDone = false; // Try to open the DB lazily. This will set up a parser if one is required if (!m_db) err = OpenSummaryFile (); if (!m_db) // must be reparsing. return err; // Reparsing is unnecessary or completed if (NS_SUCCEEDED(err)) { if (!m_listContext) dbErr = m_db->EnumerateMessages (getter_AddRefs(m_listContext)); if (NS_SUCCEEDED(dbErr) && m_listContext) { PRIntervalTime startTime = PR_IntervalNow(); while (!*aDone) // we'll break out of the loop after kTimeSliceInMS milliseconds { nsCOMPtr<nsISupports> currentItem; dbErr = m_listContext->GetNext(getter_AddRefs(currentItem)); if(NS_SUCCEEDED(dbErr)) { msgDBHdr = do_QueryInterface(currentItem, &dbErr); } if (NS_FAILED(dbErr)) *aDone = true; //###phil dbErr is dropped on the floor. just note that we did have an error so we'll clean up later else { bool match = false; nsAutoString nullCharset, folderCharset; GetSearchCharsets(nullCharset, folderCharset); NS_ConvertUTF16toUTF8 charset(folderCharset); // Is this message a hit? err = MatchTermsForSearch (msgDBHdr, m_searchTerms, charset.get(), m_scope, m_db, &expressionTree, &match); // Add search hits to the results list if (NS_SUCCEEDED(err) && match) { AddResultElement (msgDBHdr); } PRIntervalTime elapsedTime; LL_SUB(elapsedTime, PR_IntervalNow(), startTime); // check if more than kTimeSliceInMS milliseconds have elapsed in this time slice started if (PR_IntervalToMilliseconds(elapsedTime) > kTimeSliceInMS) break; } } } } else *aDone = true; // we couldn't open up the DB. This is an unrecoverable error so mark the scope as done. delete expressionTree; // in the past an error here would cause an "infinite" search because the url would continue to run... // i.e. if we couldn't open the database, it returns an error code but the caller of this function says, oh, // we did not finish so continue...what we really want is to treat this current scope as done if (*aDone) CleanUpScope(); // Do clean up for end-of-scope processing return err; }
/** * Helper method for converting from non-UTF-8 encoded strings to UTF-8. * Supported LANG values for Linux: see /usr/share/i18n/SUPPORTED. * Supported encodings for libiconv: see iconv --list . * * Note! If non-ASCII characters are used we assume a proper LANG value!!! * * @param str_in The string to be converted. * @return Returns the input string in UTF-8. */ std::string digidoc::util::File::convertUTF8(const std::string &str_in, bool to_UTF) { std::string charset("C"); char *env_lang = getenv("LANG"); if(env_lang && charset.compare(env_lang) != 0) { charset = env_lang; size_t locale_start = charset.rfind("."); if(locale_start != std::string::npos) charset = charset.substr(locale_start+1); } // no conversion needed for UTF-8 if((charset.compare("UTF-8") == 0) || (charset.compare("utf-8") == 0)) return str_in; iconv_t ic_descr = iconv_t(-1); try { ic_descr = to_UTF ? iconv_open("UTF-8", charset.c_str()) : iconv_open(charset.c_str(), "UTF-8"); } catch(std::exception &) {} if( ic_descr == iconv_t(-1)) return str_in; ICONV_CONST char* inptr = (ICONV_CONST char*)str_in.c_str(); size_t inleft = str_in.size(); std::string out; char outbuf[64]; char* outptr; size_t outleft; while(inleft > 0) { outbuf[0] = '\0'; outptr = (char *)outbuf; outleft = sizeof(outbuf) - sizeof(outbuf[0]); size_t result = iconv(ic_descr, &inptr, &inleft, &outptr, &outleft); if(result == size_t(-1)) { switch(errno) { case E2BIG: break; case EILSEQ: case EINVAL: default: iconv_close(ic_descr); return str_in; break; } } *outptr = '\0'; out += outbuf; } iconv_close(ic_descr); return out; }
option_values_t destination_implObj::parse_attribute_values(info_t::lock &lock, ipp_attribute_t *attrs, const char *option_s, bool unicode_flag) { if (!attrs) return {}; auto count=ippGetCount(attrs); auto tag_value=ippGetValueTag(attrs); switch (tag_value) { case IPP_TAG_NOVALUE: return {}; case IPP_TAG_RANGE: { std::vector<std::tuple<int, int>> v; v.reserve(count); for (decltype (count) i=0; i<count; i++) { int upper; int lower=ippGetRange(attrs, i, &upper); v.emplace_back(lower, upper); } return v; } case IPP_TAG_RESOLUTION: { std::vector<resolution> v; v.reserve(count); for (decltype (count) i=0; i<count; i++) { resolution res; ipp_res_t units; res.xres=ippGetResolution(attrs, i, &res.yres, &units); switch (units) { case IPP_RES_PER_INCH: res.units=res.per_inch; break; case IPP_RES_PER_CM: res.units=res.per_cm; break; default: res.units=res.unknown; } v.push_back(res); } return v; } case IPP_TAG_INTEGER: { std::unordered_set<int> v; for (decltype (count) i=0; i<count; i++) { v.insert(ippGetInteger(attrs, i)); } return v; } case IPP_TAG_BOOLEAN: { std::unordered_set<bool> v; for (decltype (count) i=0; i<count; i++) { v.insert(ippGetBoolean(attrs, i)); } return v; } case IPP_TAG_ENUM: if (unicode_flag) { auto l=locale::base::global(); std::unordered_map<int, std::u32string> v; for (decltype (count) i=0; i<count; i++) { auto value=ippGetInteger(attrs, i); auto s=enum_string(option_s, value); auto us=unicode::iconvert::tou ::convert(s, l->charset()).first; v.emplace(value, us); } return v; } else { std::unordered_map<int, std::string> v; for (decltype (count) i=0; i<count; i++) { auto value=ippGetInteger(attrs, i); v.emplace(value, enum_string(option_s, value)); } return v; } case IPP_TAG_BEGIN_COLLECTION: { std::vector<const_collection> v; v.reserve(count); for (decltype (count) i=0; i<count; i++) { auto ippc=ippGetCollection(attrs, i); auto c=collection::create(); for (auto attr=ippFirstAttribute(ippc); attr; attr=ippNextAttribute(ippc)) { std::string n=ippGetName(attr); c->emplace(n, parse_attribute_values (lock, attr, n.c_str(), unicode_flag)); } v.push_back(c); } return v; } default: break; } std::unordered_map<std::string, std::string> v; bool is_media=strcmp(option_s, CUPS_MEDIA) == 0; auto localizer=strcmp(option_s, CUPS_SIDES) == 0 ? sides : strcmp(option_s, CUPS_PRINT_COLOR_MODE) == 0 ? print_color_mode : no_localizer; for (decltype (count) i=0; i<count; i++) { const char *lang=0; auto val=ippGetString(attrs, i, &lang); if (!val) { std::ostringstream o; o << "{unknown tag " << ippTagString(ippGetValueTag(attrs)) << "}"; v.emplace(o.str(), ""); continue; } std::string lang_s; if (is_media) { cups_size_t size; lang_s=val; if (cupsGetDestMediaByName(lock->http, lock->dest, lock->info, val, CUPS_MEDIA_FLAGS_DEFAULT, &size)) { auto l=cupsLocalizeDestMedia (lock->http, lock->dest, lock->info, CUPS_MEDIA_FLAGS_DEFAULT, &size); if (l) lang_s=l; } } else { auto c=cupsLocalizeDestValue(lock->http, lock->dest, lock->info, option_s, val); if (c && strcmp(c, val)) { lang_s=c; } else { lang_s=localizer(val); } } v.emplace(val, lang_s); } if (!unicode_flag) return v; auto l=locale::base::global(); std::unordered_map<std::string, std::u32string> uv; for (const auto &s:v) { auto n=s.first; uv.emplace(s.first, unicode::iconvert::tou::convert(s.second, l->charset()) .first); } return uv; }
void text::createFromString(const string& in, const charset& ch) { size_t asciiCount = 0; size_t asciiPercent = 0; removeAllWords(); // Check whether there is a recommended encoding for this charset. // If so, the whole buffer will be encoded. Else, the number of // 7-bit (ASCII) bytes in the input will be used to determine if // we need to encode the whole buffer. encoding recommendedEnc; const bool alwaysEncode = ch.getRecommendedEncoding(recommendedEnc); if (!alwaysEncode) { asciiCount = utility::stringUtils::countASCIIchars(in.begin(), in.end()); asciiPercent = (in.length() == 0 ? 100 : (100 * asciiCount) / in.length()); } // If there are "too much" non-ASCII chars, encode everything if (alwaysEncode || asciiPercent < 60) // less than 60% ASCII chars { appendWord(make_shared <word>(in, ch)); } // Else, only encode words which need it else { bool is8bit = false; // is the current word 8-bit? bool prevIs8bit = false; // is previous word 8-bit? unsigned int count = 0; // total number of words for (size_t end = in.size(), pos = 0, start = 0 ; ; ) { if (pos == end || parserHelpers::isSpace(in[pos])) { const string chunk(in.begin() + start, in.begin() + pos); if (pos != end) ++pos; if (is8bit) { if (count && prevIs8bit) { // No need to create a new encoded word, just append // the current word to the previous one. shared_ptr <word> w = getWordAt(getWordCount() - 1); w->getBuffer() += " " + chunk; } else { if (count) { shared_ptr <word> w = getWordAt(getWordCount() - 1); w->getBuffer() += ' '; } appendWord(make_shared <word>(chunk, ch)); prevIs8bit = true; ++count; } } else { if (count && !prevIs8bit) { shared_ptr <word> w = getWordAt(getWordCount() - 1); w->getBuffer() += " " + chunk; } else { appendWord(make_shared <word> (chunk, charset(charsets::US_ASCII))); prevIs8bit = false; ++count; } } if (pos == end) break; is8bit = false; start = pos; } else if (!parserHelpers::isAscii(in[pos])) { is8bit = true; ++pos; } else { ++pos; } } } }
void mailboxGroup::generateImpl(utility::outputStream& os, const string::size_type maxLineLength, const string::size_type curLinePos, string::size_type* newLinePos) const { // We have to encode the name: // - if it contains characters in a charset different from "US-ASCII", // - and/or if it contains one or more of these special chars: // SPACE TAB " ; , < > ( ) @ / ? . = : // Check whether there are words that are not "US-ASCII" // and/or contain the special chars. bool forceEncode = false; for (int w = 0 ; !forceEncode && w < m_name.getWordCount() ; ++w) { if (m_name.getWordAt(w)->getCharset() == charset(charsets::US_ASCII)) { const string& buffer = m_name.getWordAt(w)->getBuffer(); for (string::const_iterator c = buffer.begin() ; !forceEncode && c != buffer.end() ; ++c) { switch (*c) { case ' ': case '\t': case ';': case ',': case '<': case '>': case '(': case ')': case '@': case '/': case '?': case '.': case '=': case ':': forceEncode = true; break; } } } } string::size_type pos = curLinePos; m_name.encodeAndFold(os, maxLineLength - 2, pos, &pos, forceEncode ? text::FORCE_ENCODING : 0); os << ":"; ++pos; for (std::vector <ref <mailbox> >::const_iterator it = m_list.begin() ; it != m_list.end() ; ++it) { if (it != m_list.begin()) { os << ", "; pos += 2; } else { os << " "; ++pos; } (*it)->generate(os, maxLineLength - 2, pos, &pos); } os << ";"; pos++; if (newLinePos) *newLinePos = pos; }
void htmlTextPart::parse(shared_ptr <const bodyPart> message, shared_ptr <const bodyPart> parent, shared_ptr <const bodyPart> textPart) { // Search for possible embedded objects in the _whole_ message. std::vector <shared_ptr <const bodyPart> > cidParts; std::vector <shared_ptr <const bodyPart> > locParts; findEmbeddedParts(*message, cidParts, locParts); // Extract HTML text std::ostringstream oss; utility::outputStreamAdapter adapter(oss); textPart->getBody()->getContents()->extract(adapter); const string data = oss.str(); m_text = textPart->getBody()->getContents()->clone(); // Find charset shared_ptr <const contentTypeField> ctf = textPart->getHeader()->findField <contentTypeField>(fields::CONTENT_TYPE); if (ctf && ctf->hasCharset()) m_charset = ctf->getCharset(); else m_charset = charset(); // Extract embedded objects. The algorithm is quite simple: for each previously // found inline part, we check if its CID/Location is contained in the HTML text. for (std::vector <shared_ptr <const bodyPart> >::const_iterator p = cidParts.begin() ; p != cidParts.end() ; ++p) { const shared_ptr <const headerField> midField = (*p)->getHeader()->findField(fields::CONTENT_ID); const messageId mid = *midField->getValue <messageId>(); if (data.find("CID:" + mid.getId()) != string::npos || data.find("cid:" + mid.getId()) != string::npos) { // This part is referenced in the HTML text. // Add it to the embedded object list. addEmbeddedObject(**p, mid.getId(), embeddedObject::REFERENCED_BY_ID); } } for (std::vector <shared_ptr <const bodyPart> >::const_iterator p = locParts.begin() ; p != locParts.end() ; ++p) { const shared_ptr <const headerField> locField = (*p)->getHeader()->findField(fields::CONTENT_LOCATION); const text loc = *locField->getValue <text>(); const string locStr = loc.getWholeBuffer(); if (data.find(locStr) != string::npos) { // This part is referenced in the HTML text. // Add it to the embedded object list. addEmbeddedObject(**p, locStr, embeddedObject::REFERENCED_BY_LOCATION); } } // Extract plain text, if any. if (!findPlainTextPart(*message, *parent, *textPart)) { m_plainText = make_shared <emptyContentHandler>(); } }
bool NE::parse(Tree *tree) const { TreeAllocator *allocator = tree->allocator(); CHECK_TREE_FALSE(allocator); if (action_mode() == PARSING_MODE) { CHECK_TREE_FALSE(model_); if (!allocator->crfpp_ne) { allocator->crfpp_ne = crfpp_model_new_tagger(model_); CHECK_TREE_FALSE(allocator->crfpp_ne); } crfpp_set_model(allocator->crfpp_ne, model_); crfpp_clear(allocator->crfpp_ne); } std::string ne_composite; switch (tree->posset()) { case IPA: ne_composite = ne_composite_ipa_; break; case JUMAN: ne_composite = ne_composite_juman_; break; case UNIDIC: ne_composite = ne_composite_unidic_; break; default: CHECK_TREE_FALSE(false) << "unknown posset"; } int comp = 0; const size_t size = tree->token_size(); std::string tmp; for (size_t i = 0; i < size; ++i) { const Token *token = tree->token(i); const char *surface = token->normalized_surface; const char *feature = token->feature; if (std::string(feature).find(ne_composite) == 0) { ++comp; } else { comp = 0; } if (comp >= 2) continue; char *char_feature = tree->alloc(16); get_char_feature(charset(), surface, char_feature); if (tree->posset() == IPA || tree->posset() == UNIDIC) { concat_feature(token, 4, &tmp); } else if (tree->posset() == JUMAN) { concat_feature(token, 2, &tmp); } const char *pos = tree->strdup(tmp.c_str()); allocator->feature.clear(); allocator->feature.push_back(surface); allocator->feature.push_back(char_feature); allocator->feature.push_back(pos); const char *ne = token->ne; if (action_mode() == PARSING_MODE) { if (ne) { allocator->feature.push_back(ne); } crfpp_add2(allocator->crfpp_ne, allocator->feature.size(), (const char **)&allocator->feature[0]); } else { CHECK_TREE_FALSE(ne) << "named entity is not defined"; std::copy(allocator->feature.begin(), allocator->feature.end(), std::ostream_iterator<const char*>( *(allocator->stream()), " ")); *(allocator->stream()) << ne << std::endl; } } if (action_mode() == PARSING_MODE) { CHECK_TREE_FALSE(crfpp_parse(allocator->crfpp_ne)); const char *prev = 0; size_t ci = 0; int comp = 0; for (size_t i = 0; i < size; ++i) { Token *token = tree->mutable_token(i); if (std::string(token->feature).find(ne_composite) == 0) { ++comp; } else { comp = 0; } if (comp >= 2) { char *ne = tree->strdup(prev); if (ne[0] != 'O') { ne[0] = 'I'; } token->ne = ne; } else { token->ne = tree->strdup(crfpp_y2(allocator->crfpp_ne, ci)); ++ci; } if ((prev && prev[0] == 'I' && token->ne[0] == 'I' && std::strlen(token->ne) >= 3 && std::strlen(prev) >= 3 && std::strcmp(token->ne + 3, prev + 3)) || (i == 0 && token->ne[0] == 'I')) { char *ne = tree->strdup(token->ne); ne[0] = 'B'; token->ne = ne; } prev = token->ne; } } else { *(allocator->stream()) << std::endl; } return true; }