2

We have issue validating a std::string that contains a valid UTF_8 character set. The issue is I have a std::string in program, which may receive few invalid UTF-8 characters at run time. Currently i am referring glib/utf8-validate.c file for reference. But the data types used by glib is not a std::string, so I can not use this.

Platform is QNX (Linux host development).

artless noise
  • 21,212
  • 6
  • 68
  • 105
user2655475
  • 21
  • 1
  • 2
  • Just to nitpick, QNX is *not* Linux-based. It's a completely separate operating system and not related to Linux in any way. – Some programmer dude Aug 06 '13 at 05:47
  • 1
    Hi joachim, thanks for input, basically i want to convey that i am working in non windows environment.hence mentioned like that. – user2655475 Aug 06 '13 at 12:25
  • @JoachimPileborg While you are correct that QNX is not Linux, the QNX development environment can run on Linux and cross-compile to QNX targets. This is what he was indicating. It can also run on Windows, or on QNX itself. See http://www.qnx.com/download/feature.html?programid=21179 for the Linux-hosted version. – kmort Nov 20 '13 at 14:52

1 Answers1

1

First you should always include version of QNX in your question, because for example gcc version of 6.3.2 (Neutrino) is 3.7.

Second use const char* c_str() const; for c functions.

#include <stdio.h>
#include <dbus/dbus.h>
#include <string>
#include <iostream>

int main(void)
{    
std::string test = "test";

std::cout << dbus_validate_utf8(test.c_str(), 0) << std::endl;
return 0;
}

But may be this will help instead of using dbus_validate_utf8 ?:

How to detect UTF-8 in plain C?

Or even this:

http://utfcpp.sourceforge.net/

Community
  • 1
  • 1
Maquefel
  • 480
  • 4
  • 16
  • Hi Maquefel, Thanks for quick reply. My actual question here is how to validate a std::string for UTF-8 encoding, Basically i am looking for piece of code or function which will take a std::string as input and return true if each and every character in that string is a valid UTF-8 charter or false if not. I dont understand how the conversion of std::string to C string resolves this, could you please explain? – user2655475 Aug 06 '13 at 12:23
  • What is version of your QNX system and gcc ? Or you are using qcc? – Maquefel Aug 06 '13 at 17:50
  • He means that you can use `word.c_str()` and then use the *glibc* UTF8 code that you already discovered (IE, utf8-validate.c). As well, Xerces/Xsd from CodeSynthesis definitely does this, but it may be hard to comprehend depending on your understanding. – artless noise Aug 06 '13 at 18:02
  • 2artless noise not exactly by last comment i mean that on some QNX distributions you have 3rd party libs and boost compile which makes such a job easier. May be he have 6.5 then a latest Qt library is available, my guess if he needs checking UTF-8 encoding he is writing interface or localization. – Maquefel Aug 06 '13 at 19:45
  • we are using QNX 6.5.0 and qcc, but it is customised for project.We don't have QT. coming to third party libs we have DBUS and POCO included in our project,but we are not allowed to call these API directly,so i am trying to explore _dbus_string_validate_utf8() from dbus-string.c ,and implement same logic here in my service,but again need to reslove lof of dependencies. – user2655475 Aug 07 '13 at 10:51
  • I looked up the code for "dbus_validate_utf8" and it is impossible to extract from the dbus system easily. It also adds some crazy dependencies to your project, therefore i don't think this is a good answer – Lothar Jan 27 '17 at 01:05