genres.c 1.9 KB

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