c++ support for unicode, utf-8, encode/decode, ifstream, wstream?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RafaÅ‚ Maj Raf256

    #1

    c++ support for unicode, utf-8, encode/decode, ifstream, wstream?


    Hi,
    I have an UNICODE text file endcoded in UTF-8.

    I should store the UNICODE strings in my program for example in
    std::wstring right? To be able to work on them normally, so that
    std::wstring foo; foo[5] would mean 5-th _character_, and not 5-th
    byte of UNICODE encoded string.

    How do I read a text from UTF-8 file into std::wstring? I need to do
    some conversion right? from utf-8 to internal format used by
    std::wstring (probably UCS-2 or -4 right?)

    Also, how to save back the string, and how to manipulate it (like,
    replace 4-th character, just str[4]=(wchar)'x' ?)

    Thanks







  • TB

    #2
    Re: c++ support for unicode, utf-8, encode/decode, ifstream, wstream?

    Rafał Maj Raf256 sade:[color=blue]
    > Hi,
    > I have an UNICODE text file endcoded in UTF-8.
    >
    > I should store the UNICODE strings in my program for example in
    > std::wstring right? To be able to work on them normally, so that
    > std::wstring foo; foo[5] would mean 5-th _character_, and not 5-th
    > byte of UNICODE encoded string.
    >
    > How do I read a text from UTF-8 file into std::wstring? I need to do
    > some conversion right? from utf-8 to internal format used by
    > std::wstring (probably UCS-2 or -4 right?)
    >
    > Also, how to save back the string, and how to manipulate it (like,
    > replace 4-th character, just str[4]=(wchar)'x' ?)
    >[/color]

    Upon reading the UTF-8 data convert it internally to UTF-32 for
    easier parsing. The conversion process is quite easy to write.
    The problem with std::wstring is that it's templatized with
    wchar_t, and that primitive is at least on my machine only 2 bytes,
    and therefore not practical to use with unicode (unless you actually
    wish to use the abnormal UTF-16 variant in such a case).

    --
    TB @ SWEDEN

    Comment

    • RafaÅ‚ Maj Raf256

      #3
      Re: c++ support for unicode, utf-8, encode/decode, ifstream, wstream?

      TB wrote:
      [color=blue]
      > Upon reading the UTF-8 data convert it internally to UTF-32 for
      > easier parsing.[/color]

      How? Arent there ready to use functions/classes doing that? In std,
      perhaps in boost?
      [color=blue]
      > The conversion process is quite easy to write.[/color]
      [color=blue]
      > The problem with std::wstring is that it's templatized with
      > wchar_t, and that primitive is at least on my machine only 2 bytes,
      > and therefore not practical to use with unicode (unless you actually
      > wish to use the abnormal UTF-16 variant in such a case).[/color]

      Hm.. so which class is best to store any-language text string then?



      Comment

      • P.J. Plauger

        #4
        Re: c++ support for unicode, utf-8, encode/decode, ifstream, wstream?

        "Rafal Maj Raf256" <use.www.to.con tact.me@raf256. com.invalid> wrote in
        message news:dqqid4$dn6 $1@inews.gazeta .pl...
        [color=blue]
        > TB wrote:
        >[color=green]
        >> Upon reading the UTF-8 data convert it internally to UTF-32 for
        >> easier parsing.[/color]
        >
        > How? Arent there ready to use functions/classes doing that? In std,
        > perhaps in boost?[/color]

        You'll find a few codecvt facets (the critters you need) in various places,
        but for a complete set of all that you're likely to need -- ready made,
        tested, and supported -- see our CoreX library.
        [color=blue][color=green]
        >> The conversion process is quite easy to write.[/color][/color]

        No it isn't. At least not correctly and robustly.
        [color=blue][color=green]
        >> The problem with std::wstring is that it's templatized with
        >> wchar_t, and that primitive is at least on my machine only 2 bytes,
        >> and therefore not practical to use with unicode (unless you actually
        >> wish to use the abnormal UTF-16 variant in such a case).[/color]
        >
        > Hm.. so which class is best to store any-language text string then?[/color]

        Depends on your goals. In truth and reality, you can still get away quite
        nicely with UCS-2. Effectively, you ignore the exotic characters with
        code values above 0xffff more recently added. Your input converter
        then treats as erroneous any UTF-8 sequence that specifies a code
        value that's too big. But if you feel the need to support the complete
        Unicode set in its current form, you need to convert UTF-8 to UTF-16
        internally, and accept the fact that characters can occupy either one or
        two storage elements. Whatever your choice, CoreX has the
        conversion tools you need to carry it out.

        P.J. Plauger
        Dinkumware, Ltd.



        Comment

        • TB

          #5
          Re: c++ support for unicode, utf-8, encode/decode, ifstream, wstream?

          Rafał Maj Raf256 sade:[color=blue]
          > TB wrote:
          >[color=green]
          >> Upon reading the UTF-8 data convert it internally to UTF-32 for
          >> easier parsing.[/color]
          >
          > How? Arent there ready to use functions/classes doing that? In std,
          > perhaps in boost?
          >[color=green]
          >> The conversion process is quite easy to write.[/color]
          >[color=green]
          >> The problem with std::wstring is that it's templatized with
          >> wchar_t, and that primitive is at least on my machine only 2 bytes,
          >> and therefore not practical to use with unicode (unless you actually
          >> wish to use the abnormal UTF-16 variant in such a case).[/color]
          >
          > Hm.. so which class is best to store any-language text string then?[/color]

          If 'unsigned int' is 4 bytes on your machine, write a unicode
          implementation based on that primitive, or use an already available
          framework; hm, perhaps this 'CoreX'-thingy advocated by P.J.

          --
          TB @ SWEDEN

          Comment

          • P.J. Plauger

            #6
            Re: c++ support for unicode, utf-8, encode/decode, ifstream, wstream?

            "TB" <TB@void.com> wrote in message
            news:43d10b59$0 $8259$6d36acad@ taz.nntpserver. com...
            [color=blue]
            > Rafal Maj Raf256 sade:[color=green]
            >> TB wrote:
            >>[color=darkred]
            >>> Upon reading the UTF-8 data convert it internally to UTF-32 for
            >>> easier parsing.[/color]
            >>
            >> How? Arent there ready to use functions/classes doing that? In std,
            >> perhaps in boost?
            >>[color=darkred]
            >>> The conversion process is quite easy to write.[/color]
            >>[color=darkred]
            >>> The problem with std::wstring is that it's templatized with
            >>> wchar_t, and that primitive is at least on my machine only 2 bytes,
            >>> and therefore not practical to use with unicode (unless you actually
            >>> wish to use the abnormal UTF-16 variant in such a case).[/color]
            >>
            >> Hm.. so which class is best to store any-language text string then?[/color]
            >
            > If 'unsigned int' is 4 bytes on your machine, write a unicode
            > implementation based on that primitive, or use an already available
            > framework; hm, perhaps this 'CoreX'-thingy advocated by P.J.[/color]

            Yep. It includes UTF-8 to UCS-4 too. And it's templatized on the
            internal character type. Forgot to mention that.

            P.J. Plauger
            Dinkumware, Ltd.



            Comment

            • JustBoo

              #7
              Re: c++ support for unicode, utf-8, encode/decode, ifstream, wstream?

              On Fri, 20 Jan 2006 17:13:37 +0100, TB <TB@void.com> wrote:
              [color=blue]
              >If 'unsigned int' is 4 bytes on your machine, write a unicode
              >implementati on based on that primitive, or use an already available
              >framework; hm, perhaps this 'CoreX'-thingy advocated by P.J.[/color]

              Oh, it isn't just advocated by Mr. Plauger it's SOLD ($) by Mr.
              Plauger. Bit of a difference I think.

              "If you have ten thousand regulations you destroy
              all respect for the law." - Winston Churchill

              Comment

              • P.J. Plauger

                #8
                Re: c++ support for unicode, utf-8, encode/decode, ifstream, wstream?

                "JustBoo" <JustBoo@BooWho .com> wrote in message
                news:e555t1l3uu rqubc54k6a13n37 nqs5rnumi@4ax.c om...
                [color=blue]
                > On Fri, 20 Jan 2006 17:13:37 +0100, TB <TB@void.com> wrote:
                >[color=green]
                >>If 'unsigned int' is 4 bytes on your machine, write a unicode
                >>implementatio n based on that primitive, or use an already available
                >>framework; hm, perhaps this 'CoreX'-thingy advocated by P.J.[/color]
                >
                > Oh, it isn't just advocated by Mr. Plauger it's SOLD ($) by Mr.
                > Plauger. Bit of a difference I think.[/color]

                Really? In what way? I certainly *advocate* using an already
                available framework, as did TB. If you can get a free one that
                does the job (and it's still sufficiently "free" after you locate
                it, download it, figure out how to build it, integrate it into
                your product, deal with the surprises, and test it to your
                satisfaction) by all means do so. I also *advocate* using CoreX,
                if you're sufficiently professional that USD 90 is cheaper than
                the above parenthetical exercise costs you in your time and
                peace of mind.

                But if you think I *advocate* something just because I make
                ninety bucks off it, then by all means avoid anything that's
                $OLD and stick with open sour¢e. Just don't measure me by
                your standards.

                P.J. Plauger
                Dinkumware, Ltd.



                Comment

                • JustBoo

                  #9
                  Re: c++ support for unicode, utf-8, encode/decode, ifstream, wstream?

                  On Sat, 21 Jan 2006 18:32:22 -0500, "P.J. Plauger"
                  <pjp@dinkumware .com> wrote:[color=blue]
                  >"JustBoo" <JustBoo@BooWho .com> wrote in message
                  >news:e555t1l3u urqubc54k6a13n3 7nqs5rnumi@4ax. com...[color=green]
                  >> Oh, it isn't just advocated by Mr. Plauger it's SOLD ($) by Mr.
                  >> Plauger. Bit of a difference I think.[/color][/color]
                  [color=blue]
                  >Really? In what way? I certainly *advocate* using an already
                  >available framework, as did TB.[/color]

                  In what way? Well, from the simple *fact* you make money selling
                  your products here. I make that the point of this paragraph. Can you
                  deny that? It is a fact. Leave emotion out of it. Leave capitalism out
                  of it. Leave your perception that this is an insult out of it, and all
                  the rest. You sell your products here. I could truly *careless*
                  whether you do or not. But you do. Please do not enumerate all the
                  good you do for the free and not-so-free world by doing this. You sell
                  them here on a consistent basis. Period. As the sun comes up every
                  morning, it's just the obvious truth. Now please pay attention; in the
                  *context* of this thread, I thought it important to point this out to
                  the poster. That's it.

                  Before getting your hackles up please read further.
                  [color=blue]
                  >If you can get a free one that
                  >does the job (and it's still sufficiently "free" after you locate
                  >it, download it, figure out how to build it, integrate it into
                  >your product, deal with the surprises, and test it to your
                  >satisfaction ) by all means do so. I also *advocate* using CoreX,
                  >if you're sufficiently professional that USD 90 is cheaper than
                  >the above parenthetical exercise costs you in your time and
                  >peace of mind.[/color]

                  Fairly ironic that. I'm certain you don't remember, but *I have
                  recommended* people look at your products on a regular basis, in
                  this ng and others. Even in the real world. And ready for this, I have
                  used precisely the exact same logic to justify the recommendation
                  when *attacked* for doing so. That is the very definition of irony.

                  [Note: I usually leave out the snarky remark about "sufficient ly
                  professional" though.] :-)
                  [color=blue]
                  >But if you think I *advocate* something just because I make
                  >ninety bucks off it, then by all means avoid anything that's
                  >$OLD and stick with open sour¢e.[/color]

                  Wow, an ocean's worth of assumption and presumptions to boot. You
                  think me a socialist? Bwha. I'm a stone-cold capitalist. You've
                  assumed far too much. <chuckling> You've read far too much into my
                  simple statement of fact.

                  And yes, I do think you advocate it because you make money from it.
                  Welcome to the commerce of the human race. It's just human nature.
                  I'll leave it up to you to decide if that is an insult or not.

                  Trend your own posts, seriously. Look at what you respond to and what
                  you always recommend. I believe you to be of a scientific mentality
                  and if you are honest with yourself you will see truth. Noting more
                  nothing less.

                  And in the end, so what. As I'm sure one of your arguments would/will
                  be: people are free to buy it or not, and you're making them aware of
                  its existence. And there you have it. Try to read this post without
                  emotion and perhaps you'll see my intent.
                  [color=blue]
                  >Just don't measure me by
                  >your standards.[/color]
                  [Insult acknowledged but not accepted; like a refused package]

                  Once again, you assume far too much. Especially given that I simply
                  pointed out that you sell products, which is true. Does being a
                  capitalist bother you? Guilt perhaps? Note those are questions, not
                  assumptions.

                  "I didn't fight my way to the top of the food chain to be a
                  vegetarian."

                  Have a *prosperous* week. :-)

                  Comment

                  • P.J. Plauger

                    #10
                    Re: c++ support for unicode, utf-8, encode/decode, ifstream, wstream?

                    "JustBoo" <JustBoo@BooWho .com> wrote in message
                    news:h697t1t480 gasjij19k4ls138 pdgjjrb9i@4ax.c om...
                    [color=blue]
                    > On Sat, 21 Jan 2006 18:32:22 -0500, "P.J. Plauger"
                    > <pjp@dinkumware .com> wrote:[color=green]
                    >>"JustBoo" <JustBoo@BooWho .com> wrote in message
                    >>news:e555t1l3 uurqubc54k6a13n 37nqs5rnumi@4ax .com...[color=darkred]
                    >>> Oh, it isn't just advocated by Mr. Plauger it's SOLD ($) by Mr.
                    >>> Plauger. Bit of a difference I think.[/color][/color]
                    >[color=green]
                    >>Really? In what way? I certainly *advocate* using an already
                    >>available framework, as did TB.[/color]
                    >
                    > In what way? Well, from the simple *fact* you make money selling
                    > your products here. I make that the point of this paragraph. Can you
                    > deny that?[/color]

                    Uh, no.
                    [color=blue]
                    > [extensive rant elided][/color]

                    Got it. Now chill out.

                    P.J. Plauger
                    Dinkumware, Ltd.



                    Comment

                    • JustBoo

                      #11
                      Re: c++ support for unicode, utf-8, encode/decode, ifstream, wstream?

                      On Sun, 22 Jan 2006 12:13:04 -0500, "P.J. Plauger"
                      <pjp@dinkumware .com> wrote:
                      [color=blue][color=green]
                      >> [extensive rant elided][/color]
                      >
                      >Got it. Now chill out.[/color]

                      Extensive rant? Wow. obviously the "voice" I wrote the post with and
                      the "voice" you read it with were in two different universes.

                      I said repeatedly "without emotion." To me that means even and calm.
                      But...

                      Boo to airline stewardess on Plauger Air,
                      "Miss, may I have a pillow?"

                      Stewardess: "AHHHH! AHHH, STOP SCREAMING
                      AT ME YOU ANGRY, BITTER, MEAN, ANGRY,
                      UNREASONABLE COMMIE, SOCIALIST,
                      BASTARD! AHHHHH, AHHHHHH!."

                      <sigh> All righty then....

                      Men occasionally stumble over the truth, but most
                      of them pick themselves up and hurry off as if
                      nothing had happened. - Winston Churchill

                      Once again, Have a *prosperous* week.
                      I'm out for some fun in the sun. :-)

                      Comment

                      • P.J. Plauger

                        #12
                        Re: c++ support for unicode, utf-8, encode/decode, ifstream, wstream?

                        "JustBoo" <JustBoo@BooWho .com> wrote in message
                        news:o5g7t198c4 o9p6i7143shlgrc dhun2a85p@4ax.c om...
                        [color=blue]
                        > On Sun, 22 Jan 2006 12:13:04 -0500, "P.J. Plauger"
                        > <pjp@dinkumware .com> wrote:
                        >[color=green][color=darkred]
                        >>> [extensive rant elided][/color]
                        >>
                        >>Got it. Now chill out.[/color]
                        >
                        > Extensive rant? Wow. obviously the "voice" I wrote the post with and
                        > the "voice" you read it with were in two different universes.
                        >
                        > I said repeatedly "without emotion." To me that means even and calm.
                        > But...[/color]

                        Yes, you said many things, but it's clear that you are
                        intellectually dishonest. I stay in "dialogs" like this
                        only when I feel a need to educate the lurkers, not in
                        any hopes of finding common ground. Since I have no
                        sermon to preach in this case, I see little reason to
                        feed this particular troll.

                        P.J. Plauger
                        Dinkumware, Ltd.



                        Comment

                        • JustBoo

                          #13
                          Re: c++ support for unicode, utf-8, encode/decode, ifstream, wstream?

                          On Sun, 22 Jan 2006 14:11:07 -0500, "P.J. Plauger"
                          <pjp@dinkumware .com> wrote:[color=blue]
                          >"JustBoo" <JustBoo@BooWho .com> wrote in message
                          >news:o5g7t198c 4o9p6i7143shlgr cdhun2a85p@4ax. com...[/color]
                          [color=blue][color=green]
                          >> Extensive rant? Wow. obviously the "voice" I wrote the post with and
                          >> the "voice" you read it with were in two different universes.[/color][/color]

                          Well, I now "see" what voice you hear. It seems obvious to me you have
                          quite literally no sense-of-humor. I mean you literally have no
                          ability to sense when humor is being applied to a situation. I now
                          have no doubt when someone makes a witty/funny remark in a meeting
                          you're that guy that sits there stone-faced because you just don't get
                          it.

                          If I make a "thumbs-up" gesture and say how many fingers am I holding
                          up you're the guy that has to launch into a deadly-dull bone-dry
                          dissertation about why thumbs aren't fingers and blah, blah... yawn,
                          blah.... Wow.

                          The airline joke (or an attempt at one :-) ) was so over the top that
                          I figured *anyone* with a social IQ above a warm doorknob would
                          get it. Disappointed again I see. And yes, that is an insult. YOU
                          insulted me numerous times in the past posts and I did *not*
                          reciprocate. I left them unanswered. Think about that.

                          "I like a man who grins when he fights." - Winston Churchill
                          [color=blue][color=green]
                          >> I said repeatedly "without emotion." To me that means even and calm.
                          >> But...[/color]
                          >
                          >Yes, you said many things, but it's clear that you are
                          >intellectual ly dishonest. I stay in "dialogs" like this
                          >only when I feel a need to educate the lurkers, not in
                          >any hopes of finding common ground.[/color]

                          Gah! Where the heck did that come from? Oh, right, no ability to sense
                          humor. But there seems to be more... hmm, let's see. Admitted to
                          hawking wares, okay, but he feels "a need to educate the lurkers."
                          Hmm, he knows he has good products so should not be so insecure that
                          he has to defend that at every turn and that was not even brought up
                          in this thread, (but I am probably wrong about that...) but… oh no!
                          Say isn't so. :-(

                          Well, I think he has decided he is the Alpha Male of this (his?)
                          newsgroup and needs to prove to his underlings, lackeys and lurkers
                          that he will have the ego-driven Last Word(tm)(r), by gawd! Yes, it's
                          Good To Be King.

                          Man, that explains so much of the behavior, tone and tenor of the
                          petty back-biting Superior Sheep in this group. The lesser
                          monkey-sheep are emulating The Alpha Males behavior. Much of it to
                          curry favor and not be "chewed on" by the emulating monkey-sheep.
                          Ding! Because after all, they are so superior to the sub-creatures
                          that will not submit.

                          Man, what a small tiny vacuous life to live. To each his own.

                          "To perceive is to suffer." - Aristotle
                          [color=blue]
                          >Since I have no
                          >sermon to preach in this case, I see little reason to
                          >feed this particular troll.[/color]

                          And you call me *intellectually * dishonest? Bwha. Look in the mirror,
                          pal.

                          Life should NOT be a journey to the grave with the intention of
                          arriving safely in a well preserved body, but to skid in sideways,
                          champagne in one hand, chocolate in the other, body thoroughly
                          used up and worn out, screaming "WOO HOO BOO- What a Ride!"
                          - Terry Pratchett

                          Comment

                          Working...