// program recursively renames files // from ANY to ASCII latin letters // for Windows XP compiler MS C++ ver 15 // Oleg Mazonka 2009 #include #include #include #include #include #include #include #include #include #include using std::map; using std::cout; using std::cerr; using std::vector; using std::string; using std::wstring; namespace g{ bool dryrun = true; bool test=false; map table; } // g namespace wdir{ typedef std::map > msul; msul read(); bool setd(wstring s); wstring getd(); bool rename(wstring oldname, wstring newname); class in{ wstring cwd; public: in(wstring s){ cwd=getd(); if( !setd(s) ) throw wstring()+L"cannot get into [" + s +L"]"; } ~in(){ if( !setd(cwd) ) throw wstring() + L"cannot return to "+cwd; } }; } //wdir std::wstring s2w(const std::string& s) { std::wstring temp(s.length(),L' '); std::copy(s.begin(), s.end(), temp.begin()); return temp; } std::string w2s(const std::wstring& s) { std::string temp(s.length(), ' '); for( size_t i=0; i populate(); int main(int ac, char *av[]) { try{ g::table = populate(); wstring cwd = wdir::getd(); if( ac==2 && string(av[1])=="test" ) g::test = true; else if( ac==2 ) { cwd = s2w(av[1]); } runDir(cwd); if( g::test ) return 0; g::dryrun = false; runDir(cwd); }catch(string s) { cerr<<"Error: "< 1000000L ) return L""; size*=2; p = new wchar_t[size]; r = _wgetcwd(p,size); } wstring rr(r); delete[] p; return rr; } wdir::msul wdir::read() { msul r; _wfinddata_t f; long handle; if ( (handle = _wfindfirst(L"*",&f) ) != -1L ) do { std::pair atr; atr.first = f.time_write; if( !(f.attrib & _A_SUBDIR) ) atr.second = f.size; else atr.second = -1L; wstring name(f.name); if( name != L"." && name != L".." ) r[name] = atr; }while( _wfindnext(handle,&f) != -1 ); _findclose(handle); return r; } bool isfile(wstring f); wstring convert(wstring s); void runDir(wstring d) { wdir::in in(d); //cout<<"Directory: "<first); if( i->second.second == -1L ) { runDir(name); } wstring newname = convert(name); if( name == newname ) { continue; } cout<<"["< ["<::iterator it = g::table.find(code); if( it != g::table.end() ) { r += s2w(it->second); continue; } if( code <= upper ) { r += s[i]; continue; } good = false; r += s2w("&#"); r += s2w(tos(int(s[i]))); r += s2w(";"); } if( good ) return r; throw wstring()+L"Codes are not in the table\n"+r; } map populate() { map r; string fname = "wchar2lat.map"; std::ifstream in(fname.c_str()); if( !in ) throw "Cannot open "+fname; while(1) { int c; string s; in>>c>>s; if( !in ) break; r[c] = s; } return r; }