genres.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include <windows.h>
  2. #include "genres.h"
  3. #include <shlwapi.h>
  4. extern const wchar_t *INI_DIRECTORY;
  5. static void file_init(wchar_t *file_path, wchar_t *fn)
  6. {
  7. PathCombineW(file_path, INI_DIRECTORY, fn);
  8. }
  9. static char eol[2]={13,10};
  10. static char get_char(HANDLE f,BOOL * eof)
  11. {
  12. DWORD br=0;
  13. char r=0;
  14. ReadFile(f,&r,1,&br,0);
  15. if (!br) *eof=1;
  16. return r;
  17. }
  18. void genres_read(HWND wnd, wchar_t* fn)
  19. {
  20. char temp[MAX_GENRE] = {0};
  21. char add[MAX_GENRE] = {0};
  22. BOOL eof=0;
  23. char c = 0;
  24. wchar_t file_path[MAX_PATH] = {0};
  25. HANDLE f;
  26. file_init(file_path, fn);
  27. f = CreateFileW(file_path, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
  28. if (f==INVALID_HANDLE_VALUE) return;
  29. GetWindowTextA(wnd,add,MAX_GENRE);
  30. while(!eof)
  31. {
  32. UINT ptr=0;
  33. BOOL start=1;
  34. while(ptr<MAX_GENRE-1)
  35. {
  36. c=get_char(f,&eof);
  37. if (eof) break;
  38. if (c==10 || c==13)
  39. {
  40. if (start) continue;
  41. else break;
  42. }
  43. start=0;
  44. temp[ptr++]=c;
  45. }
  46. if (ptr)
  47. {
  48. temp[ptr]=0;
  49. SendMessage(wnd,CB_ADDSTRING,0, (LPARAM)temp);
  50. if (add[0])
  51. {
  52. if (!_stricmp(add,temp)) add[0]=0;
  53. }
  54. }
  55. }
  56. CloseHandle(f);
  57. if (add[0]) SendMessage(wnd,CB_ADDSTRING,0,(LPARAM)add);
  58. }
  59. void genres_write(HWND wnd, wchar_t* fn)
  60. {
  61. wchar_t file_path[MAX_PATH] = {0};
  62. char temp[MAX_GENRE] = {0};
  63. UINT max = 0,n = 0;
  64. DWORD bw = 0;
  65. HANDLE f;
  66. {
  67. char add[MAX_GENRE] = {0};
  68. GetWindowTextA(wnd,add,MAX_GENRE);
  69. if (!add[0]) return;
  70. max=(UINT)SendMessage(wnd,CB_GETCOUNT,0,0);
  71. for(n=0;n<max;n++)
  72. {
  73. SendMessage(wnd,CB_GETLBTEXT,n,(LPARAM)temp);
  74. if (!_stricmp(temp,add)) return;
  75. }
  76. SendMessage(wnd,CB_ADDSTRING,0,(LPARAM)add);
  77. }
  78. file_init(file_path, fn);
  79. f = CreateFileW(file_path, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0);
  80. if (f==INVALID_HANDLE_VALUE) return;
  81. max=(UINT)SendMessage(wnd,CB_GETCOUNT,0,0);
  82. for(n=0;n<max;n++)
  83. {
  84. SendMessage(wnd,CB_GETLBTEXT,n,(LPARAM)temp);
  85. bw = 0; WriteFile(f,temp,(DWORD)strlen(temp),&bw,0);
  86. bw = 0; WriteFile(f,eol,2,&bw,0);
  87. }
  88. CloseHandle(f);
  89. }