210 lines
7.2 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// Created by CC-star on 25-5-9.
//
#include "C3_shear.h"
#include <dirent.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern int songCount;
//读取
void Read_MuiscFile() {
DIR *dir;//文件目录
struct dirent *entry;//文件信息
dir = opendir(MRES_PATH);
if (dir == NULL) {
printf("无法打开歌曲文件目录!\n");
return;
}
songCount = 0;
// 编号-歌手名-歌曲名-歌曲类别.mp3
while ((entry = readdir(dir)) != NULL && songCount < 500) {
char *filename = entry->d_name;
if (strstr(filename, ".mp3") != NULL) {
char fileCopy[200];
strcpy(fileCopy, filename);
//id
char *token = strtok(fileCopy, "-");
if (token != NULL) {
SongList[songCount].id = atoi(token);
// 歌手名
token = strtok(NULL, "-");
if (token != NULL) {
strcpy(SongList[songCount].artist, token);
// 歌曲名
token = strtok(NULL, "-");
if (token != NULL) {
strcpy(SongList[songCount].name, token);
// 歌曲类别
token = strtok(NULL, ".");
if (token != NULL) {
strcpy(SongList[songCount].category, token);
songCount++;
}
}
}
}
}
}
closedir(dir);
int i, j;
struct Song temp;
for (i = 0; i < songCount - 1; i++) {
for (j = 0; j < songCount - i - 1; j++) {
if (SongList[j].id > SongList[j + 1].id) {
temp = SongList[j];
SongList[j] = SongList[j + 1];
SongList[j + 1] = temp;
}
}
}
}
// 歌曲浏览功能
void BrowseSongs() {
FILE *fp = fopen(MUSIC_PATH, "r");
printf("\n---------------------👇歌曲列表👇----------------------\n");
char line[500];
while (fgets(line, sizeof(line), fp) != NULL) {
printf("%s", line);
}
printf("-----------------------👆歌曲列表👆------------------------\n");
fclose(fp);
}
//歌曲查询
void Song_query() {
int i = 0;//某一个歌曲信息
FILE *fp = fopen(MUSIC_PATH, "r");
if (fp == NULL) {
printf("文件打开失败\n");
return;
}
char line[100];
// 编号-歌手名-歌曲名-歌曲类别
while (fgets(line, sizeof(line), fp) != NULL) {
line[strcspn(line, "\n")] = 0;
//ID
char *token = strtok(line, "-");
if (token != NULL) {
SongList[i].id = atoi(token);
//歌手
token = strtok(NULL, "-");
if (token != NULL) {
strcpy(SongList[i].artist, token);
//歌曲
token = strtok(NULL, "-");
if (token != NULL) {
strcpy(SongList[i].name, token);
token = strtok(NULL, "-");
if (token != NULL) {
strcpy(SongList[i].category, token);
i++;
}
}
}
}
}
fclose(fp);
int sum_song = i;
while (1) {
int found = 0;
printf("\n---------------------👇歌曲查询信息类别👇----------------------\n");
printf("1id\n2.歌曲名\n3.歌手名\n4.歌曲类别\n");
printf("0.返回控制台\n");
printf("-----------------------👆选择对应数字👆------------------------\n");
printf("请输入选择:");
int opt,a;
char c[100];
scanf("%d", &opt);
while(getchar() != '\n');
switch (opt) {
case 1:
printf("请输入id");
scanf("%d", &a);
while(getchar() != '\n');
printf("\n---------------------👇您所查询的歌曲信息👇----------------------\n");
for (int i = 0; i < sum_song; ++i) {
if (a == SongList[i].id) {
printf("%d-%s-%s-%s\n", SongList[i].id, SongList[i].artist, SongList[i].name, SongList[i].category);
found = 1;
}
}
if (found == 0) {
printf("⚠️未找到歌曲...⚠️\n");
}
printf("-----------------------👆歌曲信息👆------------------------\n");
break;
case 2:
printf("请输入歌曲名:");
scanf("%[^\n]", c);
while(getchar() != '\n');
printf("\n---------------------👇您所查询的歌曲信息👇----------------------\n");
for (int i = 0; i < sum_song; ++i) {
if (strcmp(c, SongList[i].name) == 0) {
printf("%d-%s-%s-%s\n", SongList[i].id, SongList[i].artist, SongList[i].name, SongList[i].category);
found = 1;
}
}
if (found == 0) {
printf("⚠️未找到歌曲...⚠️\n");
}
printf("-----------------------👆歌曲信息👆------------------------\n");
break;
case 3:
printf("请输入歌手名:");
scanf("%[^\n]", c);
while(getchar() != '\n');
printf("\n---------------------👇您所查询的歌曲信息👇----------------------\n");
for (int i = 0; i < sum_song; ++i) {
if (strcmp(c, SongList[i].artist) == 0) {
printf("%d-%s-%s-%s\n", SongList[i].id, SongList[i].artist, SongList[i].name, SongList[i].category);
found = 1;
}
}
if (found == 0) {
printf("⚠️未找到歌曲...⚠️\n");
}
printf("-----------------------👆歌曲信息👆------------------------\n");
break;
case 4:
printf("请输入歌曲类别:");
scanf("%s", c);
while(getchar() != '\n');
printf("\n---------------------👇您所查询的歌曲信息👇----------------------\n");
for (int i = 0; i < sum_song; ++i) {
if (strcmp(c, SongList[i].category) == 0) {
printf("%d-%s-%s-%s\n", SongList[i].id, SongList[i].artist, SongList[i].name, SongList[i].category);
found = 1;
}
}
if (found == 0) {
printf("⚠️未找到歌曲...⚠️\n");
}
printf("-----------------------👆歌曲信息👆------------------------\n");
break;
case 0:
printf("⚠️返回控制台中...⚠️\n");
return;
default:
printf("⚠️输入有误,请重新输入⚠️\n");
break;
}
}
}