[xiph-cvs] cvs commit: vorbis-tools/oggenc oggenc.c oggenc.dsp utf8.c

Michael Smith msmith at xiph.org
Tue Jul 24 05:00:16 PDT 2001



msmith      01/07/24 05:00:15

  Modified:    oggenc   oggenc.c oggenc.dsp utf8.c
  Log:
  Charset conversion for win32, thanks to Peter Harris.
  
  Oggenc/win32 now works properly again (or should, anyway).

Revision  Changes    Path
1.17      +2 -1      vorbis-tools/oggenc/oggenc.c

Index: oggenc.c
===================================================================
RCS file: /usr/local/cvsroot/vorbis-tools/oggenc/oggenc.c,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -r1.16 -r1.17
--- oggenc.c	2001/07/02 09:39:10	1.16
+++ oggenc.c	2001/07/24 12:00:15	1.17
@@ -283,7 +283,8 @@
                 " -s, --serial         Specify a serial number for the stream. If encoding\n"
                 "                      multiple files, this will be incremented for each\n"
                 "                      stream after the first.\n"
-		" -e, --encoding       Specify an encoding for the comments given.\n"
+		" -e, --encoding       Specify an encoding for the comments given (not\n"
+		"                      supported on windows)\n"
                 "\n"
                 " Naming:\n"
                 " -o, --output=fn      Write file to fn (only valid in single-file mode)\n"

1.3       +4 -0      vorbis-tools/oggenc/oggenc.dsp

Index: oggenc.dsp
===================================================================
RCS file: /usr/local/cvsroot/vorbis-tools/oggenc/oggenc.dsp,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- oggenc.dsp	2000/12/24 06:17:40	1.2
+++ oggenc.dsp	2001/07/24 12:00:15	1.3
@@ -87,6 +87,10 @@
 # PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
 # Begin Source File
 
+SOURCE=.\utf8.c
+# End Source File
+# Begin Source File
+
 SOURCE=.\audio.c
 # End Source File
 # Begin Source File

1.4       +52 -9     vorbis-tools/oggenc/utf8.c

Index: utf8.c
===================================================================
RCS file: /usr/local/cvsroot/vorbis-tools/oggenc/utf8.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- utf8.c	2001/07/08 03:09:46	1.3
+++ utf8.c	2001/07/24 12:00:15	1.4
@@ -12,12 +12,56 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include "utf8.h"
+
 
 #ifdef _WIN32
+#include <windows.h>
+
 int utf8_encode(const char *from, char **to, const char *encoding)
 {
-	fprintf(stderr, "Sorry, not implemented currently on win32\n");
-	return 1;
+	/* Thanks to Peter Harris <peter.harris at hummingbird.com> for this win32
+	 * code.
+	 *
+	 * We ignore 'encoding' and assume that the input is in the 'code page'
+	 * of the console. Reasonable, since oggenc is a console app.
+	 */
+
+	unsigned short *unicode;
+	int wchars, err;
+
+	wchars = MultiByteToWideChar(GetConsoleCP(), MB_PRECOMPOSED, from,
+			strlen(from), NULL, 0);
+
+	if(whars == 0)
+	{
+		fprintf(stderr, "Unicode translation error %d\n", GetLastError());
+		return 1;
+	}
+
+	unicode = calloc(wchars + 1, sizeof(unsigned short));
+	if(unicode == NULL) 
+	{
+		fprintf(stderr, "Out of memory processing string to UTF8\n");
+		return 1;
+	}
+
+	err = MultiByteToWideChar(GetConsoleCP(), MB_PRECOMPOSED, from, 
+			strlen(from), unicode, wchars);
+	if(err != wchars)
+	{
+		free(unicode);
+		fprintf(stderr, "Unicode translation error %d\n", GetLastError());
+		return 1;
+	}
+
+	/* On NT-based windows systems, we could use WideCharToMultiByte(), but 
+	 * MS doesn't actually have a consistent API across win32.
+	 */
+	*to = make_utf8_string(unicode);
+
+	free(unicode);
+	return 0;
 }
 
 #else /* End win32. Rest is for real operating systems */
@@ -27,7 +71,6 @@
 #include <errno.h>
 #endif
 
-#include "utf8.h"
 #include "charsetmap.h"
 
 #define BUFSIZE 256
@@ -157,18 +200,20 @@
         return NULL;
 }
 
+#endif /* The rest is used by everthing */
+
 char *make_utf8_string(const unsigned short *unicode)
 {
         int size = 0, index = 0, out_index = 0;
         unsigned char *out;
         unsigned short c;
 
-        /* first calculate the size of the target string */
+    /* first calculate the size of the target string */
         c = unicode[index++];
         while(c) {
                 if(c < 0x0080) {
                         size += 1;
-		} else if(c < 0x8000) {
+		} else if(c < 0x0800) {
                         size += 2;
                 } else {
                         size += 3;
@@ -176,7 +221,7 @@
                 c = unicode[index++];
         }	
 
-	out = malloc(size);
+	out = malloc(size + 1);
         index = 0;
 
         c = unicode[index++];
@@ -194,10 +239,8 @@
                 }
                 c = unicode[index++];
         }
-	out[out_index] = 0x0000;
+	out[out_index] = 0x00;
 
         return out;
 }
-
-#endif
 

--- >8 ----
List archives:  http://www.xiph.org/archives/
Ogg project homepage: http://www.xiph.org/ogg/
To unsubscribe from this list, send a message to 'cvs-request at xiph.org'
containing only the word 'unsubscribe' in the body.  No subject is needed.
Unsubscribe messages sent to the list will be ignored/filtered.



More information about the commits mailing list