winprofile.cpp
上传用户:zslianheng
上传日期:2013-04-03
资源大小:946k
文件大小:3k
源码类别:

Linux/Unix编程

开发平台:

Visual C++

  1. /***************************************************************************
  2.  *                                                                         *
  3.  *   This program is free software; you can redistribute it and/or modify  *
  4.  *   it under the terms of the GNU General Public License as published by  *
  5.  *   the Free Software Foundation; either version 2 of the License, or     *
  6.  *   (at your option) any later version.                                   *
  7.  *                                                                         *
  8.  *   copyright            : (C) 2002 by Zhang Yong                         *
  9.  *   email                : z-yong163@163.com                              *
  10.  ***************************************************************************/
  11. #include "stdafx.h"
  12. #include "winprofile.h"
  13. #include "ndes.h"
  14. static char hex2char(char c)
  15. {
  16. if (c < 10)
  17. return (c + '0');
  18. return (c - 10) + 'a';
  19. }
  20. static char char2hex(char c)
  21. {
  22. if (isdigit(c))
  23. return (c - '0');
  24. return (c - 'a' + 10);
  25. }
  26. bool WinProfile::readBool(const char *key)
  27. {
  28. string val;
  29. readString(key, val);
  30. return (val.compare("y") == 0);
  31. }
  32. int WinProfile::readInt(const char *key)
  33. {
  34. return GetPrivateProfileInt(sectionName, key, 0, fileName);
  35. }
  36. void WinProfile::readString(const char *key, string &val)
  37. {
  38. char str[256];
  39. GetPrivateProfileString(sectionName, key, "", str, sizeof(str), fileName);
  40. val = str;
  41. }
  42. void WinProfile::readPassword(const char *key, string &passwd)
  43. {
  44. string val;
  45. readString(key, val);
  46. char buf[256];
  47. char *p = buf;
  48. int n = val.length();
  49. for (int i = 0; i < n; ++i) {
  50. char c = (char2hex(val[i++]) << 4);
  51. c |= char2hex(val[i]);
  52. *p++ = c;
  53. }
  54. n /= 16;
  55. p = buf;
  56. for (i = 0; i < n; ++i) {
  57. dedes(p);
  58. p += 8;
  59. }
  60. *p = '';
  61. passwd = buf;
  62. }
  63. void WinProfile::writeBool(const char *key, bool b)
  64. {
  65. const char *val = (b ? "y" : "n");
  66. writeString(key, val);
  67. }
  68. void WinProfile::writeInt(const char *key, int val)
  69. {
  70. CString str;
  71. str.Format("%d", val);
  72. writeString(key, str);
  73. }
  74. void WinProfile::writeString(const char *key, const char *val)
  75. {
  76. WritePrivateProfileString(sectionName, key, val, fileName);
  77. }
  78. void WinProfile::writePassword(const char *key, const char *passwd)
  79. {
  80. char buf[256];
  81. int n = strlen(passwd);
  82. memcpy(buf, passwd, n);
  83. int i = n % 8;
  84. if (i) {
  85. i = 8 - i;
  86. while (i-- > 0)
  87. buf[n++] = 0;
  88. }
  89. char *p = buf;
  90. for (i = 0; i < n / 8; ++i) {
  91. endes(p);
  92. p += 8;
  93. }
  94. char val[256];
  95. p = val;
  96. for (i = 0; i < n; ++i) {
  97. *p++ = hex2char((buf[i] & 0xf0) >> 4);
  98. *p++ = hex2char(buf[i] & 0x0f);
  99. }
  100. *p = '';
  101. writeString(key, val);
  102. }