C语言解析多格式的lrc文件

发表于:2012-5-02 10:04

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:w397090770    来源:51Testing软件测试网采编

  昨天写了一个程序,只能解析简单的lrc文件,今天我有修改了一些源程序,现在可以解析很多格式的lrc文件,并弄好歌词的顺序

/**
* create by: w397090770
* Email:wyphao.2007@163.com
* create data: 2012.3.27
**/
#include <stdio.h>  //for printf,fgets...
#include <stdlib.h>  //for exit
#include <vector>  //for vector
#include <string.h>  //for strlen
#include <string>  //for string
#include <algorithm> //for sort

#define MAXLINE 256

using namespace std;
 
typedef struct Number{
 int time;//歌词时间
 int line;//所在行
};

static int LINE = 0;//记录歌词所在的行

int LRCPrase(char *str, vector<string> &sentences, vector<Number> &songTime);
int strtoint(char *str);
int operator<(Number x,Number y);

int main(int argc, char *argv[]){
 char buf[MAXLINE];
 
 vector<string> sentences, finalSentence;
 vector<Number> songTime;
 FILE *fd;
  
 //fd = fopen("李慧珍 - 爱死了昨天.lrc", "r");
 //fd = fopen("龙梅子 - 你把爱情给了谁.lrc", "r");
 fd = fopen("小虎队 - 再见.lrc", "r");
 //fd = fopen("王凝露 - 眼泪的错觉.lrc", "r");
 
 if(fd == NULL){
  perror("open file");
  exit(1);
 }
 //处理歌词
 while(fgets(buf, sizeof(buf), fd) != NULL){
  LRCPrase(buf, sentences, songTime);
  
 }
 sort(songTime.begin(), songTime.end());//按照时间排序
  //printf("%d\n", sentences.size());
 /*vector<string>::iterator it = sentences.begin();
 for(; it != sentences.end(); it++){
  //printf("%d\t,%d\t", (*it).time, (*it).line);
  printf("%s", (*it).c_str());
 }*/
 //按时间顺序排序歌词
 vector<Number>::iterator it1 = songTime.begin();
 for(; it1 != songTime.end(); it1++){
  //printf("%d\t,%d\n", (*it1).time, (*it1).line);
  finalSentence.push_back(sentences[(*it1).line]);
 }
 
 it1 = songTime.begin();
 vector<string>::iterator it = finalSentence.begin();
 for(; it1 != songTime.end() && it != finalSentence.end(); it1++, it++){
  printf("%d\t,%d\t%s", (*it1).time, (*it1).line, (*it).c_str());
 }
 
 return 0;
}

int LRCPrase(char *str, vector<string> &sentences, vector<Number> &songTime){
 if(strlen(str) == 1){//空行
  return 0;
 }else{
  char *p, *q, *temp;
  q = str;
  //处理时间的
  while((p = strchr(q, '[')) != NULL && (temp = strchr(q, ']')) != NULL){
   q = p + 1;
   q[temp - q] = '\0';
   //printf("%s\t%d\n", q);
   
   struct Number number;
   if((number.time = strtoint(q)) < 0){
    return 0;
   }
   number.line = LINE;
   songTime.push_back(number);
   q = temp + 1;
  }
  //printf("%s", temp + 1);
  //截取歌词
  p = ++temp;
  while(*temp != NULL){
   temp++;
  }
  p[temp - p] = '\0';
  //printf("%s", p);  
  string s(p);
  sentences.push_back(s);
  LINE++;
  return 1;
 }
 
}
//把char转换为int
int chartoint(char ch){
 return ch - '0';
}

int strtoint(char *str){//计算时间,微秒
 if(isdigit(str[0]) && isdigit(str[1])
  && isdigit(str[0]) && isdigit(str[0])
  && isdigit(str[0]) && isdigit(str[0])){
   int mintue = chartoint(str[0]) * 10 + chartoint(str[1]);
   int second = chartoint(str[3]) * 10 + chartoint(str[4]);
   int microsecond = chartoint(str[6]) * 10 + chartoint(str[7]);
   return (mintue * 60 + second) * 1000 + microsecond * 10;
  }
 return -1;
}
//重载<操作符,用在sort函数比较中
int operator<(Number x,Number y){
  return x.time < y.time;
}

  歌词文件:

  运行程序的结果:

  欢迎大家指点。

《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号