解决点烟器车载mp3歌曲播放按人名顺序问题

因为老爸的车淘汰给本人了,本人开车有听歌需求,而车又比较老,没有蓝牙功能,因此本人使用点烟器外接usb实现听歌功能。点烟器中播放歌曲是按照字母顺序播放,因此每次听歌的时候会造成一直播放一个人的歌的情况(因为曲名常以人名开头),非常影响体验,因此诞生了这个脚本,原理是把所有文件名改成随机数,点烟器读取歌曲就会按照数字顺序来播放了,而每个歌曲的名字都是不重复的随机数,从而可以实现随机播放功能

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <time.h>
#define FILE_NUM_MAX 500

unsigned short isdup(int , int, char **);

int main(int argc, char * argv[])
{

  DIR                 *dp                                 = NULL;
  struct dirent       *dirp                               = NULL;
  char                *relaPath                           = NULL;
  char                absolutPath[4096]                   = {0};
  int                 r_num                               = 0;
  int                 file_num                            = 0;
  unsigned short      flag                                = 1;
  unsigned int        sto_index                           = 0;
  unsigned int        seed                                = (unsigned int)time(NULL);
  char                *orign_name_sotrage[FILE_NUM_MAX]   = {NULL};

  srand(seed);

  if(argc != 2)
  {
    perror ("plz input command like ./rest.c DIRNAME");
    closedir(dp);
    return EXIT_FAILURE;
  }

  if((dp = opendir(argv[1])) == NULL)
  {
    perror ("fail to opendir");
    closedir(dp);
    return EXIT_FAILURE;
  }

  unsigned int index_fill_file = 0;
  while((dirp = readdir(dp))!= NULL)  
  {
    orign_name_sotrage[index_fill_file] = (char *)calloc(strlen(dirp->d_name) + 1, 1); 
    if(strcmp(dirp->d_name, ".") != 0 && strcmp(dirp->d_name, "..") != 0)
    {
      strcat(orign_name_sotrage[index_fill_file], dirp->d_name);
      index_fill_file++;
    }
    file_num++;
  }

  file_num -= 2;
  printf("---------------FILE NUM = %d---------------\n\n", file_num);
  unsigned int rand_storage[file_num];

  // reopen dir
  if((dp = opendir(argv[1])) == NULL)
  {
    perror ("fail to opendir");
    closedir(dp);
    for(int i = 0; i < index_fill_file - 1; i++)
    {
      if(orign_name_sotrage[i] != NULL)
      {
        free(orign_name_sotrage[i]);
        orign_name_sotrage[i] = NULL;
      }
    }
    return EXIT_FAILURE;
  }
  
  while((dirp = readdir(dp)) != NULL)
  {
    if(strcmp(dirp->d_name, ".") != 0 && strcmp(dirp->d_name, "..") != 0)
    {
      relaPath = (char *)calloc(strlen("./") + strlen(argv[1]) + 1, 1);
    
      strcat(relaPath,         "./");
      strcat(relaPath,      argv[1]);

      if(realpath(relaPath, absolutPath) == NULL)
       {
          perror("fail to getting absolutpath");
	      closedir(dp);

          for(int i = 0; i < index_fill_file; i++)
          {
            if(orign_name_sotrage[i] != NULL)
            {
              free(orign_name_sotrage[i]);
              orign_name_sotrage[i] = NULL;
            }
          }

          free(relaPath);
          relaPath = NULL;
          return EXIT_FAILURE;
       }
      
      while(1)
      {
        r_num = rand() % file_num;

        for(int i = 0; i < sto_index; i++)
        { 
          if(r_num == rand_storage[i])
          {
            flag = 0;
            break;
          } else flag = 1;
        }

        if(flag)
        {
          char * new_name   = NULL;
          char * old_name   = (char *)malloc(4 + strlen(dirp->d_name) + strlen(absolutPath));
          sprintf(old_name, "%s/%s", absolutPath, dirp->d_name);
          
          if(isdup(r_num, index_fill_file, orign_name_sotrage))
          {
            new_name = (char *)calloc(10 + strlen(absolutPath), 1);
            sprintf(new_name, "%s/%d(1).mp3", absolutPath, r_num);
          } else {
            new_name = (char *)calloc(10 + strlen(absolutPath), 1);
            sprintf(new_name, "%s/%d.mp3", absolutPath, r_num);
          }

          rand_storage[sto_index] = r_num;
          sto_index++;

          printf("old name = %s\n",    old_name);
          printf("new name = %s\n",    new_name);
          printf("\n");

          if(rename(old_name, new_name) != 0)
          {
            for(int i = 0; i < index_fill_file; i++)
            {
              if(orign_name_sotrage[i] != NULL)
              {
                free(orign_name_sotrage[i]);
                orign_name_sotrage[i] = NULL;
              }
            }

            perror("fail to rename");
            closedir(dp);

	        free(old_name);
            old_name = NULL;

            free(new_name);
            new_name = NULL;

            free(relaPath);
            relaPath = NULL;

            return EXIT_FAILURE;
          }

          free(old_name);
          old_name = NULL;

          free(new_name);
          new_name = NULL;

          free(relaPath);
          relaPath = NULL;

          break;
        }
      }
    }
  }

  for(int i = 0; i < index_fill_file; i++)
  {
    if(orign_name_sotrage[i] != NULL)
    {
      free(*(orign_name_sotrage + i));
      orign_name_sotrage[i] = NULL;
    }
  }

  closedir(dp);
  return 0; 
}

unsigned short isdup(int num, int total, char **str)
{
  char str_4_cmp[20] = {0};
  sprintf(str_4_cmp, "%d.mp3", num);
  
  for(int i = 0; i < total; i++)
    if(!strcmp(*(str + i), str_4_cmp))  return 1;

  return 0;
}

因为不会配置vscode编译并运行时带参,所以调试耽误了很多时间,如果有大佬会配置vscode使编译后带参运行,还望告知!谢谢

  • 12
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值