본문 바로가기

Information Technology/C

[C언어] MP3 관리 프로그램(3)

이 글은 개인의 학습을 목적으로 정리한 글입니다. 이점 참고하고 읽어주세요;)


library.h

#include <stdlib.h>
#include <string.h>
#ifndef LIBRARY_H
#define LIBRARY_H

typedef struct song Song;
typedef struct snode SNode;
typedef struct artist Artist;

struct song
{
	Artist* artist;	// 곡을 부른 가수를 연결하는 Artist 포인터
	char* title;	// 곡의 제목을 저장하는 char 포인터
	char* path;		// 곡의 저장 경로를 저장하는 char 포인터
	int index;		// 곡의 인덱스를 저장하는 int 자료형
};

struct snode	// 양방향 연결리스트
{
	struct snode* next, * prev;	// 다음 곡과 이전 곡을 연결하는 snode 포인터 
	Song* song;	// snode가 연결된 곡을 연결하는 song 포인터
};

struct artist
{
	char* name;	// 가수의 이름을 저장하는 char 포인터
	struct artist* next; // 이름의 초성이 동일한 가수들을 단방향 리스트로 연결
	
	SNode* head, * tail; // 가수가 부른 노래들을 연결하는 snode 포인터
};

void initialize();
void add_song(char* artist, char* title, char* path);
#endif // !1

library.c

#include "library.h"
#define NUM_CHARS 256	// 2^8 = 256. 한 바이트가 가질 수 있는 최대 크기

Artist* artist_directory[NUM_CHARS];	// 배열 한 칸당 artist 포인터 타입을 저장하는 배열

void initialize() // Aritst 배열을 초기화
{
	for (int i = 0; i < NUM_CHARS; i++)
		artist_directory[i] = NULL;
}

void add_song(char* artist, char* title, char* path)
{
	// 가수가 이미 존재하는 경우
	// 존재하지 않는다면 NULL return
	Artist* ptr_artist = find_artist(artist);	// 가수를 찾아서 Artist 포인터를 return
	if (ptr_artist == NULL)
	{

	}
	else
	{

	}
	// 가수가 이미 존재하지 않는 경우
}

Artist* find_artist(char* name)
{
	// 찾는 가수가 속한 초성 그룹의 첫 번째 가수
	Artist* p = artist_directory[(unsigned char)name[0]]; 
	/* name[0] : 00000000 ~ 11111111 */
	// int에서 첫 비트 1은 음수를 나타냄
	// 때문에 unsigned char로 형변환
	// char = 8bit, int = 32bit

	while (p != NULL && strcmp(p->name, name) < 0)	// 알파벳 순으로 더 큰 이름이 나온다면 굳이 끝까지 읽을 필요x
		p = p->next; // 이름을 찾을 때 까지 p는 다음 노드로 이동
	
	if (p != NULL && strcmp(p->name, name) == 0)
		return p;	// p가 NULL이라면 NULL return, name을 찾으면 p를 return
	else
		return NULL;
}