Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.22 KB | None | 0 0
  1. #include "stdlib.h"
  2. #include "stdio.h"
  3. #include "string.h"
  4.  
  5. struct frame{
  6. int size;
  7. char* value;
  8. char name[4];
  9. int pos;
  10. };
  11.  
  12. int get_size(FILE* file)
  13. {
  14. int a, ans;
  15. ans = 0;
  16. for (int i = 0; i < 4; i++) {
  17. fread(&a, 1, 1, file);
  18. ans = ans * 128 + a % 128;
  19. }
  20. return ans;
  21. }
  22.  
  23. int get_frame_size(FILE* file)
  24. {
  25. int a, ans;
  26. ans = 0;
  27. for (int i = 0; i < 4; i++){
  28. fread(&a, 1, 1, file);
  29. ans = ans * 256 + a % 128;
  30. }
  31. return ans;
  32. }
  33.  
  34. struct frame T_frame_pos[38];
  35. T_it = 0;
  36. struct frame W_frame_pos[20];
  37. W_it = 0;
  38. struct frame comment;
  39. int check_comment = 0;
  40.  
  41. void get_frame(FILE* file, int *i, char c){
  42. struct frame new_frame;
  43. new_frame.name[0] = c;
  44. for (int j = 1; j < 4; ++j){
  45. fread(&new_frame.name[j], 1, 1, file);
  46. }
  47. *i = *i + 3;
  48. new_frame.size = get_frame_size(file);
  49. *i += 4;
  50. for (int j = 0; j < 2; ++j){ fread(&c, 1, 1, file);}
  51. *i += 2;
  52. if (new_frame.name[0] == 'T'){
  53. fread(&c, 1, 1, file);
  54. new_frame.pos = ftell(file);
  55. new_frame.value = (char*)malloc((new_frame.size - 1) * sizeof(char));
  56. for (int j = 0; j < new_frame.size - 1; j++){
  57. fread(&new_frame.value[j], 1, 1, file);
  58. }
  59. *i += new_frame.size;
  60. T_frame_pos[T_it] = new_frame;
  61. T_it++;
  62. return;
  63. }
  64. else if (new_frame.name[0] == 'W'){
  65. new_frame.pos = ftell(file);
  66. new_frame.value = (char*)malloc((new_frame.size - 1) * sizeof(char));
  67. for (int j = 0; j < new_frame.size; j++)
  68. fread(&new_frame.value[j], 1, 1, file);
  69. *i += new_frame.size;
  70. W_frame_pos[W_it] = new_frame;
  71. W_it++;
  72. return;
  73. }
  74. else if (new_frame.name[0] == 'C' && new_frame.name[3] == 'M'){
  75. comment.name[0] = 'C';
  76. comment.name[1] = 'O';
  77. comment.name[2] = 'M';
  78. comment.name[3] = 'M';
  79. comment.size = new_frame.size;
  80. fread(&c, 1, 1, file);
  81. fread(&c, 1, 1, file);
  82. fread(&c, 1, 1, file);
  83. fread(&c, 1, 1, file);
  84. comment.pos = ftell(file);
  85. comment.value = (char *) malloc(sizeof(char) * (comment.size - 4));
  86. for (int j = 0; j < comment.size - 4; j++){
  87. fread(&comment.value[j], 1, 1, file);
  88. }
  89. *i += comment.size;
  90. check_comment = 1;
  91. return;
  92. }
  93. else{
  94. fread(&c, 1, 1, file);
  95. fread(&c, 1, 1, file);
  96. fread(&c, 1, 1, file);
  97. *i += 3;
  98. int skip_size = get_frame_size(file);
  99. fread(&c, 1, 1, file);
  100. fread(&c, 1, 1, file);
  101. *i += 2;
  102. for (int j = 0; j < skip_size; j++)
  103. fread(&c, 1, 1, file);
  104. *i += skip_size;
  105. return;
  106. }
  107. }
  108.  
  109. void show()
  110. {
  111. for (int i = 0; i < T_it; i++)
  112. {
  113. for(int j = 0; j < 4; ++j){
  114. printf("%c", T_frame_pos[i].name[j]);
  115. }
  116. printf(" ");
  117. for(int j = 0; j < T_frame_pos[i].size - 1; ++j){
  118. printf("%c", T_frame_pos[i].value[j]);
  119. }
  120. printf("\n");
  121. }
  122. for (int i = 0; i < W_it; i++)
  123. {
  124. for(int j = 0; j < 4; ++j){
  125. printf("%c", W_frame_pos[i].name[j]);
  126. }
  127. printf(" ");
  128. for(int j = 0; j < W_frame_pos[i].size; ++j){
  129. printf("%c", W_frame_pos[i].value[j]);
  130. }
  131. printf("\n");
  132. }
  133. if (check_comment == 1){
  134. for(int j = 0; j < 4; ++j){
  135. printf("%c", comment.name[j]);
  136. }
  137. printf(" ");
  138. for(int j = 0; j < comment.size - 4; ++j){
  139. printf("%c", comment.value[j]);
  140. }
  141. printf("\n");
  142. }
  143. }
  144.  
  145. int frame_comp(char* x, char* y){
  146. int check = 1;
  147. for (int j = 0; j < 4; ++j){
  148. if (x[j] != y[j]){
  149. check = 0;
  150. }
  151. }
  152. return check;
  153. }
  154.  
  155. void get_value(char* fr){
  156. if (fr[0] == 'T'){
  157. int j = 0;
  158. while ((j < T_it) && (frame_comp(fr, T_frame_pos[j].name) == 0)){
  159. ++j;
  160. }
  161. if (j != T_it){
  162. for (int k = 0; k < T_frame_pos[j].size - 1; ++k)
  163. printf("%c", T_frame_pos[j].value[k]);
  164. printf("\n");
  165. }
  166. else{
  167. printf("There are no such frames\n");
  168. }
  169. }
  170. else if (fr[0] == 'W'){
  171. int j = 0;
  172. while ((j < W_it) && (frame_comp(fr, W_frame_pos[j].name) == 0)){
  173. ++j;
  174. }
  175. if (j != W_it){
  176. for (int k = 0; k < W_frame_pos[j].size; ++k){
  177. printf("%c", W_frame_pos[j].value[k]);
  178. }
  179. printf("\n");
  180. }
  181. else{
  182. printf("There are no such frames\n");
  183. }
  184. }
  185. else if (fr[0] == 'C' && fr[1] == 'O' && fr[2] == 'M' && fr[3] == 'P'){
  186. for (int k = 0; k < comment.size - 4; ++k){
  187. printf("%c", comment.value[k]);
  188. }
  189. printf("\n");
  190. }
  191. else{
  192. printf("There are no such frames\n");
  193. }
  194. }
  195.  
  196. void information(FILE* file)
  197. {
  198. T_it = 0;
  199. W_it = 0;
  200. fseek(file, 0, SEEK_SET);
  201. char c;
  202. for (int i = 0; i < 6; ++i){
  203. fread(&c, 1, 1, file);
  204. }
  205. int size_all_frames = get_size(file);
  206. int i = 0;
  207. fread(&c, 1, 1, file);
  208. while (i < size_all_frames && c != '\0'){
  209. get_frame(file, &i, c);
  210. fread(&c, 1, 1, file);
  211. ++i;
  212. }
  213. }
  214.  
  215. void change_frame(FILE* file, char* fr, char* value){
  216. char c = '\0';
  217. if (fr[0] == 'T') {
  218. int j = 0;
  219. while ((j < T_it) && (frame_comp(fr, T_frame_pos[j].name) == 0)){
  220. ++j;
  221. }
  222. if (j != T_it)
  223. {
  224. fseek(file, T_frame_pos[j].pos, 0);
  225. if (T_frame_pos[j].size - 1 < strlen(value)){
  226. for (int k = 0; k < T_frame_pos[j].size - 1; ++k){
  227. fwrite(&value[k], 1, 1, file);
  228. }
  229. }
  230. else{
  231. for (int k = 0; k < strlen(value); ++k){
  232. printf("%c ", value[k]);
  233. fwrite(&value[k], 1, 1, file);
  234. }
  235. printf("\n");
  236. for (int k = strlen(value); k < T_frame_pos[j].size - 1; ++k){
  237. fwrite(&c, 1, 1, file);
  238. }
  239. }
  240. }
  241. else
  242. printf("There are no such frames\n");
  243. } else
  244. if (fr[0] == 'W') {
  245. int j = 0;
  246. while ((j < W_it) && (frame_comp(fr, W_frame_pos[j].name) == 0))
  247. ++j;
  248. if (j != W_it)
  249. {
  250. fseek(file, W_frame_pos[j].pos, 0);
  251. if (W_frame_pos[j].size < strlen(value))
  252. {
  253. for (int k = 0; k < W_frame_pos[j].size; ++k)
  254. fwrite(&value[k], 1, 1, file);
  255. }
  256. else {
  257. for (int k = 0; k < strlen(value); ++k){
  258. fwrite(&value[k], 1, 1, file);
  259. }
  260. for (int k = strlen(value); k < W_frame_pos[j].size; ++k){
  261. fwrite(&c, 1, 1, file);
  262. }
  263. }
  264. }
  265. else{
  266. printf("There are no such frames\n");
  267. }
  268. }
  269. else{
  270. if (frame_comp(fr, "COMM") != 0){
  271. fseek(file, comment.pos, 0);
  272. if (comment.size - 4 < strlen(value))
  273. {
  274. for (int j = 0; j < comment.size - 4; ++j)
  275. fwrite(&value[j], 1, 1, file);
  276. }
  277. else {
  278. for (int j = 0; j < strlen(value); ++j){
  279. fwrite(&value[j], 1, 1, file);
  280. }
  281. for (int j = strlen(value); j < comment.size - 4; ++j) {
  282. fwrite(&c, 1, 1, file);
  283. }
  284. }
  285. }
  286. else
  287. printf("There are no such frames\n");
  288. }
  289. information(file);
  290. }
  291.  
  292.  
  293.  
  294. int main(){
  295. /*char* name_file = (char*)malloc(sizeof(char));
  296. for (int i = 11; i < strlen(argv[1]); ++i){
  297. if (arg)
  298. name_file[i - 11] = argv[1][i];
  299. }*/
  300. FILE *file = fopen("test.mp3", "rb");
  301. information(file);
  302. show();
  303. change_frame(file, "TIT2", "assdvs");
  304. printf("\n");
  305. show();
  306. return 0;
  307. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement