Du bist nicht angemeldet.

Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!

Werbeanzeige

Anonymous

unregistriert

1

05.01.2006, 09:34

Für die FAQ: Der uni_cast<A, T>

Hi,

nach stundenlangem Rumprobieren (okay, eigentlich ne halbe Minute), grauenvollem Verzweifeln (na ja, ich muss ja auch irgendwann mal ausm Bett rauskommen) und fehlendem Kaffeekonsum (Blöde Kaffeemaschine ist kaputt)

Hier der neue Cast aus dem Hause Patrick!

C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <string>
#include <windows.h>

template<typename A, typename T> inline ::std::basic_string<A> uni_cast (const T* source) 
{ 
    std::cout << "uni_cast <T, T>" << std::endl;

    return (source);
} 

template<> inline ::std::basic_string<char> uni_cast (const wchar_t* source) 
{ 
    std::cout << "uni_cast <char, wchar_t>" << std::endl;

    char *buffer = new char[::wcslen (source)*2];

    ::WideCharToMultiByte (CP_ACP, 0, source, -1, buffer, static_cast<int>(::wcslen (source)*2), NULL, NULL); 
 
    std::basic_string<char> result (buffer);

    delete [] buffer;

    return (result);
} 

template<> inline ::std::basic_string<wchar_t> uni_cast (const char* source) 
{ 
    std::cout << "uni_cast <wchar_t, char>" << std::endl;
    wchar_t *buffer = new wchar_t[::strlen (source)];

    ::MultiByteToWideChar (CP_ACP, 0, source, -1, buffer, static_cast<int>(::strlen (source))); 
 
    std::basic_string<wchar_t> result (buffer);

    delete [] buffer;

    return (result);
}

int main (void)
{
    std::basic_string<wchar_t>  test    = uni_cast<wchar_t>("Hallo!");  // ANSI zu Unicode

    std::basic_string<char>     test2   = uni_cast<char>(L"Hallo!");    // Unicode zu ANSI

    std::basic_string<char>     test3   = uni_cast<char>("Hallo!");     // ANSI zu ANSI

    std::basic_string<wchar_t>  test4   = uni_cast<wchar_t>(L"Hallo!"); // Unicode zu Unicode


    std::wcout << test << std::endl;
    std::cout << test2 << std::endl;
    std::cout << test3 << std::endl;
    std::wcout << test4 << std::endl;

    std::cin.get();
    return 0;
}


Try it, use it, believe it ;)

koschka

Community-Fossil

Beiträge: 2 862

Wohnort: Dresden

Beruf: Student

  • Private Nachricht senden

2

05.01.2006, 11:22

naja.... die Konsolenausgabe... in den Funktionen.... aber das kann man ja einfach mal rausschmeissen.

Ansonsten aber sehr gut ^^

Anonymous

unregistriert

3

05.01.2006, 11:23

koschka
Die ist doch nur zur Verdeutlichung da, dass andere sehen wie er funktioniert. Den lass ich doch nicht freiwillig da drin ;)

Sicaine

unregistriert

4

06.01.2006, 08:54

Zitat von »"nix da"«

koschka
Die ist doch nur zur Verdeutlichung da, dass andere sehen wie er funktioniert. Den lass ich doch nicht freiwillig da drin ;)


Naja wert weiß ;-)

5

03.12.2007, 18:16

bäääh ... wenigstens nen schöne plattformunabhängige Lösung über std::codecvt wäre wünschenswert gewesen (ok über std::ctype ginge notfalls auch ... deprected) ...
Devil Entertainment :: Your education is our inspiration
Der Spieleprogrammierer :: Community Magazin
Merlin - A Legend awakes :: You are a dedicated C++ (DirectX) programmer and you have ability to work in a team? Contact us!
Siedler II.5 RttR :: The old settlers-style is comming back!

Also known as (D)Evil

David_pb

Community-Fossil

Beiträge: 3 886

Beruf: 3D Graphics Programmer

  • Private Nachricht senden

6

03.12.2007, 18:55

Zitat von »"Deviloper"«

bäääh ... wenigstens nen schöne plattformunabhängige Lösung über std::codecvt wäre wünschenswert gewesen (ok über std::ctype ginge notfalls auch ... deprected) ...


Das kommt dir aber früh! ;)
@D13_Dreinig

7

03.12.2007, 19:41

@Deviloper mach doch ne überarbeitete Version und poste sie hier!
Das Böse ist des Menschensbeste Kraft - Friedrich Nietzsche

8

03.12.2007, 19:50

Bin mir dabei noch nicht 100% sicher (dumme local's :P )

C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <string>
#include <local>
#include <vector>

template<class dest_type, class source_type>
inline const std::basic_string<dest_type> char_cast(const std::basic_string<source_type>& source, const std::locale& loc = std::locale::classic())
{
    std::vector<dest_type> result (source.length(), 0); 
    mbstate_t state;
    const source_type* ptr_source_next;
    dest_type* ptr_dest_next; 
    std::use_facet<std::codecvt<dest_type, source_type, mbstate_t> >(loc).in(state, &source[0], &source[0] + source.length(), 
        ptr_source_next, &result[0], &result[0] + result.size(), ptr_dest_next);
    return std::basic_string<dest_type>(result.begin(), result.end());
}

template<>
inline const std::basic_string<char> char_cast(const std::basic_string<char>& source, const std::locale&)
{ return source; }

template<>
inline const std::basic_string<wchar_t> char_cast(const std::basic_string<wchar_t>& source, const std::locale&)
{ return source; }
sollte für's erste gehen ... bin mir aber wie gesagt noch nicht 100% sicher ...
Devil Entertainment :: Your education is our inspiration
Der Spieleprogrammierer :: Community Magazin
Merlin - A Legend awakes :: You are a dedicated C++ (DirectX) programmer and you have ability to work in a team? Contact us!
Siedler II.5 RttR :: The old settlers-style is comming back!

Also known as (D)Evil

Werbeanzeige