Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.0 KiB

  1. /////////////////////////////////////////////////////////////////////
  2. // DBconvert - A simple command line tool to convert between the two
  3. // prompt database formats.
  4. /////////////////////////////////////////////////////////// SS //////
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <atlbase.h>
  8. #include <wchar.h>
  9. #include "msprompteng.h"
  10. #include "msprompteng_i.c"
  11. void Error(const WCHAR* msg)
  12. {
  13. wprintf(L"ERROR - %s\n", msg);
  14. }
  15. // TODO: should use multibyte text convert.
  16. // a quick hack to convert commandline arguments to WCHAR's.
  17. WCHAR* PlainToWide(const char* str)
  18. {
  19. WCHAR* tmp;
  20. unsigned int i;
  21. tmp = (WCHAR*) malloc(sizeof(WCHAR) * (strlen(str) + 1));
  22. for (i = 0; i < strlen(str); i++)
  23. tmp[i] = (WCHAR) str[i];
  24. tmp[i] = 0;
  25. return tmp;
  26. }
  27. bool FileExists(WCHAR* filename)
  28. {
  29. FILE* fp;
  30. fp = _wfopen(filename, L"rb");
  31. if (fp) {
  32. fclose(fp);
  33. return true;
  34. }
  35. return false;
  36. }
  37. void main(int argc, char** argv)
  38. {
  39. IPromptDb* db;
  40. WCHAR* source;
  41. WCHAR* destination;
  42. HRESULT hr = E_FAIL;
  43. int f_convertToNew = 0;
  44. int f_convertToOld = 0;
  45. if (argc == 4) {
  46. // process command line arguments
  47. f_convertToNew = (0 == strcmp("-new", argv[1]));
  48. f_convertToOld = (0 == strcmp("-old", argv[1]));
  49. source = PlainToWide(argv[2]);
  50. destination = PlainToWide(argv[3]);
  51. // check for file existence
  52. if (! FileExists(source)) {
  53. wprintf(L"ERROR - %s doesn't exist\n", source);
  54. } else if (FileExists(destination)) {
  55. wprintf(L"ERROR - %s exists\n", destination);
  56. } else {
  57. if (f_convertToNew || f_convertToOld) {
  58. wprintf(L"Prompt Engine Database Conversion ");
  59. if (SUCCEEDED(::CoInitialize(NULL))) {
  60. if (SUCCEEDED(::CoCreateInstance(CLSID_PromptDb, NULL, CLSCTX_ALL, IID_IPromptDb, (void**) &db))) {
  61. // TODO: I should use DB_LOAD_WITH_WRITE defined in CPromptDb.
  62. // I use IPromptDb directly instead of MsPromptEng, and so some enums are invisible
  63. // with simply including promptdb.h .
  64. if (SUCCEEDED(db->AddDb(L"DEFAULT", source, 2 /* = DB_LOAD_WITH_WRITE */))) { // read database
  65. db->ActivateDbName(L"DEFAULT");
  66. wprintf(L"%s -> %s ", source, destination);
  67. if (f_convertToNew)
  68. hr = db->UpdateDb(destination); // save to new format
  69. else
  70. hr = db->UpdateDbOldFormat(destination); // save to old format
  71. if (SUCCEEDED(hr))
  72. wprintf(L"success\n");
  73. else
  74. wprintf(L"failure\n");
  75. } else {
  76. Error(L"bad database format");
  77. }
  78. db->Release();
  79. } else {
  80. Error(L"cannot load PromptDb COM object");
  81. }
  82. ::CoUninitialize();
  83. } else {
  84. Error(L"cannot initialize COM library");
  85. }
  86. } else {
  87. printf("ERROR - illegal switch \"%s\". use \"-new\" or \"-old\".\n", argv[1]);
  88. }
  89. }
  90. free(source);
  91. free(destination);
  92. } else {
  93. // help message
  94. wprintf(L"DBconvert usage:\n");
  95. wprintf(L"dbconvert -new <source (new/old)> <destination (new)>\n");
  96. wprintf(L"dbconvert -old <source (new/old)> <destination (old)>\n");
  97. }
  98. }