예제 #1
0
/** ini_browse()
 * \param Callback    a pointer to a function that will be called for every
 *                    setting in the INI file.
 * \param UserData    arbitrary data, which the function passes on the the
 *                    \c Callback function
 * \param Filename    the name and full path of the .ini file to read from
 *
 * \return            1 on success, 0 on failure (INI file not found)
 *
 * \note              The \c Callback function must return 1 to continue
 *                    browsing through the INI file, or 0 to stop. Even when the
 *                    callback stops the browsing, this function will return 1
 *                    (for success).
 */
int  ini_browse(INI_CALLBACK Callback, const void *UserData, const TCHAR *Filename)
{
  TCHAR LocalBuffer[INI_BUFFERSIZE];
  TCHAR *sp, *ep;
  int lenSec, lenKey;
  enum quote_option quotes;
  INI_FILETYPE fp;

  if (Callback == NULL)
    return 0;
  if (!ini_openread(Filename, &fp))
    return 0;

  LocalBuffer[0] = '\0';   /* copy an empty section in the buffer */
  lenSec = _tcslen(LocalBuffer) + 1;
  for ( ;; ) {
    if (!ini_read(LocalBuffer + lenSec, INI_BUFFERSIZE - lenSec, &fp))
      break;
    sp = skipleading(LocalBuffer + lenSec);
    /* ignore empty strings and comments */
    if (*sp == '\0' || *sp == ';' || *sp == '#')
      continue;
    /* see whether we reached a new section */
    ep = _tcschr(sp, ']');
    if (*sp == '[' && ep != NULL) {
      *ep = '\0';
      save_strncpy(LocalBuffer, sp + 1, INI_BUFFERSIZE, QUOTE_NONE);
      lenSec = _tcslen(LocalBuffer) + 1;
      continue;
    } /* if */
    /* not a new section, test for a key/value pair */
    ep = _tcschr(sp, '=');    /* test for the equal sign or colon */
    if (ep == NULL)
      ep = _tcschr(sp, ':');
    if (ep == NULL)
      continue;               /* invalid line, ignore */
    *ep++ = '\0';             /* split the key from the value */
    striptrailing(sp);
    save_strncpy(LocalBuffer + lenSec, sp, INI_BUFFERSIZE - lenSec, QUOTE_NONE);
    lenKey = _tcslen(LocalBuffer + lenSec) + 1;
    /* clean up the value */
    sp = skipleading(ep);
    sp = cleanstring(sp, &quotes);  /* Remove a trailing comment */
    save_strncpy(LocalBuffer + lenSec + lenKey, sp, INI_BUFFERSIZE - lenSec - lenKey, quotes);
    /* call the callback */
    if (!Callback(LocalBuffer, LocalBuffer + lenSec, LocalBuffer + lenSec + lenKey, UserData))
      break;
  } /* for */

  (void)ini_close(&fp);
  return 1;
}
예제 #2
0
파일: cddb.c 프로젝트: opsemanthano/mp3splt
char *get_cue_value(char *in, char *out, int maxlen)
{
	char *ptr_b, *ptr_e;
	ptr_b = strchr(in, '"');
	if (ptr_b==NULL)
		return NULL;
	ptr_e = strchr(ptr_b+1, '"');
	if (ptr_e==NULL)
		return NULL;
	*ptr_e='\0';
	strncpy(out, ptr_b, maxlen);
	cleanstring(out);
	return out;
}
예제 #3
0
int main (int argc, char const *argv[]) {
	char test[2][200] = {{"  \n  testetestet teste teste \n teste teste \n\n"}, "test"};
	int numoftests = 2;
	int size = 0;
	int i = 0;

	for(i = 1; i <= numoftests; i++) {
		printf("------ Test number %d:\n", i);
		printf("%s\n", test[i-1]);
		printf("------\n");

		size = cleanstring(test[i-1], 200);

		printf("%s\n", test[i-1]);
		printf("------ End of Test number %d\n", i);
		printf("Number of characters, counting with null: %d\n\n", size);
	}
	return 0;
}
예제 #4
0
파일: morpheus.c 프로젝트: HCMID/morpheus
int main(int argc, char** argv)
{
	char		line[BUFSIZ*4];

	PrntFlags	flags = (PERSEUS_FORMAT|STRICT_CASE);
	int			rval;
	long		freemem = 0;
	int			c;
	int			errflg = 0;

	/* process arguments */
	while (!errflg && (c = getopt(argc, argv, ARGS)) != -1)
	{
		switch (c)
		{
		  case 'c':
			flags |= CHECK_PREVERB;
			break;
		  case 'I':
			set_lang(ITALIAN);
			break;
		  case 'L':
			set_lang(LATIN);
			break;
		  case 'i':
			flags |= SHOW_FULL_INFO;
			break;
		  case 'x':
			flags |= LEXICON_OUTPUT;
			break;
		  case 'V':
			flags |= VERBS_ONLY;
			break;
		  case 'S':
			flags &= ~(STRICT_CASE);
			break;
		  case 'p':
			flags |= PARSE_FORMAT;
			break;
		  case 'm':
			SetMorphPath(optarg);
			break;
		  default:
			errflg++;
			break;
		}
	}

	/* process input */
	int	nwords = 0;
	while (1)
	{
		/* if processed command line args */
		if (argc == 0)
			break;

		/* if args left */
		if (optind < argc)
		{
			/* use remaining args as line */
			*line = '\0';
			while (optind < argc)
			{
				if (*line != '\0')
					strcat(line, " ");
				strcat(line, argv[optind++]);
			}

			/* exit loop next time through */
			argc = 0;
		}
		else
		{
			/* get line from stdin, minus trailing newline */
			if (!fgets(line, BUFSIZ * 4, stdin))
			{
				/* exit loop if no more input */
				break;
			}
			if (line[strlen(line) - 1] == '\n')
				line[strlen(line) - 1] = '\0';
		}

		/* for each word in line */
		char*	word = strtok(line, " \t");
		for (; word; word = strtok(NULL, " \t"))
		{
			cleanstring(word);
			if (!*word)
				continue;
			if (nwords == 0)
				printf("<words>\n");

			if (!alpheiosCheckstring(word, flags, stdout))
			{
				printf("<unknown xml:lang=");
				if (cur_lang() == GREEK)
					printf("\"grc-x-beta\"");
				else if (cur_lang() == LATIN)
					printf("\"lat\"");
				printf(">%s</unknown>\n", word);
			}
			++nwords;
		}
	}

	if (nwords == 0)
	{
		printf("<error>No words</error>\n");
		return 2;
	}
	else
	{
		printf("</words>\n");
	}

	return 0;
}
예제 #5
0
static int getkeystring(INI_FILETYPE *fp, const TCHAR *Section, const TCHAR *Key,
                        int idxSection, int idxKey, TCHAR *Buffer, int BufferSize)
{
  TCHAR *sp, *ep;
  int len, idx;
  enum quote_option quotes;
  TCHAR LocalBuffer[INI_BUFFERSIZE];

  assert(fp != NULL);
  /* Move through file 1 line at a time until a section is matched or EOF. If
   * parameter Section is NULL, only look at keys above the first section. If
   * idxSection is postive, copy the relevant section name.
   */
  len = (Section != NULL) ? _tcslen(Section) : 0;
  if (len > 0 || idxSection >= 0) {
    idx = -1;
    do {
      if (!ini_read(LocalBuffer, INI_BUFFERSIZE, fp))
        return 0;
      sp = skipleading(LocalBuffer);
      ep = _tcschr(sp, ']');
    } while (*sp != '[' || ep == NULL || (((int)(ep-sp-1) != len || _tcsnicmp(sp+1,Section,len) != 0) && ++idx != idxSection));
    if (idxSection >= 0) {
      if (idx == idxSection) {
        assert(ep != NULL);
        assert(*ep == ']');
        *ep = '\0';
        save_strncpy(Buffer, sp + 1, BufferSize, QUOTE_NONE);
        return 1;
      } /* if */
      return 0; /* no more section found */
    } /* if */
  } /* if */

  /* Now that the section has been found, find the entry.
   * Stop searching upon leaving the section's area.
   */
  assert(Key != NULL || idxKey >= 0);
  len = (Key != NULL) ? (int)_tcslen(Key) : 0;
  idx = -1;
  do {
    if (!ini_read(LocalBuffer,INI_BUFFERSIZE,fp) || *(sp = skipleading(LocalBuffer)) == '[')
      return 0;
    sp = skipleading(LocalBuffer);
    ep = _tcschr(sp, '='); /* Parse out the equal sign */
    if (ep == NULL)
      ep = _tcschr(sp, ':');
  } while (*sp == ';' || *sp == '#' || ep == NULL || (((int)(skiptrailing(ep,sp)-sp) != len || _tcsnicmp(sp,Key,len) != 0) && ++idx != idxKey));
  if (idxKey >= 0) {
    if (idx == idxKey) {
      assert(ep != NULL);
      assert(*ep == '=' || *ep == ':');
      *ep = '\0';
      striptrailing(sp);
      save_strncpy(Buffer, sp, BufferSize, QUOTE_NONE);
      return 1;
    } /* if */
    return 0;   /* no more key found (in this section) */
  } /* if */

  /* Copy up to BufferSize chars to buffer */
  assert(ep != NULL);
  assert(*ep == '=' || *ep == ':');
  sp = skipleading(ep + 1);
  sp = cleanstring(sp, &quotes);  /* Remove a trailing comment */
  save_strncpy(Buffer, sp, BufferSize, quotes);
  return 1;
}
예제 #6
0
/*** End of Protocol Prototype ***/
int handle_command(int sock, char *cmd, FILE *fptr, struct config_params *params, struct city **headlist, int *auth_success)
{
    int counter;
    char commandname[MAXLEN];//also used for return:command
    char tablename[MAXLEN];//also used for return:status
    char keyname[MAXLEN];
    char valuename[MAXLEN];//also used for return:secondstatus (may be detail OR value)
    char retline[MAXLEN];
    char encoded_value[MAXLEN];
    //char returncmd[MAXLEN+1]; //To send back to client
    time_t rawtime;
    struct tm * timeinfo;
    int index = 0;
    int tempcmd = 1;
    int tempcommand = 0;
    int i = 0;//common-purpose counter
    printf("command received: %s\n", cmd);
    while(cmd[tempcmd] != '&'){
	if(cmd[tempcmd] != '&'){
	    commandname[tempcommand] = cmd[tempcmd];
	    tempcommand++;
	    tempcmd++;
	}
    }
    commandname[tempcommand] = '\0';
    tempcommand = 0;
    tempcmd++;
    tempcmd++;
    while(cmd[tempcmd] != '^'){
	tablename[tempcommand] = cmd[tempcmd];
	tempcommand++;
	tempcmd++;
    }
    tablename[tempcommand] = '\0';
    tempcommand = 0;
    tempcmd++;
    while(cmd[tempcmd] != '\0'){
	valuename[tempcommand] = cmd[tempcmd];
	tempcommand++;
	tempcmd++;
    }
    valuename[tempcommand] = '\0';
    tempcommand = 0;
    if(strcmp(commandname, "QUERY") == 0){
	printf("command is: %s\n", commandname);
	printf("table is: %s\n", tablename);
	printf("valuename: %s\n", valuename);
    }
    else {
	decode_line(cmd, commandname, tablename, keyname, valuename, &counter);
    }
    //printf("commandname: %s\n", commandname);
    //printf("tablename: %s\n", tablename);
    //printf("keyname: %s\n", keyname);
    //printf("valuename: %s\n", valuename);
    char namegen[MAXLEN+1];
    if(LOGGING==2){
	char tempstr[MAXLEN+1];
	time(&rawtime);
	timeinfo=localtime(&rawtime);
	sprintf(namegen,"%.4d-%.2d-%.2d-%.2d-%.2d-%.2d: ",timeinfo->tm_year+1900,timeinfo->tm_mon+1,timeinfo->tm_mday,timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
	//sprintf(tempstr,"Processing command '%s'\n",commandname);
	printf("Processing line \"%s\"\n", cmd);
	logger(fptr,namegen);//Timestamp
	logger(fptr,tempstr);
    }
    else if(LOGGING==1){
	time(&rawtime);
	timeinfo=localtime(&rawtime);
	sprintf(namegen,"%.4d-%.2d-%.2d-%.2d-%.2d-%.2d: ",timeinfo->tm_year+1900,timeinfo->tm_mon+1,timeinfo->tm_mday,timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
	printf("%s",namegen);//Timestamp
	//printf("Processing command '%s'\n",commandname);
	printf("Processing line \"%s\"\n", cmd);
    }
    
    if (strcmp(commandname, "SET") == 0){
	
    	pthread_mutex_lock( &setMutex );
    	
	//SET below
	// check if authorized
	if ((*auth_success) == 0){
	    //printf("inside if statement.\n");
	    //strncpy(success, "AUTH", sizeof(success));
	    //sendall(sock, success, sizeof(success));
	    cleanstring(tablename);
	    cleanstring(valuename);
	    sprintf(tablename, "FAIL");
	    sprintf(valuename, "AUTH");
	    encode_line(commandname, tablename, valuename, retline);
	    sendall(sock, retline, sizeof(retline));
	}
	else {
	    index = find_index(params->tablelist, tablename);
	    if(index != -1){
		//name found
		struct city *head = headlist[index];
		struct city *temp = find_city(head, keyname);
		printf("valuename: %s\n", valuename);
		if(temp == NULL){
		    //entry doesn't exist
		    if(strcmp(valuename, "@NULL@?") == 0){
			//deleting a key that doesn't exist
			//sprintf(success, "SET$KEY");
			//sendall(sock, success, sizeof(success));
			cleanstring(tablename);
			cleanstring(valuename);
			sprintf(tablename, "FAIL");
			sprintf(valuename, "KEY");
			encode_line(commandname, tablename, valuename, retline);
			sendall(sock, retline, sizeof(retline));
		    }
		    else{
			int column_count = 0;
			column_count = count_column(valuename);		
			if (params->num_columns[index] == column_count)
			    {
				insert_city(headlist[index], keyname, valuename);
				//VALUE IS INSERTED AS A STRING
				cleanstring(tablename);
				cleanstring(valuename);
				sprintf(tablename, "SUCCESS");
				sprintf(valuename, "CREATE");
				encode_line(commandname, tablename, valuename, retline);
				sendall(sock, retline, sizeof(retline));
			    }
			else
			    {
				sprintf(retline, "SET FAIL COLUMN\n");
				sendall(sock, retline, sizeof(retline));						
			    }
		    }
		}
		else {
		    //entry exists, temp != NULL
		    if(strcmp(valuename, "@NULL@?") == 0){
			//delete
			//printf("delete entry\n");
			delete_city(&head, keyname);
			temp = NULL;
			//sprintf(success, "SET$SUCCESS");
			//sendall(sock, success, sizeof(success));
			cleanstring(tablename);
			cleanstring(valuename);
			sprintf(tablename, "SUCCESS");
			sprintf(valuename, "DELETE");
			encode_line(commandname, tablename, valuename, retline);
			sendall(sock, retline, sizeof(retline));
		    }
		    else{
			//modify record
			//strncpy(temp->population, , sizeof(temp->population));
			//sprintf(success, "SET$SUCCESS");
			//sendall(sock, success, sizeof(success));
			//printf("valuename: %s\n", valuename);
			if (counter == temp->counter || counter == 0)
			    {
				int column_count = 0;
				temp = find_city(headlist[index], keyname);
				column_count = count_column(valuename);
				
				if (temp->numocolumns == column_count)
				    {
					modify_city(temp, valuename);
					cleanstring(tablename);
					cleanstring(valuename);
					sprintf(tablename, "SUCCESS");
					sprintf(valuename, "MODIFY");
					encode_line(commandname, tablename, valuename, retline);
					sendall(sock, retline, sizeof(retline));							
				    }
				else
				    {
					sprintf(retline, "SET FAIL COLUMN\n");
					sendall(sock, retline, sizeof(retline));						
				    }
			    }
			else
			    {
				char temp[100];
				sprintf(temp, "%s", "SET FAIL COUNTER");
				sendall(sock, temp, sizeof(temp));	
			    }
		    }
		}
	    }
	    else {
		//table doesn't exist
		//strncpy(fail, "SET$TABLE", sizeof(fail));
		//sendall(sock, fail, sizeof(fail));
		cleanstring(tablename);
		cleanstring(valuename);
		sprintf(tablename, "FAIL");
		sprintf(valuename, "TABLE");
			encode_line(commandname, tablename, valuename, retline);
			sendall(sock, retline, sizeof(retline));
	    }
	}
	
	pthread_mutex_unlock( &setMutex );
    }
    
    // For now, just send back the command to the client.
    //command cases: get, set
    
    else if(strcmp(commandname, "AUTH") == 0){
	//AUTH below
	if(strcmp(tablename, params->username) == 0){
	    if (strcmp(keyname, params->password) == 0){
		(*auth_success) = 1;
		printf("authenticated\n");
		//strncpy(success, "AUTH$SUCCESS", sizeof(success));
		//sendall(sock, success, sizeof(success));
		cleanstring(tablename);
		cleanstring(valuename);
		sprintf(tablename, "SUCCESS");
		sprintf(valuename, " ");
		encode_line(commandname, tablename, valuename, retline);
		sendall(sock, retline, sizeof(retline));
	    }
	    else {
		printf("authenticated\n");
		cleanstring(tablename);
		cleanstring(valuename);
		sprintf(tablename, "FAIL");
		sprintf(valuename, " ");
		encode_line(commandname, tablename, valuename, retline);
		sendall(sock, retline, sizeof(retline));
		//strncpy(fail, "AUTH$FAIL", sizeof(fail));
		//sendall(sock, fail, sizeof(fail));
			}
	}
	else {
	    cleanstring(tablename);
	    cleanstring(valuename);
	    sprintf(tablename, "FAIL");
	    sprintf(valuename, "");
	    encode_line(commandname, tablename, valuename, retline);
	    sendall(sock, retline, sizeof(retline));
	    //strncpy(fail, "AUTH$FAIL", sizeof(fail));
	    //sendall(sock, fail, sizeof(fail));
	}
    }
    else if(strcmp(commandname, "GET") == 0){
	// check if authenticated
	if ((*auth_success) == 0){
	    //not authenticated
	    cleanstring(tablename);
	    cleanstring(valuename);
	    sprintf(tablename, "FAIL");
	    sprintf(valuename, "AUTH");
	    encode_line(commandname, tablename, valuename, retline);
	    sendall(sock, retline, sizeof(retline));
	    //strncpy(success, "AUTH", sizeof(success));
	    //sendall(sock, success, sizeof(success));
	}
	printf("tablename: %s\n", tablename);
	index=find_index(params->tablelist, tablename);
	printf("index: %d\n", index);
	if(index != -1) {
	    //name found
	    struct city *head = (headlist)[index];
	    print_city(head);
	    struct city *temp = find_city(head, keyname);
	    //printf("temp key: %s\n", temp->name);
	    //printf("temp colnum: %d\n", temp->numocolumns);
	    if(temp == NULL){
		//key doesn't exist
		//strncpy(fail, "GET$KEY$FAIL$FAIL", sizeof(fail));
		//sendall(sock, fail, sizeof(fail))
		
		cleanstring(tablename);
		cleanstring(valuename);
		sprintf(tablename, "FAIL");
		sprintf(valuename, "KEY");
		//printf("key doesn't exist\n");
		encode_line(commandname, tablename, valuename, retline);
		sendall(sock, retline, sizeof(retline));
		
	    }
	    else {
		//key exists
		//printf("columnlist: %s\n", temp->columnlist);
		//printf("encoded_value: %s\n", encoded_value);
		//printf("temp->numocolumns: %d\n", temp->numocolumns);
		cleanstring(encoded_value);
		encode_retval(temp->columnlist, encoded_value, temp->numocolumns);
		cleanstring(tablename);
		cleanstring(valuename);
		sprintf(tablename, "SUCCESS");
		sprintf(valuename, "%s", encoded_value);
		printf("serverside\n");
		printf("tablename: %s\n", tablename);
		printf("valuename: %s\n", valuename);
		encode_line(commandname, tablename, valuename, retline);

		
		char temp_str[100];
		char number[100];
		strcpy(temp_str, " COUNTER ");
		sprintf(number, "%d", temp->counter);
		strcat(temp_str, number);
		strcat(retline, temp_str);
		sendall(sock, retline, sizeof(retline));
		//sprintf(success, "GET$SUCCESS$%s$%s", temp->name, temp->population);
		//sendall(sock, success, sizeof(success));
	    }
	}
	else {
	    //table doesn't exist
	    cleanstring(tablename);
	    cleanstring(valuename);
	    sprintf(tablename, "FAIL");
	    sprintf(valuename, "TABLE");
	    encode_line(commandname, tablename, valuename, retline);
	    sendall(sock, retline, sizeof(retline));
	    //strncpy(fail, "GET$TABLE$FAIL$FAIL", sizeof(fail));
	    //sendall(sock, fail, sizeof(fail));
	}
    }
    else if(strcmp(commandname, "QUERY") == 0) {//query
	//what to do?
	//first allocate a keylist
	//then do searching
	//when done, encode the keylist and send back to client
	puts("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$HANDLEQUERY$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
	struct queryarg *testque = (struct queryarg *)malloc(sizeof(struct queryarg));
	int numque = 0;
	int questatus = 0;
	char server_keylist[1000][1024];
	for(i = 0; i < 1000; i++){
	    cleanstring(server_keylist[i]);
	}
	strncpy(server_keylist[0], "testcopy", sizeof(server_keylist[0]));
	index = find_index(params->tablelist, tablename);
	//printf("index is %d\n", index);
	if(index != -1){
	    //found matching name in tablelist
	    struct city *node = (headlist)[index];
	    numque = query_argument(testque, valuename);
	    //printf("numque = %d\n", numque);
	    testque->max_keys++;
	    questatus = query_write(server_keylist, testque, node, &testque->max_keys, &numque);
	    if(questatus == -1) {
		//printf("query incorrect\n");
	    }
	    else if(questatus == 0) {
		//printf("query correct\n");
		i = 1;
		if(server_keylist[i][0] == '\0') {
		    printf("no matching keys detected.\n");
		}
		while(server_keylist[i][0] != '\0'){
		    printf("keylist[%d]: %s\n", i, server_keylist[i]);
		    i++;
		}
		if(i > testque->max_keys){
		    i = testque->max_keys;
		}
		encode_queryret(i, server_keylist, retline);
		//printf("query retline: %s\n", retline);
		sendall(sock, retline, sizeof(retline));
	    }
	    free(testque);
	    testque = NULL;
	}
	else{
	    //table doesn't exist
	    printf("table doesn't exist\n");
	    cleanstring(retline);
	    sprintf(retline, "&QUERY&$FAIL$^TABLE^");
	    sendall(sock, retline, sizeof(retline));
	}
	puts("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%HANDLEQUERY_END%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
    }
    sendall(sock, "\n", 1);
    return 0;
}
예제 #7
0
파일: cddb.c 프로젝트: opsemanthano/mp3splt
int get_cddb_splitpoints (unsigned char *file, splt_state *state)
{

	FILE *file_input;
	char line[512];
	char prev[10];
	char *number, *c;
	int tracks = 0, i, j;
	
	if (!(file_input=fopen(file, "r"))) {
		perror(file);
		exit(1);
	}

	fseek (file_input, 0, SEEK_SET);
	do {
		if ((fgets(line, 512, file_input))==NULL)
			return -1;
		number = strstr(line, "Track frame offset");
	} while (number == NULL);

	memset(prev, 0, 10);
	
	do {
		if ((fgets(line, 512, file_input))==NULL)
			return -1;
		line[strlen(line)-1] = '\0';
		i = 0;
		while ((isdigit(line[i])==0) && (line[i]!='\0')) {
			i++;
			number = line + i;
		}
		if (number == (line + strlen(line))) break;
		else state->splitpoints[tracks++] = (float) atof (number);
	} while (tracks<MAXTRACKS);
	for (i=tracks-1; i>=0; i--) {
		state->splitpoints[i] -= state->splitpoints[0];
		state->splitpoints[i] /= 75;
	}
	state->splitpoints[tracks] = (float) -1.f; // End of file

	j=0;
	do {
		char temp[10];
		memset(temp, 0, 10);
		if ((fgets(line, 512, file_input))==NULL)
			return -1;
		line[strlen(line)-1] = '\0';
		if (strlen(line)>0)
			if (line[strlen(line)-1]=='\r') line[strlen(line)-1]='\0';
		if (j==0) {
			if (strstr(line, "DTITLE")==NULL) continue;
		}
		else
			if (strstr(line, "TTITLE")==NULL)
				continue;
		if ((number=strchr(line, '='))==NULL) return -1;
		if (j>0) {
			int len = number-line;
			if (len>10) len = 10;
			strncpy(temp, line, len);
			if ((c=strchr(number, '/'))!=NULL) number = c + 1;
			number = cleanstring (number);
		}
		if (strlen(++number)>255) number[255]='\0';
		if ((j>0)&&(strstr(number, "Data")!=NULL) && (strstr(number, "Track")!=NULL)) {
			state->splitpoints[j-1]=state->splitpoints[j];
			tracks -= 1;
		}
		else
		{
			if ((j>0)&&(strcmp(temp, prev)==0))
				strncat(state->fn[j-1], number, 255-strlen(state->fn[j-1]));
			else strncpy(state->fn[j++], number, 255);
		}
		strncpy(prev, temp, 10);
	} while (j<=tracks);

	while ((fgets(line, 512, file_input))!=NULL) {
		line[strlen(line)-1] = '\0';
		if (strlen(line)>0)
			if (line[strlen(line)-1]=='\r') line[strlen(line)-1]='\0';
		if (strstr(line, "EXTD")==NULL) continue;
		else {
			if ((number=strchr(line, '='))==NULL) break;
			else {
				if ((c=strstr(number, "YEAR"))!=NULL)
					strncpy(state->id.year, c+6, 4);
				if ((c=strstr(number, "ID3G"))!=NULL) {
					strncpy(line, c+6, 3);
					state->id.genre= (unsigned char) atoi(line);
				}
				break;
			}
		}
	}

	i=0;
	while ((state->fn[0][i]!='/') && (state->fn[0][i]!='\0')&&(i<30)) {
		state->id.artist[i] = state->fn[0][i];
		i++;
	}
	state->id.artist[i-1]='\0';
	cleanstring(state->id.artist);
	j=0;
	i += 2;
	do
		state->id.album[j++]=state->fn[0][i++];
	while ((state->fn[0][i]!='\0')&&(j<30));

	fclose(file_input);

	return tracks;
}
예제 #8
0
파일: cddb.c 프로젝트: opsemanthano/mp3splt
int get_cue_splitpoints(unsigned char *file, splt_state *state)
{
	FILE *file_input;
	char line[512];
	char *ptr, *dot;
	int tracks = -1, check=1;

        ptr = dot = NULL;

	if (!(file_input=fopen(file, "r"))) {
		perror(file);
		exit(1);
	}

	fseek (file_input, 0, SEEK_SET);

	while (fgets(line, 512, file_input)!=NULL)
	{
		int type=0;

		if ((strstr(line, "TRACK")!=NULL)&&(strstr(line, "AUDIO")!=NULL))
			type=1;
		else if (strstr(line, "TITLE")!=NULL)
			type=2;
			else if (strstr(line, "PERFORMER")!=NULL)
				type=3;
				else if ((ptr=strstr(line, "INDEX 01"))!=NULL)
					type=4;

		switch (type)
		{
			case 0:	break;
			case 1:	if (tracks==-1) tracks = 0;
						if (check) tracks++;
						else return -1;
						check=0;
					  	break;
			case 2:	if (tracks==-1)
					{
						if (get_cue_value(line, state->id.album, 30)==NULL)
							return -1;
					}
					else
					{
						if (tracks > 0)
								if (get_cue_value(line, state->fn[tracks], 255)==NULL)
									return -1;
					}
					break;
			case 3:	if (tracks==-1)
					{
						if (get_cue_value(line, state->id.artist, 30)==NULL)
							return -1;
					}
					else
					{
						if (tracks>0)
							if (get_cue_value(line, state->performer[tracks], 127)==NULL)
								return -1;
					}
					break;
			case 4:	line[strlen(line)-1]='\0';
						ptr += 9;
                                                if ((dot = strchr(ptr, ':'))==NULL)
                                                	return -1;
						ptr[dot-ptr] = ptr[dot-ptr+3] = '.';
						cleanstring(ptr);
						if (tracks>0)
						{
							float seconds = c_seconds(ptr);
							if (seconds==-1)
								return -1;
							state->splitpoints[tracks-1] = floorf(seconds);
							seconds = (seconds - state->splitpoints[tracks-1])*4/3;
							state->splitpoints[tracks-1] += seconds;
							check=1;
						}
						break;
			default:	break;
		}
	}

	if (!check) tracks--;

	if (tracks>0) state->splitpoints[tracks] = (float) -1.f; // End of file

	fclose(file_input);

	return tracks;
}