Encoding Encoder::getFileEncoding(const string &filename) { try { fstream fs; fs.exceptions(fstream::failbit | fstream::badbit); fs.open(filename, ios_base::in | ios_base::binary); fs.seekg(0, fs.end); int fileLenght = fs.tellg(); fs.seekg(0, fs.beg); if(fileLenght < 2) { fs.close(); return nobom; } const int BOMLEN = min(fileLenght, 4); // reserve buffer ByteArray bom( new Byte[BOMLEN], default_delete<Byte[]>()); // read file to buffer fs.read((char*)bom.get(), BOMLEN); fs.close(); static const char *UTF_16_BE_BOM = "\xFE\xFF"; static const char *UTF_16_LE_BOM = "\xFF\xFE"; static const char *UTF_8_BOM = "\xEF\xBB\xBF"; static const char *UTF_32_BE_BOM = "\x00\x00\xFE\xFF"; static const char *UTF_32_LE_BOM = "\xFF\xFE\x00\x00"; if (memcmp(bom.get(), UTF_8_BOM, 3) == 0) return u8; else if (memcmp(bom.get(), UTF_32_LE_BOM, 4) == 0) return u32le; else if (memcmp(bom.get(), UTF_32_BE_BOM, 4) == 0) return u32be; else if (memcmp(bom.get(), UTF_16_LE_BOM, 2) == 0) return u16le; else if (memcmp(bom.get(), UTF_16_BE_BOM, 2) == 0) return u16be; return nobom; } catch(const exception& e ) { throw MyEx("Encoder::getFileEncoding() Can't open " + filename + ".\n" + string(e.what())); } }
int64_t fact(int n) { int one = 1; fprintf(stderr,"--fact arg: %p stackvar: %p\n", &n, &one ); if ( n == 1 ) { fprintf(stderr,"Throwing exception\n"); throw MyEx(1); return 1; } return n * fact( n - 1 ); }