Exemplo n.º 1
0
BOOST_FIXTURE_TEST_CASE(TestListDir, FileOpenFixture)
{
    std::string parentPath = tmpPath.string();
    std::vector<std::string> files = ListDir(parentPath, "txt");
    BOOST_TEST_REQUIRE(files.size() == 3u);
    for(const std::string& file : files)
    {
        BOOST_REQUIRE(bfs::exists(file));
        BOOST_REQUIRE(bfs::path(file).is_absolute());
        // Filepath must be utf8 encoded
        BOOST_REQUIRE(isValidUTF8(file));

        bfs::path filePath(file);
        // String result must still be utf8
        BOOST_REQUIRE(isValidUTF8(filePath.string()));

        // Scopes for auto-close
        {
            // path input
            bnw::ifstream sFile(filePath);
            BOOST_REQUIRE(sFile);
            std::string content;
            BOOST_REQUIRE(sFile >> content);
            BOOST_TEST_REQUIRE(content == "OK");
        }

        {
            // string input
            bnw::ifstream sFile(file);
            BOOST_REQUIRE(sFile);
            std::string content;
            BOOST_REQUIRE(sFile >> content);
            BOOST_TEST_REQUIRE(content == "OK");
        }

        {
            // Memory mapped file
            boost::iostreams::mapped_file_source mmapFile;
            mmapFile.open(bfs::path(file));
            BOOST_TEST_REQUIRE(mmapFile.is_open());
            using MMStream = boost::iostreams::stream<boost::iostreams::mapped_file_source>;
            MMStream map(mmapFile);
            std::string content;
            BOOST_REQUIRE(map >> content);
            BOOST_TEST_REQUIRE(content == "OK");
        }
    }
}
Exemplo n.º 2
0
char *
Helpers::GnunetFsDubiousMetaToUtf8 (enum EXTRACTOR_MetaFormat format,
                                    const char *data, size_t data_len)
{
  switch (format)
  {
  case EXTRACTOR_METAFORMAT_UTF8:
    /* data must not contain NULLs (hence the -1) */
    if (isValidUTF8 (data))
      return GNUNET_strdup (data);
    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                _("Failed to validate supposedly utf-8 string `%s' of length %u, assuming it to be a C string\n"),
                data,
                (unsigned int) data_len);
    format = EXTRACTOR_METAFORMAT_C_STRING;
    /* fall-through */
  case EXTRACTOR_METAFORMAT_C_STRING:
    if (data_len > 0)
    {
      /* There are no guarantees that data is NULL-terminated, AFAIU,
       * so let's play it safe, shall we?
       */
      char data_copy[data_len + 1];

      memcpy (data_copy, data, data_len);
      data_copy[data_len] = '\0';
      return "Implement me !";//GNUNET_GTK_from_loc_to_utf8 (data_copy);
    }
    break;
  default:
    break;
  }
  return NULL;
}
Exemplo n.º 3
0
void TranslateString(char *src, size_t size)
{
	char *fptr = src;
	size_t src_len = strlen(src);
	char *tptr_start = alloca(src_len * 4 + 1);
	char *tptr = tptr_start;

	if (isValidUTF8(src))
		strncpy(tptr_start, fptr, src_len + 1);
	else {
		while (*fptr) {
			int i;
			for (i = 0; tc[i] && (tc[i] != *fptr); i++);
			if (tc[i]) {
				*tptr++ = 0xC3;
				*tptr++ = su[i];
				fptr++;
			} else if (*fptr & 0x80)
				fptr++;
			else
				*tptr++ = *fptr++;
		}
		*tptr = 0;
	}

	fptr = tptr_start;
	tptr = src;
	char *tptr_end = src + size - 4;
	while (*fptr && tptr < tptr_end) {
		if (*fptr == '~') {
			fptr++;
			int i;
			for (i = 0; sc[i] && (sc[i] != *fptr); i++);
			if (sc[i]) {
				*tptr++ = 0xC3;
				*tptr++ = su[i];
				fptr++;
			} else if (*fptr == 'd') {
				*tptr++ = 0xC2;
				*tptr++ = 0xb0;
				fptr++;
			} else
				*tptr++ = '~';
		} else
			CopyUTF8Char(&tptr, &fptr);
	}
	*tptr = 0;
}
/**
 *  @brief
 */
std::vector<std::string> glArchivItem_Font::WrapInfo::CreateSingleStrings(const std::string& text)
{
    RTTR_Assert(isValidUTF8(text)); // Can only handle UTF-8 strings!

    std::vector<std::string> destStrings;
    if(positions.empty())
        return destStrings;

    destStrings.reserve(positions.size());
    unsigned curStart = positions.front();
    for(auto it = positions.begin() + 1; it != positions.end(); ++it)
    {
        RTTR_Assert(*it >= curStart);
        std::string curLine = text.substr(curStart, *it - curStart);
        boost::algorithm::trim_right(curLine);
        destStrings.push_back(curLine);
        curStart = *it;
    }
    /* Push last part */
    destStrings.push_back(text.substr(curStart));
    return destStrings;
}
Exemplo n.º 5
0
/**
 * This is a fallback method.  It should never be called.
 * If UTF8 support is desired, then a UTF8 StringMgr needs
 * to be used.
 *
 * Here we just do our best.
 *
 * Converts the param to an upper case UTF8 string
 * @param t - The text encoded in utf8 which should be turned into an upper case string
 *
 */	
char *StringMgr::upperUTF8(char *t, unsigned int maxlen) const {
	// try to decide if it's worth trying to toupper.  Do we have more
	// characters which are probably lower latin than not?
	// we still don't use isValidUTF8 optimally. what if we have 1 unicode
	// character in the string?  should we not try to upper any of the string?
	// dunno.  Best solution is to upper all other characters. Don't have
	// time to write that before release.
	long performOp = 0;
	if (!isValidUTF8((unsigned char *)t)) {
		performOp = 1;
	}
	else {
		for (const char *ch = t; *ch; ch++) {
			performOp += (*ch > 0) ? 1 : -1;
		}
	}

	if (performOp > 0) {
		return upperLatin1(t);
	}

	return t;
}
Exemplo n.º 6
0
zmsg_t* PrimeWorker::ReceiveRequest(proto::Request& req, void* socket) {
	
	zmsg_t* msg = zmsg_recv(socket);
	zframe_t* frame = zmsg_last(msg);
	zmsg_remove(msg, frame);
	size_t fsize = zframe_size(frame);
	const byte* fbytes = zframe_data(frame);
	
	bool ok = req.ParseFromArray(fbytes, fsize);
	zframe_destroy(&frame);
	
	bool valid = false;
	while(ok){
		
		if(!proto::Request::Type_IsValid(req.type()))
			break;
		
		if(!req.has_reqnonce())
			break;
		
		if(CheckVersion(req.version()) <= 0)
			break;
		
		uint256 reqnonce;
		{
			const std::string& nonce = req.reqnonce();
			if(nonce.length() != sizeof(uint256))
				break;
			memcpy(reqnonce.begin(), nonce.c_str(), sizeof(uint256));
		}
		
		if(!CheckReqNonce(reqnonce) || !mReqNonces.insert(reqnonce).second)
			break;
		
		if(req.has_stats()){
			
			const proto::ClientStats& stats = req.stats();
			if(!isValidUTF8(stats.addr()))
				break;
			if(!isValidUTF8(stats.name()))
				break;
			if(stats.cpd() < 0 || stats.cpd() > 150. || stats.cpd() != stats.cpd())
				break;
			
		}
		
		if(req.has_share()){
			
			const proto::Share& share = req.share();
			if(!isValidUTF8(share.addr()))
				break;
			if(!isValidUTF8(share.name()))
				break;
			if(!isValidUTF8(share.hash()))
				break;
			if(!isValidUTF8(share.merkle()))
				break;
			if(!isValidUTF8(share.multi()))
				break;
			if(share.has_blockhash() && !isValidUTF8(share.blockhash()))
				break;
			
		}
		
		valid = true;
		break;
	}
	
	if(valid)
		return msg;
	else{
		mInvCount++;
		zmsg_destroy(&msg);
		return 0;
	}
	
}
Exemplo n.º 7
0
bool isValidUTF8(const std::string& s) {
    return isValidUTF8(s.c_str());
}
Exemplo n.º 8
0
 bool isValidUTF8(string s) { 
     return isValidUTF8(s.c_str()); 
 }
/**
 *  Gibt Infos, über die Unterbrechungspunkte in einem Text
 *
 *  @param[in]     text            Text, der auf Zeilen verteilt werden soll
 *  @param[in]     primary_width   Maximale Breite der ersten Zeile
 *  @param[in]     secondary_width Maximale Breite der weiteren Zeilen
 */
glArchivItem_Font::WrapInfo glArchivItem_Font::GetWrapInfo(const std::string& text, const unsigned short primary_width,
                                                           const unsigned short secondary_width)
{
    if(!fontNoOutline)
        initFont();

    RTTR_Assert(isValidUTF8(text)); // Can only handle UTF-8 strings!

    // Current line width
    unsigned line_width = 0;
    // Width of current word
    unsigned word_width = 0;
    unsigned curMaxLineWidth = primary_width;

    WrapInfo wi;
    // We start the first line at the first char (so wi.positions.size() == numLines)
    wi.positions.push_back(0);

    utf8Iterator it(text.begin(), text.begin(), text.end());
    utf8Iterator itEnd(text.end(), text.begin(), text.end());
    utf8Iterator itWordStart = it;

    const unsigned spaceWidth = CharWidth(' ');

    for(;; ++it)
    {
        uint32_t curChar = (it != itEnd) ? *it : 0;
        // Word ended
        if(curChar == 0 || curChar == '\n' || curChar == ' ')
        {
            // Is the current word to long for the current line
            if(word_width + line_width > curMaxLineWidth)
            {
                // Word does not fit -> Start new line

                // Can we fit the word in one line?
                if(word_width <= secondary_width)
                {
                    // New line starts at index of word start
                    wi.positions.push_back(static_cast<unsigned>(itWordStart.base() - text.begin()));
                    line_width = 0;
                } else
                {
                    // Word does not even fit on one line -> Put as many letters in one line as possible
                    for(utf8Iterator itWord = itWordStart; itWord != it; ++itWord)
                    {
                        unsigned letter_width = CharWidth(*itWord);

                        // Can we fit the letter onto current line?
                        if(line_width + letter_width <= curMaxLineWidth)
                            line_width += letter_width; // Add it
                        else
                        {
                            // Create new line at this letter
                            wi.positions.push_back(static_cast<unsigned>(itWord.base() - text.begin()));
                            line_width = letter_width;
                            itWordStart = nextIt(itWord);
                        }
                    }

                    // Restart word
                    word_width = 0;
                }
                curMaxLineWidth = secondary_width;
            }
            if(curChar == 0)
                break;
            else if(curChar == ' ')
            {
                // Set up this line if we are going to continue it (not at line break or text end)
                // Line contains word and whitespace
                line_width += word_width + spaceWidth;
                word_width = 0;
                itWordStart = nextIt(it);
            } else
            {
                // If line break add new line (after all the word-breaking above)
                itWordStart = nextIt(it);
                if(itWordStart == itEnd)
                    break; // Reached end
                word_width = 0;
                line_width = 0;
                wi.positions.push_back(static_cast<unsigned>(itWordStart.base() - text.begin()));
            }
        } else
        {
            // Some char -> Add its width
            word_width += CharWidth(curChar);
        }
    }

    // Ignore trailing newline
    if(wi.positions.back() + 1 >= text.length() && wi.positions.size() > 1)
        wi.positions.pop_back();
    return wi;
}
/**
 *  Zeichnet einen Text.
 *
 *  @param[in] x      X-Koordinate
 *  @param[in] y      Y-Koordinate
 *  @param[in] text   Der Text
 *  @param[in] format Format des Textes (verodern)
 *                      @p FontStyle::LEFT    - Text links ( standard )
 *                      @p FontStyle::CENTER  - Text mittig
 *                      @p FontStyle::RIGHT   - Text rechts
 *                      @p FontStyle::TOP     - Text oben ( standard )
 *                      @p FontStyle::VCENTER - Text vertikal zentriert
 *                      @p FontStyle::BOTTOM  - Text unten
 *  @param[in] color  Farbe des Textes
 *  @param[in] length Länge des Textes
 *  @param[in] max    maximale Länge
 *  @param     end    Suffix for displaying a truncation of the text (...)
 */
void glArchivItem_Font::Draw(DrawPoint pos, const std::string& text, FontStyle format, unsigned color, unsigned short length,
                             unsigned short maxWidth, const std::string& end)
{
    if(!fontNoOutline)
        initFont();

    RTTR_Assert(isValidUTF8(text));

    // Breite bestimmen
    if(length == 0)
        length = (unsigned short)text.length();

    unsigned maxNumChars;
    unsigned short textWidth;
    bool drawEnd;
    if(maxWidth == 0xFFFF)
    {
        maxNumChars = length;
        textWidth = getWidth(text, length);
        drawEnd = false;
    } else
    {
        RTTR_Assert(isValidUTF8(end));
        textWidth = getWidth(text, length, maxWidth, &maxNumChars);
        if(!end.empty() && maxNumChars < length)
        {
            unsigned short endWidth = getWidth(end);

            // If "end" does not fit, draw nothing
            if(textWidth < endWidth)
                return;

            // Wieviele Buchstaben gehen in den "Rest" (ohne "end")
            textWidth = getWidth(text, length, textWidth - endWidth, &maxNumChars) + endWidth;
            drawEnd = true;
        } else
            drawEnd = false;
    }

    if(maxNumChars == 0)
        return;
    auto itEnd = text.cbegin();
    std::advance(itEnd, maxNumChars);

    // Vertical alignment (assumes 1 line only!)
    if(format.is(FontStyle::BOTTOM))
        pos.y -= dy;
    else if(format.is(FontStyle::VCENTER))
        pos.y -= dy / 2;
    // Horizontal center must change current line only. Everything else changes the start point
    DrawPoint curPos(pos);
    // Horizontal alignment
    if(format.is(FontStyle::RIGHT))
        curPos.x = pos.x -= textWidth;
    else if(format.is(FontStyle::CENTER))
    {
        unsigned short line_width;
        const auto itNl = std::find(text.cbegin(), itEnd, '\n');
        if(itNl != itEnd)
            line_width = getWidthInternal(text.cbegin(), itNl);
        else
            line_width = textWidth;
        curPos.x = pos.x - line_width / 2;
    }

    texList.texCoords.clear();
    texList.vertices.clear();

    for(auto it = text.begin(); it != itEnd;)
    {
        const uint32_t curChar = utf8::next(it, itEnd);
        if(curChar == '\n')
        {
            if(format.is(FontStyle::CENTER))
            {
                unsigned short line_width;
                const auto itNl = std::find(it, itEnd, '\n');
                line_width = getWidthInternal(it, itNl);
                curPos.x = pos.x - line_width / 2;
            } else
                curPos.x = pos.x;
            curPos.y += dy;
        } else
            DrawChar(curChar, texList, curPos);
    }

    if(drawEnd)
    {
        for(auto it = end.begin(); it != end.end();)
        {
            const uint32_t curChar = utf8::next(it, end.end());
            if(curChar == '\n')
            {
                curPos.x = pos.x;
                curPos.y += dy;
            } else
                DrawChar(curChar, texList, curPos);
        }
    }

    if(texList.vertices.empty())
        return;

    // Get texture first as it might need to be created
    glArchivItem_Bitmap& usedFont = format.is(FontStyle::NO_OUTLINE) ? *fontNoOutline : *fontWithOutline;
    unsigned texture = usedFont.GetTexture();
    if(!texture)
        return;
    const GlPoint texSize(usedFont.GetTexSize());
    RTTR_Assert(texList.texCoords.size() == texList.vertices.size());
    RTTR_Assert(texList.texCoords.size() % 4u == 0);
    // Vectorizable loop
    for(unsigned i = 0; i < texList.texCoords.size(); i += 4)
    {
        for(int j = 0; j < 4; j++)
            texList.texCoords[i + j] /= texSize;
    }

    glVertexPointer(2, GL_FLOAT, 0, &texList.vertices[0]);
    glTexCoordPointer(2, GL_FLOAT, 0, &texList.texCoords[0]);
    VIDEODRIVER.BindTexture(texture);
    glColor4ub(GetRed(color), GetGreen(color), GetBlue(color), GetAlpha(color));
    glDrawArrays(GL_QUADS, 0, texList.vertices.size());
}