1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
| #include <stdio.h> #include <windows.h>
typedef struct { unsigned int time; char data[40]; }LineData; class CLrc { public: BOOL Open( LPTSTR FileName ); void Close(); BOOL GetTitle( LPTSTR Title ); BOOL GetAlbum( LPTSTR Album ); BOOL GetArtist( LPTSTR Artist ); BOOL GetEditor( LPTSTR Editor ); BOOL ReadLine( LPTSTR Data ); CLrc() { memset( LrcFile, 0, 128 ); ldpoint = 0; hLrcFile = NULL; } ~CLrc() { if( hLrcFile != NULL ) { fclose( hLrcFile ); } } FILE *hLrcFile; char LrcFile[128]; LineData ld[100]; int ldpoint; void DelCRLF( char *Data ); void Sort(); int Findch( const char *str, unsigned char ch ); int GetTime( const char *line ); void ResolveLine( const char *line ); void GetLineData( const char *line, char *data ); };
BOOL CLrc::Open( LPTSTR FileName ) { FILE *fp; fp = fopen(FileName, "r"); if( fp == NULL ) { return FALSE; } hLrcFile = fp; return TRUE; }
void CLrc::Close() { if( hLrcFile != NULL ) { fclose( hLrcFile ); } }
void CLrc::GetLineData( const char *line, char *data ) { int i = 0, j = 0, len; if( strcmp( line, "" ) ) { len = strlen( line ); i = len; while( line[i] != ']' && i > 0 ) { i--; } for( j = i + 1, i += 1; j <= len; j++ ) { data[j - i] = line[j]; } } }
BOOL CLrc::ReadLine( char *line ) { char Line[100]; memset( Line, 0, 100 ); if( feof( hLrcFile ) || fgets( Line, 100, hLrcFile) == NULL ) { return FALSE; } DelCRLF( Line ); strcpy( line, Line ); return TRUE; }
void CLrc::DelCRLF( char *Data ) { int i = 0; int len = strlen( Data ); for( ; i < len; i++ ) { if( Data[i] == 0x0D || Data[i] == 0x0A ) { int j = i; for( ; j < len ; j++ ) { Data[j] = Data[j + 1]; } } } }
int CLrc::Findch( const char *str, unsigned char ch ) { int i = strlen( str ); int quan = 0; while( i-- ) { if( str[i] == ch ) { quan++; } } return quan; }
int CLrc::GetTime( const char *line ) { int min = 0, sec = 0, ms = 0; if(Findch(line, '.') == 0) { sscanf( line, "[%d:%d]", &min, &sec ); } else { sscanf( line, "[%d:%d.%d]", &min, &sec, &ms ); } return (min * 6000 + sec * 100 + ms ); }
void CLrc::ResolveLine( const char *line ) { char str[100]; int i = 0, j = 0, last = 0; strcpy( str, line ); if (Findch(str, '[') != Findch(str, ']') ) { return; } for (i = 0; i <= strlen(str); i++ ) { if (str[i] == ']') { char buf[50] = ""; int t = 0; for (j = last; j <= i; j++ ) { buf[t++] = str[j]; } ld[ ldpoint ].time = GetTime( buf ) * 10; GetLineData( line, ld[ldpoint].data); ldpoint++; i++; last = i; } } }
void CLrc::Sort() { int i = 0, j; for( ; i < ldpoint - 1; i++ ) { for( j = i; j < ldpoint - i; j++ ) { if( ld[j].time > ld[j + 1].time ) { LineData t; t = ld[j]; ld[j] = ld[j + 1]; ld[j + 1] = t; } } } }
|