Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdlib.h"
- #include "stdio.h"
- #include "string.h"
- struct frame{
- int size;
- char* value;
- char name[4];
- int pos;
- };
- int get_size(FILE* file)
- {
- int a, ans;
- ans = 0;
- for (int i = 0; i < 4; i++) {
- fread(&a, 1, 1, file);
- ans = ans * 128 + a % 128;
- }
- return ans;
- }
- int get_frame_size(FILE* file)
- {
- int a, ans;
- ans = 0;
- for (int i = 0; i < 4; i++){
- fread(&a, 1, 1, file);
- ans = ans * 256 + a % 128;
- }
- return ans;
- }
- struct frame T_frame_pos[38];
- T_it = 0;
- struct frame W_frame_pos[20];
- W_it = 0;
- struct frame comment;
- int check_comment = 0;
- void get_frame(FILE* file, int *i, char c){
- struct frame new_frame;
- new_frame.name[0] = c;
- for (int j = 1; j < 4; ++j){
- fread(&new_frame.name[j], 1, 1, file);
- }
- *i = *i + 3;
- new_frame.size = get_frame_size(file);
- *i += 4;
- for (int j = 0; j < 2; ++j){ fread(&c, 1, 1, file);}
- *i += 2;
- if (new_frame.name[0] == 'T'){
- fread(&c, 1, 1, file);
- new_frame.pos = ftell(file);
- new_frame.value = (char*)malloc((new_frame.size - 1) * sizeof(char));
- for (int j = 0; j < new_frame.size - 1; j++){
- fread(&new_frame.value[j], 1, 1, file);
- }
- *i += new_frame.size;
- T_frame_pos[T_it] = new_frame;
- T_it++;
- return;
- }
- else if (new_frame.name[0] == 'W'){
- new_frame.pos = ftell(file);
- new_frame.value = (char*)malloc((new_frame.size - 1) * sizeof(char));
- for (int j = 0; j < new_frame.size; j++)
- fread(&new_frame.value[j], 1, 1, file);
- *i += new_frame.size;
- W_frame_pos[W_it] = new_frame;
- W_it++;
- return;
- }
- else if (new_frame.name[0] == 'C' && new_frame.name[3] == 'M'){
- comment.name[0] = 'C';
- comment.name[1] = 'O';
- comment.name[2] = 'M';
- comment.name[3] = 'M';
- comment.size = new_frame.size;
- fread(&c, 1, 1, file);
- fread(&c, 1, 1, file);
- fread(&c, 1, 1, file);
- fread(&c, 1, 1, file);
- comment.pos = ftell(file);
- comment.value = (char *) malloc(sizeof(char) * (comment.size - 4));
- for (int j = 0; j < comment.size - 4; j++){
- fread(&comment.value[j], 1, 1, file);
- }
- *i += comment.size;
- check_comment = 1;
- return;
- }
- else{
- fread(&c, 1, 1, file);
- fread(&c, 1, 1, file);
- fread(&c, 1, 1, file);
- *i += 3;
- int skip_size = get_frame_size(file);
- fread(&c, 1, 1, file);
- fread(&c, 1, 1, file);
- *i += 2;
- for (int j = 0; j < skip_size; j++)
- fread(&c, 1, 1, file);
- *i += skip_size;
- return;
- }
- }
- void show()
- {
- for (int i = 0; i < T_it; i++)
- {
- for(int j = 0; j < 4; ++j){
- printf("%c", T_frame_pos[i].name[j]);
- }
- printf(" ");
- for(int j = 0; j < T_frame_pos[i].size - 1; ++j){
- printf("%c", T_frame_pos[i].value[j]);
- }
- printf("\n");
- }
- for (int i = 0; i < W_it; i++)
- {
- for(int j = 0; j < 4; ++j){
- printf("%c", W_frame_pos[i].name[j]);
- }
- printf(" ");
- for(int j = 0; j < W_frame_pos[i].size; ++j){
- printf("%c", W_frame_pos[i].value[j]);
- }
- printf("\n");
- }
- if (check_comment == 1){
- for(int j = 0; j < 4; ++j){
- printf("%c", comment.name[j]);
- }
- printf(" ");
- for(int j = 0; j < comment.size - 4; ++j){
- printf("%c", comment.value[j]);
- }
- printf("\n");
- }
- }
- int frame_comp(char* x, char* y){
- int check = 1;
- for (int j = 0; j < 4; ++j){
- if (x[j] != y[j]){
- check = 0;
- }
- }
- return check;
- }
- void get_value(char* fr){
- if (fr[0] == 'T'){
- int j = 0;
- while ((j < T_it) && (frame_comp(fr, T_frame_pos[j].name) == 0)){
- ++j;
- }
- if (j != T_it){
- for (int k = 0; k < T_frame_pos[j].size - 1; ++k)
- printf("%c", T_frame_pos[j].value[k]);
- printf("\n");
- }
- else{
- printf("There are no such frames\n");
- }
- }
- else if (fr[0] == 'W'){
- int j = 0;
- while ((j < W_it) && (frame_comp(fr, W_frame_pos[j].name) == 0)){
- ++j;
- }
- if (j != W_it){
- for (int k = 0; k < W_frame_pos[j].size; ++k){
- printf("%c", W_frame_pos[j].value[k]);
- }
- printf("\n");
- }
- else{
- printf("There are no such frames\n");
- }
- }
- else if (fr[0] == 'C' && fr[1] == 'O' && fr[2] == 'M' && fr[3] == 'P'){
- for (int k = 0; k < comment.size - 4; ++k){
- printf("%c", comment.value[k]);
- }
- printf("\n");
- }
- else{
- printf("There are no such frames\n");
- }
- }
- void information(FILE* file)
- {
- T_it = 0;
- W_it = 0;
- fseek(file, 0, SEEK_SET);
- char c;
- for (int i = 0; i < 6; ++i){
- fread(&c, 1, 1, file);
- }
- int size_all_frames = get_size(file);
- int i = 0;
- fread(&c, 1, 1, file);
- while (i < size_all_frames && c != '\0'){
- get_frame(file, &i, c);
- fread(&c, 1, 1, file);
- ++i;
- }
- }
- void change_frame(FILE* file, char* fr, char* value){
- char c = '\0';
- if (fr[0] == 'T') {
- int j = 0;
- while ((j < T_it) && (frame_comp(fr, T_frame_pos[j].name) == 0)){
- ++j;
- }
- if (j != T_it)
- {
- fseek(file, T_frame_pos[j].pos, 0);
- if (T_frame_pos[j].size - 1 < strlen(value)){
- for (int k = 0; k < T_frame_pos[j].size - 1; ++k){
- fwrite(&value[k], 1, 1, file);
- }
- }
- else{
- for (int k = 0; k < strlen(value); ++k){
- printf("%c ", value[k]);
- fwrite(&value[k], 1, 1, file);
- }
- printf("\n");
- for (int k = strlen(value); k < T_frame_pos[j].size - 1; ++k){
- fwrite(&c, 1, 1, file);
- }
- }
- }
- else
- printf("There are no such frames\n");
- } else
- if (fr[0] == 'W') {
- int j = 0;
- while ((j < W_it) && (frame_comp(fr, W_frame_pos[j].name) == 0))
- ++j;
- if (j != W_it)
- {
- fseek(file, W_frame_pos[j].pos, 0);
- if (W_frame_pos[j].size < strlen(value))
- {
- for (int k = 0; k < W_frame_pos[j].size; ++k)
- fwrite(&value[k], 1, 1, file);
- }
- else {
- for (int k = 0; k < strlen(value); ++k){
- fwrite(&value[k], 1, 1, file);
- }
- for (int k = strlen(value); k < W_frame_pos[j].size; ++k){
- fwrite(&c, 1, 1, file);
- }
- }
- }
- else{
- printf("There are no such frames\n");
- }
- }
- else{
- if (frame_comp(fr, "COMM") != 0){
- fseek(file, comment.pos, 0);
- if (comment.size - 4 < strlen(value))
- {
- for (int j = 0; j < comment.size - 4; ++j)
- fwrite(&value[j], 1, 1, file);
- }
- else {
- for (int j = 0; j < strlen(value); ++j){
- fwrite(&value[j], 1, 1, file);
- }
- for (int j = strlen(value); j < comment.size - 4; ++j) {
- fwrite(&c, 1, 1, file);
- }
- }
- }
- else
- printf("There are no such frames\n");
- }
- information(file);
- }
- int main(){
- /*char* name_file = (char*)malloc(sizeof(char));
- for (int i = 11; i < strlen(argv[1]); ++i){
- if (arg)
- name_file[i - 11] = argv[1][i];
- }*/
- FILE *file = fopen("test.mp3", "rb");
- information(file);
- show();
- change_frame(file, "TIT2", "assdvs");
- printf("\n");
- show();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement