util.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "stdafx.h"
  14. #include "util.hpp"
  15. #define INIFILE ".\\treeview.ini"
  16. void showFIOErr(LPCSTR fname, bool open)
  17. {
  18. char msg[384], lastErrTxt[256];
  19. if(GetLastError())
  20. FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0, lastErrTxt, sizeof(lastErrTxt), NULL);
  21. else
  22. lastErrTxt[0] = NULL;
  23. if(open)
  24. sprintf(msg, "Unable to load %s.\n%s", fname, lastErrTxt);
  25. else
  26. sprintf(msg, "Unable to save to %s.\n%s", fname, lastErrTxt);
  27. MessageBox(NULL, msg, "File Open Error", MB_OK | MB_ICONWARNING);
  28. }
  29. void putProfile(LPCSTR section, LPCSTR key, LPCSTR value)
  30. {
  31. WritePrivateProfileString(section, key, value, INIFILE);
  32. }
  33. void putProfile(LPCSTR section, LPCSTR key, int value)
  34. {
  35. char buf[16];
  36. itoa(value, buf, 10);
  37. putProfile(section, key, buf);
  38. }
  39. LPCSTR getProfileStr(LPCSTR section, LPCSTR key)
  40. {
  41. static char buffer[256];
  42. DWORD b = GetPrivateProfileString(section, key, "0", buffer, sizeof(buffer), INIFILE);
  43. buffer[b] = 0;
  44. return buffer;
  45. }
  46. int getProfileInt(LPCSTR section, LPCSTR key)
  47. {
  48. return atoi(getProfileStr(section, key));
  49. }
  50. void toEp(SocketEndpoint & ep, LPCSTR epTxt)
  51. {
  52. char * tmp = static_cast <char *> (alloca(strlen(epTxt) + 1));
  53. strcpy(tmp, epTxt);
  54. for(int i = strlen(tmp); i >= 0; i--)
  55. {
  56. if(*(tmp + i) == ':')
  57. {
  58. ep.port = atoi(tmp + i + 1);
  59. *(tmp + i) = 0;
  60. ep.ipset(tmp);
  61. break;
  62. }
  63. }
  64. }
  65. void reportException(IException * e)
  66. {
  67. StringBuffer eMsg;
  68. e->errorMessage(eMsg);
  69. CString msg("An error occured whilst attempting connection:\n");
  70. msg += eMsg.str();
  71. msg += ".";
  72. MessageBox(NULL, msg, "Connection Error", MB_OK | MB_ICONEXCLAMATION);
  73. e->Release();
  74. }