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

上一篇 / 下一篇  2012-05-03 09:46:50 / 个人分类:C语言

昨天写了一个程序,只能解析简单的lrc文件,今天我有修改了一些源程序,现在可以解析很多格式的lrc文件,并弄好歌词的顺序
51Testing软件测试网%uI)xq+@$EJ

/**
+d5} w o~0* create by: w39709077051Testing软件测试网:I!O&In d4W!r3S
* Email:wyphao.2007@163.com51Testing软件测试网,o)N)`9L'e+U1KJ
* create data: 2012.3.27
Q_2Q8]I!P"I0**/
2Jj(v"z/k0#include <stdio.h>  //for printf,fgets...
9d8u*Q v-]v0#include <stdlib.h>  //for exit
n k8M:wp%qH0#include <vector>  //for vector
!t v X9G%i"\0#include <string.h>  //for strlen
&yvuofdWY0#include <string>  //for string51Testing软件测试网 Ah3Y4kKT,`
#include <algorithm> //for sort

L]ug'P*{0

;b\.{!d[6o0#define MAXLINE 256

?{_e5xH V0

?}\tA#A(r(vm0using namespace std;51Testing软件测试网 {+z0lt~rS-~
 51Testing软件测试网G&?fA/q
typedef struct Number{
zL`K(X;V0 int time;//歌词时间
3]3H:q_,K,ofS'C0 int line;//所在行
4Pj*`;}6SC-mU0};

;y"C G0u/~6d bd051Testing软件测试网q8|$]%t3t8r

static int LINE = 0;//记录歌词所在的行51Testing软件测试网zzy`G_4f~s

51Testing软件测试网$}2c|)Z_ol8[n;Rq

int LRCPrase(char *str, vector<string> &sentences, vector<Number> &songTime);
QL&yiVeC k0int strtoint(char *str);
7y"Yq(F+y$@ wU0int operator<(Number x,Number y);

M(U f&S)n|v nXg0

/x4a:jPG3ZVeU'}!a0int main(int argc, char *argv[]){51Testing软件测试网%Dg'Wj+ENvZ&AL
 char buf[MAXLINE];51Testing软件测试网 }/kbB{ N
 51Testing软件测试网6JA6_(My~
 vector<string> sentences, finalSentence;51Testing软件测试网3}q KA eN4F%[7P'z
 vector<Number> songTime;51Testing软件测试网3L~%n9eP-~tN-@
 FILE *fd;51Testing软件测试网y9IDZ$jHnl
  51Testing软件测试网[ f ZGV
 //fd = fopen("李慧珍 - 爱死了昨天.lrc", "r");51Testing软件测试网-w;vXhp*i R M%RE3MRi
 //fd = fopen("龙梅子 - 你把爱情给了谁.lrc", "r");51Testing软件测试网L^t WUO'ufv
 fd = fopen("小虎队 - 再见.lrc", "r");
(L U6`CZ(\Dr0v0 //fd = fopen("王凝露 - 眼泪的错觉.lrc", "r");51Testing软件测试网 y7x8pJ[H
 
rC&M#~g%n0 if(fd == NULL){
k/u EbL-TY0  perror("open file");
%V8d6v Y j3z#}0  exit(1);
J8|M;mE;k0 }
(snEjSDP0 //处理歌词
n_h6Oi?0 while(fgets(buf, sizeof(buf), fd) != NULL){51Testing软件测试网 q%PX.E-K#M
  LRCPrase(buf, sentences, songTime);51Testing软件测试网"q:QnM6n*cn|
  
jnz cf1W0 }51Testing软件测试网1~7Z4Wf;b*w/~(]
 sort(songTime.begin(), songTime.end());//按照时间排序
5T:}/Q*BH0  //printf("%d\n", sentences.size());51Testing软件测试网 bfn1m/hM-U j}+~w
 /*vector<string>::iterator it = sentences.begin();51Testing软件测试网&\.P^2SvYu
 for(; it != sentences.end(); it++){51Testing软件测试网so@R(l8| [*\)d
  //printf("%d\t,%d\t", (*it).time, (*it).line);51Testing软件测试网^^!}\KE
  printf("%s", (*it).c_str());51Testing软件测试网 jFU(m U0mb"d
 }*/
%b4f`V A:n1G0 //按时间顺序排序歌词
P }y kw T0 vector<Number>::iterator it1 = songTime.begin();51Testing软件测试网 ]%|/Gi-oD
 for(; it1 != songTime.end(); it1++){51Testing软件测试网%_b:Z)IxvC*h
  //printf("%d\t,%d\n", (*it1).time, (*it1).line);51Testing软件测试网.zA-`X$Y
  finalSentence.push_back(sentences[(*it1).line]);
/M^X9]{0 }
xUR{9p3f0 51Testing软件测试网&K"m Tqr*u
 it1 = songTime.begin();51Testing软件测试网i:w0c9Ery/JT+g
 vector<string>::iterator it = finalSentence.begin();51Testing软件测试网B!Odb [a+@q
 for(; it1 != songTime.end() && it != finalSentence.end(); it1++, it++){
TS C8h:^h:}0  printf("%d\t,%d\t%s", (*it1).time, (*it1).line, (*it).c_str());
QX M&k(eYp0 }
'Z OrQ&} o6Q4a N\0 51Testing软件测试网?Y b;I(b?L"l9w
 return 0;
6ug5C k&LNM0}

q}/z,sLu9B0

8C H}"lQ0int LRCPrase(char *str, vector<string> &sentences, vector<Number> &songTime){51Testing软件测试网&M(tqV zC!z6q!]
 if(strlen(str) == 1){//空行
kK2h;lvN K;A0  return 0;51Testing软件测试网Dz@V2Rt
 }else{
z0Y/M(i%[_%^&\ETK3n0  char *p, *q, *temp;
[e4CV;J*j1V(G0  q = str;51Testing软件测试网 N2?UeE O;vf0h
  //处理时间的
cI6V cO7ch0  while((p = strchr(q, '[')) != NULL && (temp = strchr(q, ']')) != NULL){51Testing软件测试网z|5f@XS3z
   q = p + 1;51Testing软件测试网4Pj(WX]SL
   q[temp - q] = '\0';
$v:|urg.s6i0   //printf("%s\t%d\n", q);51Testing软件测试网NJ R2}`C?rS
   51Testing软件测试网_!q:~K&q5|
   struct Number number;
%uT ?qId|0   if((number.time = strtoint(q)) < 0){
9\ \(?)E8K`$KQu4_0    return 0;51Testing软件测试网%|.S-E u4C5B~6E
   }
b sg6oX2u8a,p,{0   number.line = LINE;51Testing软件测试网 \un:a4R8H Hh
   songTime.push_back(number);51Testing软件测试网 j6Wsl%Tr;e
   q = temp + 1;51Testing软件测试网 |yDA"f4l
  }51Testing软件测试网wW8Xn5pbT(Es I
  //printf("%s", temp + 1);51Testing软件测试网5u3dxXm.e
  //截取歌词
8X Qgw$]SS d4Y0  p = ++temp;
Pu^ S4^]"E0  while(*temp != NULL){51Testing软件测试网g!`/ce3VI"[
   temp++;
+dYv Y!Qw3]-`4xx0  }51Testing软件测试网W$Y*aXol
  p[temp - p] = '\0';
3K:k ]g,}5i0  //printf("%s", p);  51Testing软件测试网;c$Hbe-kZ D v
  string s(p);51Testing软件测试网)NE)XEA{.Q F
  sentences.push_back(s);
U{ f3XA;_3}0  LINE++;
)G0H'{ u` c'm0  return 1;51Testing软件测试网"gHMC$\9^YuF
 }51Testing软件测试网hcr:K8K&O C'b}^
 
}%p h7{ Uk1{0}
,TC1Q,^e5C/Ca0//把char转换为int
f.\X0etq.l0int chartoint(char ch){51Testing软件测试网+h;L0b;M g @8a
 return ch - '0';51Testing软件测试网8Jv _!c4E#hV%C e
}

2KV5YI~L8uc5u0

d.g^k6|/^S0int strtoint(char *str){//计算时间,微秒51Testing软件测试网1l[{~ g[O)s
 if(isdigit(str[0]) && isdigit(str[1])51Testing软件测试网9F;HR(n,N1kw;Sm
  && isdigit(str[0]) && isdigit(str[0])
,}1A @0B*T%bi:i'X*C0  && isdigit(str[0]) && isdigit(str[0])){
?%V:ljz#m0   int mintue = chartoint(str[0]) * 10 + chartoint(str[1]);
"ZZ2zr:p8lK0   int second = chartoint(str[3]) * 10 + chartoint(str[4]);51Testing软件测试网"M:E&[d \ D'J2e9o
   int microsecond = chartoint(str[6]) * 10 + chartoint(str[7]);
:` bZ5sC0   return (mintue * 60 + second) * 1000 + microsecond * 10;51Testing软件测试网7Fg4sS-N ~:zk
  }51Testing软件测试网q4?$R}3l`!KN9JF#w
 return -1;
@O;U `8w x @ oo0}51Testing软件测试网fO*\r"ju8[%e
//重载<操作符,用在sort函数比较中51Testing软件测试网6}A;iO/t3_
int operator<(Number x,Number y){51Testing软件测试网2X&lg)k9WYW9U
  return x.time < y.time;51Testing软件测试网ep0R ~ Q8?)r0o
}
51Testing软件测试网6f w$VpK

Ye9]ETyL0  歌词文件:

6`6J!hty"H0

.~8y T'O-j,Bytb0

  运行程序的结果:

;o2|6Lb [T xt0

51Testing软件测试网tg/C/B dn2M Ja

  欢迎大家指点。

,M6y P[k(x0

TAG:

 

评分:0

我来说两句

Open Toolbar