Dealing with string encodings


Question ,

I may be slightly off-topic with this but I'm not really sure where else
to go with this.

what's the "best/easiest" ways to deal with string encodings

Right now, I'm using wstring for all my string operations outside the GUI
and this works really well. Also using it for file io.

Problem is this though: It's defined as wchar_t which inherently isn't a
problem, except that wchar_t is 32-bit under linux and 16-bit under
windows.

That difference right there is going to break my file IO code as I target
both platforms.

utf-8 is another option but I don't terribly like it much due to the fact
that each character can have a different width in bytes.

That is one of the things I like the most about 32-bit wchar_t. No matter
what, each character will always be *one* wchar_t. Makes indexing into a
string simple and painless.

But still, with one platform/compiler (not sure on which level the
difference is) having wchar_t 32-bit and the other 16-bit, this is slowly
turning into a nightmare.

Any suggestions would be very appreciated,

Posted On: Wednesday 7th of November 2012 12:53:11 PM Total Views:  435
View Complete with Replies

RELATED TOPICS OF C Language PROGRAMMING LANGUAGE




Pointers + dealing with integer arrays and strings

I am a beginner and have some confusion with respect to pointers and strings. It seems that the pointers with dealing with integer arrays behave differently, as opposed to strings. Can some one explain me the difference Sample Program: int main() { int array[]={1,2,3,4,5}; char array1[]={"Name is Max"}; int *ptr; char *ptr1; ptr=array; ptr1=array1; cout
VIEWS ON THIS POST

160

Posted on:

Monday 5th November 2012
View Replies!

dealing with UTF-8 file with C++

I am wondering how can read/write UTF-8 files with C++. Say, I know there is a file encoded with UTF-8, I am now wanting to change some character in the file. How can I achieve Could anybody here to help on explaining some encoding issue
VIEWS ON THIS POST

114

Posted on:

Wednesday 7th November 2012
View Replies!

Boost::Thread: Joining without waiting

Hi! I'm experimenting with boost::thread. I have a GUI application with a slot function: void someFunction(){...} void Frame::OnOkButtonClick(...) { switch(someInteger) { case 0: break case 1: { boost::thread mthTread(&someFunction); mthTread.join(); break; } default: break } } The program compiles and runs properly, but the GUI doesn't react (Well, this is what to expect with this code because it waits for the Thread to finish). How can I make the program not to wait until the thread is finished
VIEWS ON THIS POST

138

Posted on:

Wednesday 7th November 2012
View Replies!

Can't figure out what's wrong with this code

Thanks Ian and Suman, that was the problem. I'll make sure I give my friend this lin
VIEWS ON THIS POST

141

Posted on:

Wednesday 7th November 2012
View Replies!

replace a array with fixed size to a variable size (at runtime)

I have a function (IOExchange) from a Hardware Vendor (it means not changeable). In all the code examples, parameter 6 is a array with fixed size e.g. float[10]. It must be 'a pointer to the users data puffer'. Now i want to have this parameter 6 changeable at runtime so i can't define a array with fixed size. My fist attempt to replace the array was a: std::list input; This does not work input gets no values. How do I solve this Problem. ------Thread-execute---------------------------------- .... .... while (!this->Terminated){ input.clear(); Form1->Memo1->Clear(); sRet=IOExchange(0,0,0,0, Form1->Edit1->Text.ToInt(), Form1->Edit2->Text.ToInt(), // Receive Size &input, 0L); } for (std::list::iterator it=input.begin(); it != input.end(); it++) { Form1->Memo1->Lines->Add(*it); }
VIEWS ON THIS POST

168

Posted on:

Wednesday 7th November 2012
View Replies!

for_each loop on a map without a functor

I used to loop on a std::map to act on the data (d) like that (d being a class with setIntMember method): typedef std::map tMap; struct setIntMember { setIntMember(int j) : i(j) {} void operator()(tMap::value_type& pair) { pair.second.setIntMember(i); } int i; }; The loop being: for_each(m.begin(), m.end(), setIntMember(4)); I searched for a method to write all in the for_each loop (to avoid increasing the functors). We need a function returning the second member of the pair tMap::value_type, and their is the SGI template: template struct Select2nd : public unary_function { typename _Pair::second_type& operator()(_Pair& __x) const { return __x.second; } const typename _Pair::second_type& operator()(const _Pair& __x) const { return __x.second; } }; But I didn't manage to write a single line to perform the for_each loop, thanks to Select2nd with for_each, bind2nd and mem_fun and mem_fun1 I didn't manage with boost::bind (on VC++ 6.0): With boost, I can access the objects like that: for_each(m.begin(), m.end(), boost::bind(&d::Trace, boost::bind(&tMap::value_type::second, _1))); but the d::Trace method is called on a temporary object, so I cannot modify the content of the objects contained in the map. Some ideas
VIEWS ON THIS POST

164

Posted on:

Wednesday 7th November 2012
View Replies!

Re: Using boost::lambda with std::count_if

francis_r writes: > I have following class: > > class User > { > public: > enum Status > { > Online, > Away > Offline > }; > > User(const std::string & name, Status status); > > Status status() const; > > const std::string & name() const; > > private: > std::string mName; > Status mStatus; > }; > > And I have a vector: > std::vector users; > > Now I want to use count_if to count all users that have not the > Offline status. I can do this by writing a predicate function of > course but I think it would be nice to do this using a lambda > expression. > > I can't figure out how to write the expression. Idealy it would look > something like this I think: > count_if(users.begin(), users.end(), lambda::_1.status() != > User::Offline); > But that doesn't compile, as I expected. Indeed. The type of _1 doesn't have a member function named status. std::count_if(users.begin(), users.end(), boost::bind(&User::status,_1)!=User::Offline); works for me. > Another question: suppose I do write following predicate functions: > > bool Predicate_UserOnline(const User& user) > { return user.status() == User::Online; } > > bool Predicate_UserAway(const User& user) > { return user.status() == User::Away;} > > bool Predicate_UserOffline(const User& user) > { return user.status() == User::Offline; } > > Is it then possible to count the users, using count_if, that are > either Online or Away It would be !Predicate_UserOffline, but I can't > use that expression as a function pointer. But you can use !boost::bind(&Predicate_UserOffline,_1) -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
VIEWS ON THIS POST

245

Posted on:

Wednesday 7th November 2012
View Replies!

operator+ on nested type within template class

Michael Kiermaier ha scritto: > , > > I have this code: > > #include > > template class modRing { > public: > class element { > public: > element(unsigned int valIn): > val(valIn) > {} > unsigned int val; > }; > }; > > template > typename modRing::element > operator+(typename modRing::element const &a, typename > modRing::element const &b) { > return typename modRing::element((a.val + b.val) % modulus); > } > > int main() { > typedef modRing R; > R::element a(3); > R::element b(4); > std::cout } > > > Compilation with g++ 4.1.2 results in: > > In function int main(): > 24: error: no match for operator+ in a + b > > > So the compiler does not recognize that it can specialize the operator+ > function to the situation in line 24. My questions are: > Why "typename modRing::element" is a so-called "non-deduced" context that doesn't allow the compiler to deduce modulus. > How can I fix this problem > template class modRing { public: class element { public: element(unsigned int valIn): val(valIn) {} unsigned int val; friend element operator+(element const &a, element const &b) { return element((a.val + b.val) % modulus); } }; }; HTH, Ganesh -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
VIEWS ON THIS POST

160

Posted on:

Wednesday 7th November 2012
View Replies!

collections with items accessible by name?

I've been looking into vector, deque and list documentation. None of these have the nifty little feature that a VB collection has, and which comes in very handy for code readability as soon as you are developing a GUI. I'm thinking along the lines of mywindow("lblLastChanged")->caption = "2008-01-18 14:13h"; Before I create my own template for this, based on list or vector class, is there something that does this in any c++ libraries I must have missed it then. And if there is an implementation, is it speed-optimized (with indexing maybe) Just so that it doesn't end up similar to the following element *collection::getElementByName (const char *name) { element *e = this->first; do { if (!strcmp (name, e->name)) return e; e = e->next; } while (e != this->first); return NULL; }
VIEWS ON THIS POST

125

Posted on:

Wednesday 7th November 2012
View Replies!

Formatting a large number with a locale

I have some very large numbers that don't fit in any of the standard (or non-standard) builtins. I can make these base-10 ascii easily, but I would like to use a locale for nice l10n support. Is there any solution to this I see the std::num_put facet but it only takes builtins.
VIEWS ON THIS POST

160

Posted on:

Wednesday 7th November 2012
View Replies!

Comparing a Vector with Another Vector or a Single Value

Consider a function that compares a vector of doubles, for instance, with either another vector of equal length or just a single double. The type of comparison is not known until runtime. The output will be a vector of bools. Case 1: For each element in vectorA, if vectorA[x] is greater than vectorB[x], output[x] = true. Otherwise output[x] = false. Case 2: For each element in vectorA, if vectorA[x] is greater than doubleB, output[x] = true. Otherwise output[x] = false. My plan is to use a Boost:Variant to hold either a pointer to vectorB or doubleB itself. The comparison function will step through vectorA and either compare each element with the corresponding element in vectorB or just the single doubleB value. (I could also just fill a vector with multiple copies of doubleB, eliminating Case 2 above.) Is this the best approach -- Charles [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] , In article , Charles wrote: > Consider a function that compares a vector of doubles, for instance, with > either another vector of equal length or just a single double. The type of > comparison is not known until runtime. The output will be a vector of bools. > > Case 1: > For each element in vectorA, if vectorA[x] is greater than vectorB[x], > output[x] = true. Otherwise output[x] = false. > Case 2: > For each element in vectorA, if vectorA[x] is greater than doubleB, > output[x] = true. Otherwise output[x] = false. > > My plan is to use a Boost:Variant to hold either a pointer to vectorB or > doubleB itself. The comparison function will step through vectorA and either > compare each element with the corresponding element in vectorB or just the > single doubleB value. (I could also just fill a vector with multiple copies > of doubleB, eliminating Case 2 above.) > > Is this the best approach Depends, if the variant is just for this operation, its not needed. and only a functor with three overloads of operator (). such as VectorGreater below. class VectorGreater { std::vector *ans; public: typedef const std::vector &vector_type; typedef std::vector const & return_type; typedef double scalar_type; VectorGreater(std::vector &a):ans(&a){} // do the compares storing results in *ans, and return // *ans as a const reference, avoiding unnecesary copying // of the result. return_value operator ()const (vector_type,vector_type); return_value operator () const (vector_type,scalar_type); return_value operator () const (scalar_type,vector_type); }; -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
VIEWS ON THIS POST

95

Posted on:

Wednesday 7th November 2012
View Replies!

duplicate definitions with template member function

Why is the following code causing duplicate definitions (gcc 4.2) when including swapper.h in multiple files
VIEWS ON THIS POST

163

Posted on:

Wednesday 7th November 2012
View Replies!

For classes with static members

Can a class with out any instances present have it static members updated. If yes, on which object does the update happen. -Parag
VIEWS ON THIS POST

149

Posted on:

Wednesday 7th November 2012
View Replies!

How can I instantiate a class with it's name? read my text ahead...

Hi everybody I've a string that contains the name of a class. I want to instantiate from that class: string strategyNameStr; Strategy * strategy; // of the following classes are derived from Strategy. // and """ strategyNameStr """ contains the name of one of the following classes. // e.g. strategyNameStr = _T( "SimpleStrategy" ); SimpleStrategy * simStrategy; MiddleStrategy * midStrategy; HighStrategy * highStrategy; How can I use """ strategyNameStr """ in the next line... strategy = new or Is there at all any solution
VIEWS ON THIS POST

174

Posted on:

Wednesday 7th November 2012
View Replies!

Problem with user-defined IO streambuf for encoding purposes

Hi , I'm having some difficulty with the following piece of code. I have stripped it to it's bare minimum to demonstrate the problem at hand. Compiler: MS Visual C++ 2005 Express Edition (similar problem arises with 2008) Runtime Library: multi-threaded variants have been seen to fail [DLL/Static] | [Debug|Release] Purpose: define a user defined stream buffer that processes each incoming character and translates it to an encoded value. Place the encoded character into a local buffer for output. The most simple case would be an encoder that translates each character to upper case. A more complicated case would be an encoder that encodes plain-text to base64 encoding (this is not a one-to-one character encoding, it's a 3 character to 4character encoding, this is why an internal buffer is needed) Problem: The code throws an "Unhandled exception at 0x00529bcc in hsl_d.exe: 0xC0000005: Access violation reading location 0x00000000." after the "encoderbuf::underflow c = 51" character '3' has been read. This basically tells me that deep down in the internals of the IO library something is being dereferenced that is not allocated. Question(s): 1) Does this occur with other compilers (requires testing) 2) Is this a problem with the IO library (unlikely I think) 3) Am I doing something stupid (more than likely) And if so what References: C++ Standard Library - A Tutorial and Reference (13.13.3 User-Defined Stream Buffers) Code: #include #include class encoderbuf : public std::streambuf { char mCharBuf[128]; int mBufLen; int mBufPos; public: //-------------------------------------------------------------- /** default constructor */ encoderbuf() : std::streambuf() , mBufLen(0) , mBufPos(0) { } //-------------------------------------------------------------- /** outgoing data */ virtual int_type underflow () { int_type c = EOF; if (mBufPos < mBufLen) { c = mCharBuf[mBufPos++]; } std::cout
VIEWS ON THIS POST

419

Posted on:

Wednesday 7th November 2012
View Replies!

Some help with the compiler needed

, I have to ensure a bigger code base is functional when using BorlandC++. But I have several issues with the compiler (__BORLANDC__ equals 0x0582). Does someone have seen similar issues and knows how to work around these problems [thread local storage is not working in classes, causing an internal compiler error] ----------------------------------------------------------------------------------- // TLS problems : this will produce [C++ Fatal Error] Unit1.cpp(8): F1004 Internal compiler error at 0xa7b7103 with base 0xa720000 class A { static int __thread a; }; int __thread A::a; [template partial specialization in combination with overloading operator->() has problems] ------------------------------------------------------------------------------------------ template class B { public: B() { } _Tp b; }; template class B { public: _Tp operator->() { return b; } _Tp b; }; B BA; B BAPtr; // errors observed : // [C++ Error] Unit1.cpp(34): E2028 operator-> must return a pointer or a class // [C++ Error] Unit1.cpp(40): E2450 Undefined structure 'B' // [C++ Error] Unit1.cpp(40): E2449 Size of 'BAPtr' is unknown or zero // [C++ Error] Unit1.cpp(40): E2315 'B()' is not a member of 'B', because the type is not yet defined ['operator !=() const' prevents operator = from beeing autogenerated] --------------------------------------------------------------------- template class D { public: // remove const to circumvent compiler bug .... bool operator!=(T other) const { return other != t; } // removing struct also helps but my code is more complicated and I need it that way struct { T t; }; D() : t(0) { } }; D a; // compiler cannot generate deep copy operator here void foo() { D b; b = a; } these examples work well with other compilers (gcc,icc,msvc,suncc,armcc).
VIEWS ON THIS POST

189

Posted on:

Wednesday 7th November 2012
View Replies!

How to create two-column list box in Visual C++ without MFC?

I'm writing a chess game reviewer for Yahoo! players. This will be my first Visual C++ application. Right now the list box containing the moves in a history file looks like this: ___________ |Start |1. W e2-e4 |2. B e7-e5 |3. W g1-f3 |4. B b8-c6 |. |. |. I want to change it to this _____________ |_|White Black |0. ... ... |1.e2-e4 e7-e5 |2.g1-f3 b8-c6 |. |. |. and allow the user to select only a single move at a time. I've looked through the LB_ messages and LBS_ styles and have found no successful combination. I've also received a few suggestions from CodeGuru about ListView, multiple ListBoxes, and list control: "Use a listview control instead or even simpler, two listbox controls: one for 'White' and one for 'Black.'" "List view control is created using Windows registered class called SysListView32 and defined using WC_LISTVIEW macro." However, WC_LISTVIEW isn't included in my help file, I haven't found a web tutorial that includes the function SysListView32, and it isn't obvious how to join two listboxes in a way that the user can only select one entry. I'm thinking about joining three ListBoxes, but if I did, I would want a single scroll bar that would scroll each.
VIEWS ON THIS POST

169

Posted on:

Wednesday 7th November 2012
View Replies!

pure virtual function with unspecified parameters?

I have an abstract base class called Data. It has a pure virtual function virtual void write(std:stream& out) =0; which writes the internal data to a stream. Now the problem is that this is not appropriate for some implementations of the class. Some implementations have an internal representation that they should rather write into several separate files. So for those, something like this would be more appropriate: void write(const std::string& outputDirectoryName); What would be a good design for the abstract base class in this case It seems like it should require some kind of write function, but with flexible parameter lists for different implementations. Certainly we don't want to deal with void pointers: write(void* toWhatever) = 0; that the implementation classes would cast and use however they like because void pointers are evil. A template also doesn't work: template virtual void write(T& out) = 0; because you can't template a virtual function. And it would seem like overkill to template the whole class on this output parameter; after all it's just a little write function within a much larger class. What would be a good design in this case
VIEWS ON THIS POST

159

Posted on:

Wednesday 7th November 2012
View Replies!

[NEWBIE] Initialization of an array with a runtime determined size

Stefano Sabatini wrote: > And now another newbie question: how it is supposed to be initialized > the new array > > I mean, I can check that the new array will only contains zero values, > can I rely on this behaviour or is it somehow dependant on the compiler, > so that I should rather initialize it explicitly Yes. Usually memory will be initialised to zero in debug build configurations but left uninitialised otherwise. , Pete Becker wrote: > On 2007-12-03 08:13:32 -0500, Paul Sinnett > said: > >> Stefano Sabatini wrote: >>> And now another newbie question: how it is supposed to be initialized >>> the new array >>> >>> I mean, I can check that the new array will only contains zero values, >>> can I rely on this behaviour or is it somehow dependant on the compiler, >>> so that I should rather initialize it explicitly >> >> Yes. Usually memory will be initialised to zero in debug build >> configurations but left uninitialised otherwise. > > That's a truly bad idea. It means that code that checks for null > pointers in allocated data objects may work okay in debug build > configurations but fail mysteriously otherwise. Better to initialize to > some non-zero value, to avoid making uninitialized things look like > valid data. > Yes. Sorry. I meant non-zero. 0xCC and 0xCD seem to crop up a lot. But there's no standard for this as far as I'm aware. It's quite handy to know so you know what to look for in memory dumps. Anyway that's a side issue. The short answer is: yes, you should initialise the memory explicitly.
VIEWS ON THIS POST

146

Posted on:

Wednesday 7th November 2012
View Replies!

Pb with vista

, My Application can't run under vista,she is bis, and I go to have to spend time before finishing to change the paths using SHGetSpecialFolder()........Do not there is a trick for exemple install my application in an other directory tha "program files" Waiting I change all my code Thank you very much at all for your experiences.
VIEWS ON THIS POST

131

Posted on:

Wednesday 7th November 2012
View Replies!