UTF-16 reading and writing with C

It seems almost no one does this kind of stuff with C, it was very hard to find material for this on the net. So I thought of posting it so that everyone can benefit. The code is for an XML/HTML tag stripper, which strips all tags. Make sure you keep the error checker intact, it spits out some uncommon errors that at first glance you won’t know what it’s talking about.

A major plus point in using “ccs=UTF16[BE|LE]” is that it handles endianness and BOM automatically. But you have to know what you are dealing with, I *think* “ccs=UTF16” has Native Endianness, which means LE for majority of PCs that are in use the files I got to strip were BE and LE mixed so I had to check BOM manually; 0xFF 0xFE is Little Endian.

Note: This code was only tested on Linux, it should work without any changes on any POSIX system. Windows is a different story as always it may need some changes in “ccs=…” part I saw in some material UTF-16BE/UTF-16LE has been used, but it didn’t work on Linux that way if this code doesn’t work on Windows you might have to change to that.

#include <stdio.h>
#include <wchar.h>
#include <errno.h>
#include <string.h>

int main(int argc, char *argv[])
{
	int i;
	for (i=1;i<argc;i++){
		printf("%s\n",argv[i]);
		FILE *o=fopen(argv[i],"r");
		FILE *f;
		if (fgetc(o)!=0xFF){
			f=fopen(argv[i],"r,ccs=UTF16BE");	
		} else {
			f=fopen(argv[i],"r,ccs=UTF16LE");

		}
		fclose(o);
		char new[32];
		sprintf(new,"./striped/%s.txt",argv[i]);
		FILE *g=fopen(new,"w,ccs=UTF16LE");

		int write=1;
		wint_t c=fgetwc(f);
		while (c!=WEOF) {
			if (c==L'<')
				write=0;

			if (write){
				fputwc(c,g);
			}
			if (c==L'>')
				write=1;

			c=fgetwc(f);

		}
		printf("%s\n",strerror(errno));
		fclose(g);
		fclose(f);

	}
	return 0;
}
Advertisements

Unix toolset for Windows

If you need a Unix toolset on Windows install these packages on Windows and you’ll be having a Unix toolset and a more Unixy environment for Free!(most tools listed are FOSS ones)

MinGW

Needs no introduction, you know what it does. Just make sure you get “mingw-get” or its GUI installer it’s a hell of a deal that does the installation of MinGW in package manager style(the biggest thing you’ll ever miss when building stuff with dependencies you don’t even have a clue how to build successfully). It doesn’t include pkg-config, you can find a build for Windows if you know how to use google. I’ve tried to install zsh on MinGW some headers were lacking and adding cygwin’s version didn’t solve anything because it can’t make use of MinGW style paths.

ConEmu

Best alternative with split console windows.

Dr Memory

Valgrind like memory debugger for Windows(more goodies here).

AutoHotKey(freeware)

Bind a hotkey to console, we just need so many consoles.

qtcreator with cmake

You don’t need the Qt libraries if you aren’t going do a Qt project, but you can still use qtcreator, with cmake or make. You can link MinGW install to qtcreator with relative ease. And its best to make cmake from source and have it in MinGW other than having it installed on Windows, this makes cmake find the dependencies from MinGW distro.