summary refs log blame commit diff stats
path: root/plugins/mpcinfo/functions.c
blob: ff2d563e705d1b75200e6c594808eb3cae4dd007 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13












                                                                        
                                                                             








































































































                                                                                                 
                                                              














































                                                                                            
/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

/*
typedef int (*MYPROC)(HWND,HWND,char*,char*,BOOL,BOOL); 

int dllProc(char *name, char *data){
    HINSTANCE hinstLib; 
    hinstLib = LoadLibrary("mpcinfo");
    //MYPROC proc;
    int res;
    if (hinstLib != NULL){
       //proc = ;
       if ((MYPROC) GetProcAddress(hinstLib, name)!=NULL){
          res=(MYPROC)(NULL,NULL,data,NULL,TRUE,TRUE);
       }
       else{fprintf(stderr,"can't get proc: %s\n",name);res=-2;}
    }
    else{fprintf(stderr,"can't access dll\n");return -1;}
    FreeLibrary(hinstLib);
    return res;
}
*/

/*
int dllProc(char *name, char *data)
{
	static HMODULE lib = NULL;
	if (!lib)
	{
		lib = LoadLibraryA ("mpcinfo");
		if (!lib)
		{
			return FALSE;
		}
		FreeLibrary (lib);
	}

	return TRUE;
}
*/

char *split(char *text, char seperator){
     //if (DEBUG==1) putlog("splitting");
     int i;int pos=-1;
     for (i=0;i<strlen(text);i++){
         if (text[i]==seperator){pos=i;i=strlen(text)+1;}
     }
     if (pos==-1) return text;
     text[pos]=0;
     return &(text[pos+1]);
}

int endsWith(char *text, char *suffix){
    char *tmp=strstr(text,suffix);
    if (tmp==NULL) return 0;
    if (strlen(tmp)==strlen(suffix)) return 1;
    return 0;
}

int inStr(char *s1, int sl1, char *s2){
    //if (DEBUG==1) putlog("checking instr");
	int i;int j;
	for(i=0;i<sl1-strlen(s2);i++){
		for (j=0;j<strlen(s2);j++){
			if (s1[i+j]!=s2[j]) j=strlen(s2)+2;
		}
		if (j==strlen(s2)) return i;
	}
	return -1;
}

static char *subString(char *text, int first, int length, int spcKill){
//if (DEBUG==1) putlog("creating substring");
	char *ret=(char*) calloc (length+1,sizeof(char)); //malloc(sizeof(char)*(length+1));
	int i;
	ret[length]=0;
	for (i=0;i<length;i++){
		ret[i]=text[i+first];
		//if (ret[i]==0) ret[i]='0';
	}
	if (spcKill==1){
	   for (i=length-1;i>=0;i--){
           if (ret[i]==32) ret[i]=0;
           else i=-1;
       }
    }
    //if (DEBUG==1) putlog("substring created");
	return ret;
}

static char *substring(char *text, int first, int length){return subString(text,first,length,0);}


char *readLine(FILE *f){
     //if (DEBUG==1) putlog("reading line from file");
     char *buffer=(char*)calloc(1024,sizeof(char)); //malloc(sizeof(char)*1024);
     int pos=0;
     int cc=0;
     while((cc!=EOF)&&(pos<1024)&&(cc!=10)){
          cc=fgetc(f);
          if ((cc!=10)&&(cc!=13)){
             if (cc==EOF) buffer[pos]=0;
             else buffer[pos]=(char)cc;pos++;
          }
     }
     if (buffer[pos]==EOF) hexchat_printf(ph,"EOF: %i\n",pos);
     return buffer;
}

char *toUpper(char *text){
     //if (DEBUG==1) putlog("converting text to upper case");
     char *ret=(char*) calloc(strlen(text)+1,sizeof(char));
     int i;
     for (i=0;i<strlen(text);i++) ret[i]=toupper(text[i]);
     ret[strlen(text)]=0;
     //if (DEBUG==1) putlog("uc done");
     return ret;
}

static char *str3cat(char *s1, char *s2, char *s3){
       //if (DEBUG==1) putlog("cating 3 strings");
       char *ret=(char*)calloc(strlen(s1)+strlen(s2)+strlen(s3)+1,sizeof(char));
       strcpy(ret,s1);strcat(ret,s2);strcat(ret,s3);
       ret[strlen(s1)+strlen(s2)+strlen(s3)]=0;
       //if (DEBUG==1) putlog("strings cated");
       return ret;
}

char *replace(char *text, char *from, char *to){
     //if (DEBUG==1) putlog("replacing");
     char *ret=(char*)calloc( strlen(text)+(strlen(to)-strlen(from)),sizeof(char));
     char *left;
     char *right;
     int pos=inStr(text,strlen(text),from);
     if (pos!=-1){
           left=substring(text,0,pos);
           right=substring(text,pos+strlen(from),strlen(text)-(pos+strlen(from)));          
           ret=str3cat(left,to,right);
           return replace(ret,from,to);
     }
     //if (DEBUG==1) putlog("replaced");
     return text;
}

char *intReplaceF(char *text, char *from, int to, char *form){
     //if (DEBUG==1) putlog("replaceF");
     char *buffer=(char*) calloc(16,sizeof(char));
     sprintf(buffer,form,to);
     //if (DEBUG==1) putlog("replaceF done");
     return replace(text,from,buffer);
}

char *intReplace(char *text, char *from, int to){return intReplaceF(text,from,to,"%i");}
>) { len+=tag[pos+strlen(info)+i]*iPow(255,3-i); } //printf("Tag-Length: %i\n",len); if (strcmp("COMM",info)!=0) return substring(tag,pos+7+strlen(info),len-1);//11 return substring(tag,pos+7+strlen(info),len-1);//11 //char *ct=substring(tag,pos+7+strlen(info),len-1);//11 //return substring(ct,strlen(ct)+1,len-1-strlen(ct)); //<-- do not understand, what i did here :( } struct tagInfo readID3V1(char *file){ //if (DEBUG==1) putlog("reading ID3V1"); FILE *f; struct tagInfo ret; int res, i, c, val; char *tag; char *id; char *tmp; tag = (char*) malloc(sizeof(char)*129); ret.artist=NULL; f=fopen(file,"rb"); if (f==NULL){ hexchat_print(ph,"file not found while trying to read id3v1"); //if (DEBUG==1) putlog("file not found while trying to read id3v1"); return ret; } //int offset=getSize(file)-128; res=fseek(f,-128,SEEK_END); if (res!=0) {printf("seek failed\n");fclose(f);return ret;} //long int pos=ftell(f); //printf("position= %li\n",pos); for (i=0;i<128;i++) { c=fgetc(f); if (c==EOF) {hexchat_printf(ph,"read ID3V1 failed\n");fclose(f);return ret;} tag[i]=(char)c; } fclose(f); //printf("tag readed: \n"); id=substring(tag,0,3); //printf("header: %s\n",id); if (strcmp(id,"TAG")!=0){hexchat_printf(ph,"no id3 v1 found\n");return ret;} ret.title=subString(tag,3,30,1); ret.artist=subString(tag,33,30,1); ret.album=subString(tag,63,30,1); ret.comment=subString(tag,97,30,1); tmp=substring(tag,127,1); //ret.genre=substring(tag,127,1); val=(int)tmp[0]; if (val<0)val+=256; //hexchat_printf(ph, "tmp[0]=%i (%i)",val,tmp[0]); if ((val<148)&&(val>=0)) ret.genre=GENRES[val];//#############changed else { ret.genre="unknown"; //hexchat_printf(ph, "tmp[0]=%i (%i)",val,tmp[0]); } //hexchat_printf(ph, "tmp: \"%s\" -> %i",tmp,tmp[0]); //hexchat_printf(ph,"genre \"%s\"",ret.genre); //if (DEBUG==1) putlog("id3v1 extracted"); return ret; } char *extractID3Genre(char *tag){ //if (DEBUG==1) putlog("extracting id3 genre"); if (tag[strlen(tag)-1]==')'){ tag[strlen(tag)-1]=0; tag=&tag[1]; return GENRES[str2int(tag)]; //return tag; } else{ int i; //hexchat_print(ph, "Using 2 criteria"); for (i=0;i<strlen(tag);i++){ if (tag[i]==')'){ tag=&tag[i]+1;return tag;} //return tag; } } return "[152] failed"; } struct tagInfo readID3V2(char *file){ //if (DEBUG==1) putlog("reading id3v2"); FILE *f; int i, c, len; char header[10]; char *tag; struct tagInfo ret; f = fopen(file,"rb"); //hexchat_printf(ph,"file :%s",file); if (f==NULL) { hexchat_print(ph,"file not found whilt trying to read ID3V2"); //if (DEBUG==1)putlog("file not found while trying to read ID3V2"); return ret; } ret.artist=NULL; for (i=0;i<10;i++){ c=fgetc(f); if (c==EOF){ //putlog("found eof while reading id3v2"); return ret; } header[i]=(char)c; } if (strstr(header,"ID3")==header){ //hexchat_printf(ph,"found id3v2\n"); len=0; for (i=6;i<10;i++) len+=(int)header[i]*iPow(256,9-i); //char *tag=(char*)malloc(sizeof(char)*len); tag=(char*) calloc(len,sizeof(char)); //malloc(sizeof(char)*len); for (i=0;i<len;i++){c=fgetc(f);tag[i]=(char)c;} //hexchat_printf(ph,"tag length: %i\n",len); //hexchat_printf(ph,"tag: %s\n",tag); fclose(f); ret.comment=tagExtract(tag,len,"COMM"); //hexchat_printf(ph,"Comment: %s\n",ret.comment); ret.genre=tagExtract(tag,len,"TCON"); //if (strcmp(ret.genre,"(127)")==0) ret.genre="unknown"; //hexchat_printf(ph, "ret.genre = %s",ret.genre); if ((ret.genre!=NULL)&&(ret.genre[0]=='(')) ret.genre=extractID3Genre(ret.genre); //hexchat_printf(ph,"genre: %s\n",ret.genre); ret.title=tagExtract(tag,len,"TIT2"); //hexchat_printf(ph,"Title: %s\n",ret.title); ret.album=tagExtract(tag,len,"TALB"); //hexchat_printf(ph,"Album: %s\n",ret.album); ret.artist=tagExtract(tag,len,"TPE1"); //hexchat_printf(ph,"Artist: %s\n",ret.artist); } else{fclose(f);printf("no id3v2 tag found\n"); return ret;} //printf("id2v2 done\n"); //if (DEBUG==1) putlog("id3v2 readed"); return ret; } struct tagInfo readHeader(char *file){ //if (DEBUG==1) putlog("reading header"); FILE *f; //int buffer[5120]; int versionB, layerB, bitrateB, freqB, modeB; int header[4]; int count=0; int cc=0; struct tagInfo info; info.artist=NULL; f = fopen(file,"rb"); if (f==NULL) { hexchat_print(ph,"file not found while trying to read mp3 header"); //if (DEBUG==1) putlog("file not found while trying to read mp3 header"); return info; } //struct tagInfo tagv2 info=readID3V2(file); //struct tagInfo tagv1;//=readID3V1(file); //if (tagv2.artist!=NULL){info=tagv2;} //else { if (info.artist==NULL){ //printf("searching for id3v1\n"); //tagv1=readID3V1(file); info=readID3V1(file); //##################### } /* if (tagv1.artist!=NULL){ //printf("Artist: %s\nTitle: %s\nAlbum: %s\nComment: %s\nGenre: %s\n",tagv1.artist,tagv1.title,tagv1.album,tagv1.comment,tagv1.genre); info=tagv1; } */ while ((count<5120)&&(cc!=EOF)&&(cc!=255)) {cc=fgetc(f);count++;} if ((cc==EOF)||(count==5119)) printf("no header found\n"); else { //printf("located header at %i\n",count); header[0]=255; for (count=1;count<4;count++){ header[count]=fgetc(f); //printf("header[%i]=%i\n",count,header[count]); } versionB=(header[1]&8)>>3; layerB=(header[1]&6)>>1; bitrateB=(header[2]&240)>>4; //4 freqB=(header[2]&12)>>2;//2 modeB=(header[3]&192)>>6;//6 //printf("Mpeg: %i\nLayer: %i\nBitrate: %i\nFreq: %i\nMode: %i\n",versionB, layerB, bitrateB, freqB, modeB); //int Bitrate=RATES[versionB][layerB-1][bitrateB]; //int Freq=FREQS[versionB][freqB]; info.bitrate=RATES[versionB][layerB-1][bitrateB]; info.freq=FREQS[versionB][freqB]; info.mode=modeB; } fclose(f); //if (DEBUG==1) putlog("header readed"); return info; } /* static void printMp3Info(char *file){ //printf("\nScanning Mp3-File for Informations: %s\n",file); //printf("size:\t%10d byte\n",getSize(file)); struct tagInfo info =readHeader(file); printf("%s | %10d",file,getSize(file)); if (info.bitrate>0){ //printf("Bitrate: %i\nFreq: %i\nMode: %s\n",info.bitrate,info.freq,MODES[info.mode]); printf(" | %i kbps | %i kHz | %s",info.bitrate,info.freq,MODES[info.mode]); //if (info.artist!=NULL) printf("\nArtist: %s\nTitle: %s\nAlbum: %s\nComment: %s\nGenre: %s\n",info.artist,info.title,info.album,info.comment,info.genre); if (info.artist!=NULL) { printf("| %s | %s | %s | %s | %s",info.artist,info.title,info.album,info.comment,info.genre); //printf("| %s ",info.title);//,info.title,info.album,info.comment,info.genre } } printf("\n"); } */