Exemple #1
0
/* This is meant to find and open files for reading, possibly piping
   them through a decompressor. */
FILE *open_file(const char *name, int decompress, int noise_mode)
{
  FILE *fp;
  PathList *plp;
  int l;

  if (!name || !(*name))
    {
      ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Attempted to open nameless file.");
      return 0;
    }

  if (pathlist==NULL) {
    if(customPath[0]!='\0') {
        add_to_pathlist(customPath); }
    /* Generate path list */
#ifdef DEFAULT_PATH
    add_to_pathlist(DEFAULT_PATH);
#endif
#ifdef DEFAULT_PATH1
    add_to_pathlist(DEFAULT_PATH1);
#endif
#ifdef DEFAULT_PATH2
    add_to_pathlist(DEFAULT_PATH2);
#endif
#ifdef DEFAULT_PATH3
    add_to_pathlist(DEFAULT_PATH3);
#endif
  }

  /* First try the given name */

  strncpy(current_filename, name, PATH_MAX - 1);
  current_filename[PATH_MAX - 1]='\0';

  ctl->cmsg(CMSG_INFO, VERB_DEBUG, "Trying to open %s", current_filename);
  if ((fp=try_to_open(current_filename, decompress, noise_mode)))
    return fp;

#ifdef ENOENT
  if (noise_mode && (errno != ENOENT))
    {
      ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: %s", 
	   current_filename, strerror(errno));
      return 0;
    }
#endif

  plp=pathlist;
  if (name[0] != PATH_SEP)
    while (plp)  /* Try along the path then */
      {
	*current_filename=0;
	l=strlen(plp->path);
	if(l)
	  {
	    strcpy(current_filename, plp->path);
	    if(current_filename[l-1]!=PATH_SEP)
	      strcat(current_filename, PATH_STRING);
	  }
	strcat(current_filename, name);
	ctl->cmsg(CMSG_INFO, VERB_DEBUG, "Trying to open %s", current_filename);
	if ((fp=try_to_open(current_filename, decompress, noise_mode)))
	  return fp;
#ifdef ENOENT
	if (noise_mode && (errno != ENOENT))
	  {
	    ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: %s", 
		 current_filename, strerror(errno));
	    return 0;
	  }
#endif
	plp=plp->next;
      }
  
  /* Nothing could be opened. */

  *current_filename=0;
  
  if (noise_mode>=2)
    ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: %s", name, strerror(errno));
  
  return 0;
}
Exemple #2
0
static int read_config_file(const char *name)
{
  FILE *fp;
  char tmp[PATH_MAX], *w[MAXWORDS], *cp;
  ToneBank *bank=0;
  int i, j, k, line=0, words;
  static int rcf_count=0;
printf("read_config_file %s \n",name);
  if (rcf_count>50)
   {
    ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
      "Probable source loop in configuration files");
    return (-1);
   }

  if (!(fp=open_file(name, 1, OF_VERBOSE)))
   return -1;

  while (fgets(tmp, sizeof(tmp), fp))
  {
    line++;
    w[words=0]=strtok(tmp, " \t\r\n\240");
    if (!w[0] || (*w[0]=='#')) continue;
    while (w[words] && (words < MAXWORDS))
      {
        w[++words]=strtok(0," \t\r\n\240");
        if (w[words] && w[words][0]=='#') break;
      }
    if (!strcmp(w[0], "map")) continue;
    if (!strcmp(w[0], "dir"))
    {
      if (words < 2)
       {
        ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
          "%s: line %d: No directory given\n", name, line);
        return -2;
       }
      for (i=1; i<words; i++)
        add_to_pathlist(w[i]);
    }
  else if (!strcmp(w[0], "source"))
  {
    if (words < 2)
      {
        ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
          "%s: line %d: No file name given\n", name, line);
        return -2;
     }
    for (i=1; i<words; i++)
      {
        rcf_count++;
      read_config_file(w[i]);
        rcf_count--;
      }
  }
      else if (!strcmp(w[0], "default"))
  {
    if (words != 2)
      {
        ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
        "%s: line %d: Must specify exactly one patch name\n",
          name, line);
        return -2;
      }
    strncpy(def_instr_name, w[1], 255);
    def_instr_name[255]='\0';
  }
    else if (!strcmp(w[0], "drumset"))
  {
    if (words < 2)
      {
        ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
          "%s: line %d: No drum set number given\n", 
          name, line);
      return -2;
      }
    i=atoi(w[1]);
    if (i<0 || i>127)
     {
        ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
          "%s: line %d: Drum set must be between 0 and 127\n",
        name, line);
        return -2;
     }
    if (!drumset[i])
      {
        drumset[i]=safe_malloc(sizeof(ToneBank));
      memset(drumset[i], 0, sizeof(ToneBank));
     }
    bank=drumset[i];
  }
    else if (!strcmp(w[0], "bank"))
  {
    if (words < 2)
     {
        ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
          "%s: line %d: No bank number given\n", 
        name, line);
        return -2;
     }
    i=atoi(w[1]);
    if (i<0 || i>127)
      {
        ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
          "%s: line %d: Tone bank must be between 0 and 127\n",
        name, line);
        return -2;
      }
    if (!tonebank[i])
     {
      tonebank[i]=safe_malloc(sizeof(ToneBank));
        memset(tonebank[i], 0, sizeof(ToneBank));
      }
    bank=tonebank[i];
  }
      else {
  if ((words < 2) || (*w[0] < '0' || *w[0] > '9'))
    {
     ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
        "%s: line %d: syntax error\n", name, line);
     return -2;
    }
  i=atoi(w[0]);
  if (i<0 || i>127)
    {
      ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
        "%s: line %d: Program must be between 0 and 127\n",
        name, line);
      return -2;
    }
  if (!bank)
    {
      ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
       "%s: line %d: Must specify tone bank or drum set "
        "before assignment\n",
        name, line);
     return -2;
    }
  if (bank->tone[i].name)
    free(bank->tone[i].name);
  strcpy((bank->tone[i].name=safe_malloc(strlen(w[1])+1)),w[1]);
  bank->tone[i].note=bank->tone[i].amp=bank->tone[i].pan=
    bank->tone[i].strip_loop=bank->tone[i].strip_envelope=
      bank->tone[i].strip_tail=-1;

  for (j=2; j<words; j++)
    {
      if (!(cp=strchr(w[j], '=')))
        {
    ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: line %d: bad patch option %s\n",
      name, line, w[j]);
    return -2;
        }
      *cp++=0;
      if (!strcmp(w[j], "amp"))
      {
    k=atoi(cp);
    if ((k<0 || k>MAX_AMPLIFICATION) || (*cp < '0' || *cp > '9'))
      {
       ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
          "%s: line %d: amplification must be between "
         "0 and %d\n", name, line, MAX_AMPLIFICATION);
       return -2;
      }
    bank->tone[i].amp=k;
        }
      else if (!strcmp(w[j], "note"))
        {
    k=atoi(cp);
    if ((k<0 || k>127) || (*cp < '0' || *cp > '9'))
      {
       ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
         "%s: line %d: note must be between 0 and 127\n",
          name, line);
        return -2;
      }
    bank->tone[i].note=k;
      }
     else if (!strcmp(w[j], "pan"))
      {
    if (!strcmp(cp, "center"))
      k=64;
    else if (!strcmp(cp, "left"))
      k=0;
    else if (!strcmp(cp, "right"))
      k=127;
    else
      k=((atoi(cp)+100) * 100) / 157;
    if ((k<0 || k>127) ||
       (k==0 && *cp!='-' && (*cp < '0' || *cp > '9')))
      {
       ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
         "%s: line %d: panning must be left, right, "
         "center, or between -100 and 100\n",
         name, line);
       return -2;
      }
    bank->tone[i].pan=k;
      }
     else if (!strcmp(w[j], "keep"))
      {
    if (!strcmp(cp, "env"))
      bank->tone[i].strip_envelope=0;
    else if (!strcmp(cp, "loop"))
      bank->tone[i].strip_loop=0;
    else
      {
        ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
          "%s: line %d: keep must be env or loop\n", name, line);
       return -2;
      }
      }
     else if (!strcmp(w[j], "strip"))
      {
    if (!strcmp(cp, "env"))
      bank->tone[i].strip_envelope=1;
    else if (!strcmp(cp, "loop"))
      bank->tone[i].strip_loop=1;
    else if (!strcmp(cp, "tail"))
      bank->tone[i].strip_tail=1;
    else
      {
       ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
         "%s: line %d: strip must be env, loop, or tail\n",
         name, line);
       return -2;
      }
      }
     else
      {
    ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: line %d: bad patch option %s\n",
      name, line, w[j]);
    return -2;
      }
    }
    }
   }
  if (ferror(fp))
   {
    ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Can't read from %s\n", name);
    close_file(fp);
    return -2;
   }
  close_file(fp);
  return 0;
}
Exemple #3
0
static int read_config_file(const char *name)
{
	std::ifstream  fp;
	char *w[MAXWORDS], *cp;
	ToneBank *bank=0;
	int i, j, k, line=0, words;
	static int rcf_count=0;

	if (rcf_count>50)
	{
        printf(
			"Probable source loop in configuration files");
		return (-1);
	}

    fp.open(resourcePath() + name,std::ios::binary);
	if (!fp.good())
		return -2;
	
	char tokTmp[1024];

	while ( fp.getline( tokTmp, 1024 ) )
	{
		
		line++;
		w[words=0]=strtok(tokTmp, " \t\r\n\240");
		if (!w[0] || (*w[0]=='#')) continue;
		while (w[words] && (words < MAXWORDS))
			w[++words]=strtok(0," \t\r\n\240");
		if (!strcmp(w[0], "dir"))
		{
			if (words < 2)
			{
                printf(
					"%s: line %d: No directory given\n", name, line);
				return -3;
			}
			for (i=1; i<words; i++)
				add_to_pathlist(w[i]);
		}
		else if (!strcmp(w[0], "source"))
		{
			if (words < 2)
			{
                printf(
					"%s: line %d: No file name given\n", name, line);
				return -4;
			}
			for (i=1; i<words; i++)
			{
				rcf_count++;
				read_config_file(w[i]);
				rcf_count--;
			}
		}
		else if (!strcmp(w[0], "default"))
		{
			if (words != 2)
			{
                printf(
					"%s: line %d: Must specify exactly one patch name\n",
					name, line);
				return -5;
			}
			def_instr_name = std::string(w[1], 255);
			def_instr_name[255]='\0';
		}
		else if (!strcmp(w[0], "drumset"))
		{
			if (words < 2)
			{
                printf(
					"%s: line %d: No drum set number given\n", 
					name, line);
				return -6;
			}
			i=atoi(w[1]);
			if (i<0 || i>127)
			{
                printf(
					"%s: line %d: Drum set must be between 0 and 127\n",
					name, line);
				return -7;
			}
			if (!drumset[i])
			{
				drumset[i]=(ToneBank*)safe_malloc(sizeof(ToneBank));
				memset(drumset[i], 0, sizeof(ToneBank));
			}
			bank=drumset[i];
		}
		else if (!strcmp(w[0], "bank"))
		{
			if (words < 2)
			{
                printf(
					"%s: line %d: No bank number given\n", 
					name, line);
				return -8;
			}
			i=atoi(w[1]);
			if (i<0 || i>127)
			{
                printf(
					"%s: line %d: Tone bank must be between 0 and 127\n",
					name, line);
				return -9;
			}
			if (!tonebank[i])
			{
				tonebank[i]=(ToneBank*)safe_malloc(sizeof(ToneBank));
				memset(tonebank[i], 0, sizeof(ToneBank));
			}
			bank=tonebank[i];
		}
		else {
			if ((words < 2) || (*w[0] < '0' || *w[0] > '9'))
			{
                printf(
					"%s: line %d: syntax error\n", name, line);
				return -10;
			}
			i=atoi(w[0]);
			if (i<0 || i>127)
			{
                printf(
					"%s: line %d: Program must be between 0 and 127\n",
					name, line);
				return -11;
			}
			if (!bank)
			{
                printf(
					"%s: line %d: Must specify tone bank or drum set "
					"before assignment\n",
					name, line);
				return -12;
			}
			if (bank->tone[i].name)
				Real_Tim_Free(bank->tone[i].name);
			strcpy((bank->tone[i].name=(char*)safe_malloc(strlen(w[1])+1)),w[1]);
			bank->tone[i].note=bank->tone[i].amp=bank->tone[i].pan=
				bank->tone[i].strip_loop=bank->tone[i].strip_envelope=
				bank->tone[i].strip_tail=-1;

			for (j=2; j<words; j++)
			{
				if (!(cp=strchr(w[j], '=')))
				{
                    printf("%s: line %d: bad patch option %s\n",
						name, line, w[j]);
					return -13;
				}
				*cp++=0;
				if (!strcmp(w[j], "amp"))
				{
					k=atoi(cp);
					if ((k<0 || k>MAX_AMPLIFICATION) || (*cp < '0' || *cp > '9'))
					{
                        printf(
							"%s: line %d: amplification must be between "
							"0 and %d\n", name, line, MAX_AMPLIFICATION);
						return -14;
					}
					bank->tone[i].amp=k;
				}
				else if (!strcmp(w[j], "note"))
				{
					k=atoi(cp);
					if ((k<0 || k>127) || (*cp < '0' || *cp > '9'))
					{
                        std::cout << name <<
                            ": line " << line << ": note must be between 0 and 127\n";
						return -15;
					}
					bank->tone[i].note=k;
				}
				else if (!strcmp(w[j], "pan"))
				{
					if (!strcmp(cp, "center"))
						k=64;
					else if (!strcmp(cp, "left"))
						k=0;
					else if (!strcmp(cp, "right"))
						k=127;
					else
						k=((atoi(cp)+100) * 100) / 157;
					if ((k<0 || k>127) ||
						(k==0 && *cp!='-' && (*cp < '0' || *cp > '9')))
					{
                        std::cout << name <<
                            ": line " << line << ": panning must be left, right, " <<
                            "center, or between -100 and 100\n";
						return -16;
					}
					bank->tone[i].pan=k;
				}
				else if (!strcmp(w[j], "keep"))
				{
					if (!strcmp(cp, "env"))
						bank->tone[i].strip_envelope=0;
					else if (!strcmp(cp, "loop"))
						bank->tone[i].strip_loop=0;
					else
					{
                        std::cout << name << 
                            ": line " << line << ": keep must be env or loop\n";
						return -17;
					}
				}
				else if (!strcmp(w[j], "strip"))
				{
					if (!strcmp(cp, "env"))
						bank->tone[i].strip_envelope=1;
					else if (!strcmp(cp, "loop"))
						bank->tone[i].strip_loop=1;
					else if (!strcmp(cp, "tail"))
						bank->tone[i].strip_tail=1;
					else
					{
                        std::cout << name << 
                            ": line " << line << ": strip must be env, loop, or tail\n";
						return -18;
					}
				}
				else
				{
                    std::cout << name << ": line " << line << ": bad patch option " << w[j] << "\n";
					return -19;
				}
			}
		}
	}
	if ( !fp.good() ) //(ferror(fp))
	{
		std::cout <<  "Can't read from " << name << "\n";
        fp.close();
		//return -20;
	}
    fp.close();
	return 0;
}