Exemplo n.º 1
0
void
append_digit (char *s)
{
    char c = last_char(s);
    if (c < '0' || c > '9')
        strcat(s, "0");
}
Exemplo n.º 2
0
parse_result parse_write_file (char const * str) {
	FILE * fp=fopen(str, "w");
	if (fp)
		return make_parse_result(make_ptr(fp), last_char(str));
	else
		return make_parse_result(NULL, str);
}
Exemplo n.º 3
0
Arquivo: ref-util.c Projeto: mpw/p2
static int
get_label_num (char *s) 
{
  char c = last_char(s);
  
  if (c >= '0' && c <= '9')
    return ((int) c - '0');
  return(0);
} 
Exemplo n.º 4
0
// Конструирует объект типа DICT_METHOD с заданными параметрами упаковки
// или возвращает NULL, если это другой метод сжатия или допущена ошибка в параметрах
COMPRESSION_METHOD* parse_DICT (char** parameters)
{
  if (strcmp (parameters[0], "dict") == 0) {
    // Если название метода (нулевой параметр) - "dict", то разберём остальные параметры

    DICT_METHOD *p = new DICT_METHOD;
    int error = 0;  // Признак того, что при разборе параметров произошла ошибка

    // Переберём все параметры метода (или выйдем раньше при возникновении ошибки при разборе очередного параметра)
    while (*++parameters && !error)
    {
      char* param = *parameters;
      if (strlen(param)==1) switch (*param) {    // Однобуквенные параметры
        case 'p':  p->MinLargeCnt=8192; p->MinMediumCnt=400; p->MinSmallCnt=100; p->MinRatio=4; continue;
        case 'f':  p->MinLargeCnt=2048; p->MinMediumCnt=100; p->MinSmallCnt= 50; p->MinRatio=0; continue;
      }
      else switch (*param) {                    // Параметры, содержащие значения
        case 'b':  p->BlockSize    = parseMem (param+1, &error); continue;
        case 'c':  p->MinWeakChars = parseInt (param+1, &error); continue;
        case 'l':  p->MinLargeCnt  = parseInt (param+1, &error); continue;
        case 'm':  p->MinMediumCnt = parseInt (param+1, &error); continue;
        case 's':  p->MinSmallCnt  = parseInt (param+1, &error); continue;
        case 'r':  p->MinRatio     = parseInt (param+1, &error); continue;
      }
      // Если параметр заканчивается знаком процента. то попробуем распарсить его как "N%"
      if (last_char(param) == '%') {
        char str[100]; strcpy(str,param); last_char(str) = '\0';
        int n = parseInt (str, &error);
        if (!error) { p->MinCompression = n; continue; }
        error=0;
      }
      // Сюда мы попадаем, если в параметре не указано его название
      // Если этот параметр удастся разобрать как целое число (т.е. в нём - только цифры),
      // то присвоим его значение полю MinMatchLen, иначе попробуем разобрать его как BlockSize
      int n = parseInt (param, &error);
      if (!error) p->MinWeakChars = n;
      else        error=0, p->BlockSize = parseMem (param, &error);
    }
    if (error)  {delete p; return NULL;}  // Ошибка при парсинге параметров метода
    return p;
  } else
    return NULL;   // Это не метод DICT
}
Exemplo n.º 5
0
// Конструирует объект типа REP_METHOD с заданными параметрами упаковки
// или возвращает NULL, если это другой метод сжатия или допущена ошибка в параметрах
COMPRESSION_METHOD* parse_REP (char** parameters)
{
  if (strcmp (parameters[0], "rep") == 0) {
    // Если название метода (нулевой параметр) - "rep", то разберём остальные параметры

    REP_METHOD *p = new REP_METHOD;
    int error = 0;  // Признак того, что при разборе параметров произошла ошибка

    // Переберём все параметры метода (или выйдем раньше при возникновении ошибки при разборе очередного параметра)
    while (*++parameters && !error)
    {
      char* param = *parameters;
      switch (*param) {                    // Параметры, содержащие значения
        case 'b':  p->BlockSize   = parseMem (param+1, &error); continue;
        case 'l':  p->MinMatchLen = parseInt (param+1, &error); continue;
        case 'd':  p->Barrier     = parseMem (param+1, &error); continue;
        case 's':  p->SmallestLen = parseInt (param+1, &error); continue;
        case 'h':  p->HashSizeLog = parseInt (param+1, &error); continue;
        case 'a':  p->Amplifier   = parseInt (param+1, &error); continue;
      }
      // Если параметр заканчивается знаком процента. то попробуем распарсить его как "N%"
      if (last_char(param) == '%') {
        char str[100]; strcpy(str,param); last_char(str) = '\0';
        int n = parseInt (str, &error);
        if (!error) { p->MinCompression = n; continue; }
        error=0;
      }
      // Сюда мы попадаем, если в параметре не указано его название
      // Если этот параметр удастся разобрать как целое число (т.е. в нём - только цифры),
      // то присвоим его значение полю MinMatchLen, иначе попробуем разобрать его как BlockSize
      int n = parseInt (param, &error);
      if (!error) p->MinMatchLen = n;
      else        error=0, p->BlockSize = parseMem (param, &error);
    }
    if (error)  {delete p; return NULL;}  // Ошибка при парсинге параметров метода
    return p;
  } else
    return NULL;   // Это не метод REP
}
Exemplo n.º 6
0
parse_result parse_bool(char const * str) {
	bool * r=NULL;
	if (!str)
		return make_parse_result(NULL, str);
	if (!strcmp(str, "true") ) r=make_bool(true);
	if (!strcmp(str, "false")) r=make_bool(false);

	// although it is doubtful ...
	if (!strcmp(str, "1")) r=make_bool(true);
	if (!strcmp(str, "0")) r=make_bool(false);

	return make_parse_result(r, last_char(str));
}
Exemplo n.º 7
0
void read_line(ParserData* data) {
	// Read one line
	if (fgets(data->line, data->buffer_size, data->file) == NULL) {
		data->eof = true;
		return;
	}
	data->linelength = strlen(data->line);

	// If the buffer was too small, extend it and keep reading.
	// Note: when the last character in the buffer is the CR of a CRLF-terminated line, 
	// the parser thinks the line is terminated and the next line parsed is an empty 
	// LF-terminated line. However, as empty lines are ignored, this is not a problem.
	while (data->linelength == data->buffer_size - 1 && !isnewline(last_char(data))) {
		char* old_buffer = data->line;
		size_t old_buffer_size = data->buffer_size;

		// Create new buffer
		data->buffer_size = old_buffer_size * 2;
		data->line = malloc(data->buffer_size * sizeof (char));

		// Fill it with the previously read part of the line
		strcpy(data->line, old_buffer);

		// Destroy old buffer
		free(old_buffer);

		// Continue reading
		fgets(&(data->line[old_buffer_size - 1]),
				data->buffer_size - (old_buffer_size - 1), data->file);
		data->linelength = strlen(data->line);
	}
	
	// Remove end-line character - iterative to catch CRLF and LFCR
	while (isnewline(last_char(data))) {
		data->line[data->linelength - 1] = '\0';
		data->linelength--;
	}
}
Exemplo n.º 8
0
char *add2path( char *path, char *add, int delim ) {
    static char toend[2];
    int c;
        
    assert( path != NULL );
    assert( add != NULL );
    c = last_char( path );
    if ( c && c != delim ) {
        toend[0] = delim;
        toend[1] = 0;
        strcat( path, toend );
    }
    return strcat( path, add );
}
Exemplo n.º 9
0
/*
//	add path & fname
*/
void make_path( char *fname, char *dir, char *buf ) {
    char dird[2];

    assert( fname != NULL );
    assert( dir != NULL );
    assert( buf != NULL );

    strcpy( buf, dir );
    if ( last_char(buf) != DIR_DELIM ) {
        dird[0] = DIR_DELIM;
        dird[1] = 0;
        strcat( buf, dird );
    }
    strcat( buf, fname );
}
Exemplo n.º 10
0
      string correct_slash_at_end( string const& path ) {
         string temp = path;

         if( temp.size() == 0 ) {
            return string();
         }

         // correct slash at end
         string s = fn_intern::slash();

         if( last_char( temp ) != char_slash() ) {
            temp +=  s;
         }

         return temp;
      }
Exemplo n.º 11
0
char * replace_char(char * str, char * delim)
{
    //on trouve la longueur du nouveau string
    int new_length = 0;
    int str_length = 0;
    int bg_check = 0;
    for(int i=0; str[i] != '\0'; i++)
    {
        if(str[i] == delim[0])
            new_length += 2; //on va ajouter deux " si on tombe sur le caractère
        new_length++;
        str_length++;
    }
    new_length++; //pour le caractère de fin '\0'
    //cas spécial pour delim[0] == '&', car à la fin de la commande
    if(delim[0] == '&' && last_char(str, '&') == 1)
    {
        new_length += 2;
        bg_check = 1;
    }
    char * new_str = malloc(sizeof(char)*new_length);
    //initialisation du nouveau string
    for(int i=0; i<new_length; i++)
        new_str[i] = '\0';
    char * token = strtok(str, delim);
    while(token)
    {
        strcat(new_str, token);
        //puts(token);
        token = strtok(NULL, delim);
        if(token != NULL)
        {
            strcat(new_str, "\"");
            strcat(new_str, delim);
            strcat(new_str, "\"");
        }
    }
    if(bg_check == 1)
    {
        strcat(new_str, "\"&\"");
    }
    free(str);
    return new_str;
}
Exemplo n.º 12
0
Arquivo: ref-util.c Projeto: mpw/p2
int
get_param_num (char *s)
{
#if 0
#if 0 
  char c = last_char(s);
 
  if (s[0] == 'c' && (s[1]=='u' || s[1]=='o')) {
    if (c >= '0' && c <= '9')
      return(((int) c - '0')/2);
    else
      return(0);
  }
#else
  if (c >= '0' && c <= '9')
    return ((int) c - '0');
  return(0);
#endif
#else
  /* Note: get_label_num and get_param_num are EXACTLY identical. (JAT) */
  return(get_label_num(s));
#endif
}
Exemplo n.º 13
0
static bool read_integer(int base, bool minus, int64_t *result, bool local)
{
	uint64_t nbr2, nbr;
	int d, n, c; //, nmax;
	char thsep;
	int ndigit_thsep;
	bool first_thsep;

	thsep = LOCAL_get(local)->thousand_sep;
	ndigit_thsep = 0;
	first_thsep = FALSE;

	n = 0;
	nbr = 0;
	
	/*switch (base)
	{
		case 2: nmax = 64; break;
		case 8: nmax = 21; break;
		case 10: nmax = 19; break;
		case 16: nmax = 16; break;
	}*/

	c = last_char();

	for(;;)
	{
		if (local && base == 10)
		{
			if (c == thsep && (ndigit_thsep == 3 || (!first_thsep && ndigit_thsep >= 1 && ndigit_thsep <= 3)))
			{
				c = get_char();
				first_thsep = TRUE;
				ndigit_thsep = 0;
			}
		}

		if (c >= '0' && c <= '9')
		{
			d = c - '0';
			if (local && base == 10)
				ndigit_thsep++;
		}
		else if (c >= 'A' && c <='Z')
			d = c - 'A' + 10;
		else if (c >= 'a' && c <='z')
			d = c - 'a' + 10;
		else
			break;

		if (d >= base)
			break;

		n++;
		
		nbr2 = nbr * base + d;
		
		if ((nbr2 / base) != nbr || nbr2 > ((uint64_t)LLONG_MAX + minus))
			return TRUE;
		
		nbr = nbr2;

		c = get_char();
		if (c < 0)
			break;
	}

	c = last_char();

	if (base == 10)
	{
		if (local && first_thsep && ndigit_thsep != 3)
			return TRUE;
	}
	else
	{
		if ((c == '&' || c == 'u' || c == 'U') && base != 10)
			c = get_char();
		else
		{
			if ((base == 16 && n == 4) || (base == 2 && n == 16))
			{
				if (nbr >= 0x8000L && nbr <= 0xFFFFL)
					nbr |= INT64_C(0xFFFFFFFFFFFF0000);
			}
			else if ((base == 16 && n == 8) || (base == 2 && n == 32))
			{
				if (nbr >= 0x80000000L && nbr <= 0xFFFFFFFFL)
					nbr |= INT64_C(0xFFFFFFFF00000000);
			}
		}
	}

	if (c > 0 && !isspace(c))
		return TRUE;
	
	if (n == 0)
		return TRUE;

	*((int64_t *)result) = nbr;  
	return FALSE;
}
Exemplo n.º 14
0
parse_result success_parser(char const * str) {
	return make_parse_result(make_ptr(string_dup(str)), last_char(str));
}
Exemplo n.º 15
0
void Template::process (const bool have_adjustments)
{
    const bool show_projection_details  = strequal (projection_details, "Y");
    const bool show_summary = 
	show_projection_details || strequal (projection_details, "S");

    int n_threads;
    if (strequal (n_cores, "A"))
        n_threads = get_n_cores ();
    else
        n_threads = atoi (n_cores);

    bool print_off = false;
    int line_no = 0;

    for (int iii = 1;  iii <= n_lines;  iii++)
    {
        char* line = lines[iii];

        if (strstr (line, "$year-declarations$"))
	{
	    for (int ii = 1;  ii <= projection_years;  ii++)
	    {
		print_indent ();
		fprintf (f_out, "static void yr_%d (const int, const int);\n", ii);
		++line_no;
	    }
	    continue;
	}

        if (strstr (line, "$n-trials$"))
	    replace_string_in_string (line, "$n-trials$", number_of_trials);

        if (strstr (line, "$n-years$"))
	    replace_string_in_string (line, "$n-years$", projection_years);

        if (strstr (line, "$n-threads$"))
	    replace_string_in_string (line, "$n-threads$", n_threads);

        if (strstr (line, "$show-projection-details$"))
	{
	    const char* spd = show_projection_details ? "true" : "false";
	    replace_string_in_string (line, "$show-projection-details$", spd);
	}

        if (strstr (line, "$show-summary$"))
	{
	    const char* sum = show_summary ? "true" : "false";
	    replace_string_in_string (line, "$show-summary$", sum);
	}

        if (strstr (line, "$have-seed$"))
	{
	    bool have_seed = !strequal (fixed_random_seed, "N");
	    const char* s_have_seed = have_seed ? "true" : "false";
	    replace_string_in_string (line, "$have-seed$", s_have_seed);
	}

        if (strstr (line, "$seed$"))
	{
	    bool have_seed = !strequal (fixed_random_seed, "N");

	    char s_seed[20];
	    sprintf (s_seed, "%s", have_seed ? fixed_random_seed : "-1");

	    replace_string_in_string (line, "$seed$", s_seed);
	}

        if (strstr (line, "$non-dpc$"))
	{
	    char* s_non_dpc = (char*)"false";

	    if (strequal (def_prob_code, "\"\"") 
	    || strequal (def_prob_code, ""))
	        s_non_dpc = (char*)"true";

	    replace_string_in_string (line, "$non-dpc$", s_non_dpc);
	}

        if (strstr (line, "$variable-definitions$"))
	{
	    for (int i = 1;  i <= n_vars;  i++)
	    {
	        if (starts_with (vars[i], "//")) continue;
		if (!compute[i]) continue;

		print_indent ();

		if (is_random (vars[i]))
		    fprintf (f_out, "static %s %s [n_threads][%d+1];\n", 
			"double", vars[i], projection_years);
		else
		    fprintf (f_out, "static %s %s [n_threads][%d+1];\n", 
			types[i], vars[i], projection_years);

		++line_no;
	    }
	    continue;
	}

        if (strstr (line, "$sigma-definitions$"))
	{
	    if (show_summary)
	    {
		for (int i = 1;  i <= n_vars;  i++)
		{
		    if (starts_with (vars[i], "//")) continue;
		    if (strciequal (include_in_output[i], "N")) continue;

		    print_indent ();

		    if (is_random (vars[i]))
			fprintf (f_out, "static %s sigma_%s[n_threads][%d+1];\n", 
			    "double", vars[i], projection_years);
		    else
			fprintf (f_out, "static %s sigma_%s[n_threads][%d+1];\n", 
			    types[i], vars[i], projection_years);

		    ++line_no;
		}
	    }

	    continue;
	}

        if (strstr (line, "$minima-definitions$"))
	{
	    if (show_summary)
	    {
		for (int i = 1;  i <= n_vars;  i++)
		{
		    if (starts_with (vars[i], "//")) continue;
		    if (strciequal (include_in_output[i], "N")) continue;
    
		    print_indent ();
    
		    if (is_random (vars[i]))
			fprintf (f_out, "static %s minimum_%s [n_threads][%d+1];\n", 
			    "double", vars[i], projection_years);
		    else
			fprintf (f_out, "static %s minimum_%s [n_threads][%d+1];\n", 
			    types[i], vars[i], projection_years);
    
		    ++line_no;
		}
	    }
	    continue;
	}

        if (strstr (line, "$maxima-definitions$"))
	{
	    if (show_summary)
	    {
		for (int i = 1;  i <= n_vars;  i++)
		{
		    if (starts_with (vars[i], "//")) continue;
		    if (strciequal (include_in_output[i], "N")) continue;
    
		    print_indent ();
    
		    if (is_random (vars[i]))
			fprintf (f_out, "static %s maximum_%s [n_threads][%d+1];\n", 
			    "double", vars[i], projection_years);
		    else
			fprintf (f_out, "static %s maximum_%s [n_threads][%d+1];\n", 
			    types[i], vars[i], projection_years);
    
		    ++line_no;
		}
	    }
	    continue;
	}

        if (strstr (line, "$sigma-inits$"))
	{
	    if (show_summary)
	    {
		for (int i = 1;  i <= n_vars;  i++)
		{
		    if (starts_with (vars[i], "//")) continue;
		    if (strciequal (include_in_output[i], "N")) continue;
    
		    print_indent ();
    
		    fprintf (f_out, 
 "    sigma_%s[th][y] = 0;\n", vars[i]);
    
		    ++line_no;
		}
	    }
	    continue;
	}

        if (strstr (line, "$summations$"))
	{
	    if (show_summary)
	    {
		for (int i = 1;  i <= n_vars;  i++)
		{
		    if (starts_with (vars[i], "//")) continue;
		    if (strciequal (include_in_output[i], "N")) continue;
    
		    ::print_indent (f_out, line);
    
		    if (is_random (vars[i])) 
			fprintf (f_out, "if (y>0) ");
    
		    fprintf (f_out, "sigma_%s[th][y] += %s[th][y];\n", 
			vars[i], vars[i]);
    
		    ++line_no;
		}
	    }
	    continue;
	}

        if (strstr (line, "$update-minima"))
	{
	    if (show_summary)
	    {
		for (int i = 1;  i <= n_vars;  i++)
		{
		    if (starts_with (vars[i], "//")) continue;
		    if (strciequal (include_in_output[i], "N")) continue;
    
		    ::print_indent (f_out, line);
    
		    if (is_random (vars[i])) 
			fprintf (f_out, "if (y>0) ");
    
		    fprintf (f_out, 
    "if (%s[th][y] < minimum_%s[th][y]) minimum_%s[th][y] = %s[th][y];\n", 
			    vars[i], vars[i], vars[i], vars[i]);
    
		    ++line_no;
		}
	    }
	    continue;
	}

        if (strstr (line, "$update-maxima"))
	{
	    if (show_summary)
	    {
		for (int i = 1;  i <= n_vars;  i++)
		{
		    if (starts_with (vars[i], "//")) continue;
		    if (strciequal (include_in_output[i], "N")) continue;
    
		    ::print_indent (f_out, line);
    
		    if (is_random (vars[i])) 
			fprintf (f_out, "if (y>0) ");
    
		    fprintf (f_out, 
	"if (%s[th][y] > maximum_%s[th][y]) maximum_%s[th][y] = %s[th][y];\n", 
				vars[i], vars[i], vars[i], vars[i]);
    
		    ++line_no;
		}
	    }
	    continue;
	}

        if (strstr (line, "$sigma-combinations"))
	{
	    if (show_summary)
	    {
		for (int i = 1;  i <= n_vars;  i++)
		{
		    if (starts_with (vars[i], "//")) continue;
		    if (strciequal (include_in_output[i], "N")) continue;
    
		    ::print_indent (f_out, line);
    
		    if (is_random (vars[i])) 
			fprintf (f_out, "if (y>0) ");
    
		    fprintf (f_out, "sigma_%s[0][y] += sigma_%s[th][y];\n",
			    vars[i], vars[i]);
    
		    ++line_no;
		}
	    }
	    continue;
	}


        if (strstr (line, "$min-combinations"))
	{
	    if (show_summary)
	    {
		for (int i = 1;  i <= n_vars;  i++)
		{
		    if (starts_with (vars[i], "//")) continue;
		    if (strciequal (include_in_output[i], "N")) continue;
    
		    ::print_indent (f_out, line);
    
		    if (is_random (vars[i])) 
			fprintf (f_out, "if (y>0) ");
    
		    fprintf (f_out, 
	"minimum_%s[0][y] = min (minimum_%s[0][y], minimum_%s[th][y]);\n",
			    vars[i], vars[i], vars[i]);
    
		    ++line_no;
		}
	    }
	    continue;
	}

        if (strstr (line, "$max-combinations"))
	{
	    if (show_summary)
	    {
		for (int i = 1;  i <= n_vars;  i++)
		{
		    if (starts_with (vars[i], "//")) continue;
		    if (strciequal (include_in_output[i], "N")) continue;
    
		    ::print_indent (f_out, line);
    
		    if (is_random (vars[i])) 
			fprintf (f_out, "if (y>0) ");
    
		    fprintf (f_out, 
	"maximum_%s[0][y] = max (maximum_%s[0][y], maximum_%s[th][y]);\n",
			    vars[i], vars[i], vars[i]);
    
		    ++line_no;
		}
	    }
	    continue;
	}

        if (strstr (line, "$means$"))
	{
	    if (show_summary)
	    {
		for (int i = 1;  i <= n_vars;  i++)
		{
		    if (starts_with (vars[i], "//")) continue;
    
		    char* sep = (i < n_vars) ? (char*)"\\t" : (char*)"\\n";
    
		    if (strciequal (include_in_output[i], "Y"))
		    {
			if (is_random (vars[i]))
			{
			    ::print_indent (f_out, line);
			    fprintf (f_out, "if (y > 0)\n");
    
			    ::print_indent (f_out, line);
			    fprintf (f_out, "    fprintf (fp, \"%s", formats[i]);
    
			    ::print_indent (f_out, line);
			    fprintf (f_out, "%s\"", sep);
			    fprintf (f_out, ", sigma_%s[0][y]/n_trials);\n", vars[i]);
    
			    ::print_indent (f_out, line);
			    fprintf (f_out, "else\n");
    
			    ::print_indent (f_out, line);
			    fprintf (f_out, "    fprintf (fp, \"");
			    fprintf (f_out, "%s\"", sep);
			    fprintf (f_out, ");\n");
    
			    line_no += 4;
			}
			else
			{
			    ::print_indent (f_out, line);
			    fprintf (f_out, "fprintf (fp, \"%s", formats[i]);
			    fprintf (f_out, "%s\"", sep);
			    fprintf (f_out, ", sigma_%s[0][y]/n_trials);\n", vars[i]);
    
			    ++line_no;
			}
		    }
		}
	    }
	    continue;
	}

        if (strstr (line, "$minima$"))
	{
	    if (show_summary)
	    {
		for (int i = 1;  i <= n_vars;  i++)
		{
		    if (starts_with (vars[i], "//")) continue;
    
		    if (strciequal (include_in_output[i], "Y"))
		    {
			char* sep = (i < n_vars) ? (char*)"\\t" : (char*)"\\n";
    
			if (is_random (vars[i]))
			{
			    ::print_indent (f_out, line);
			    fprintf (f_out, "if (y > 0)\n");
    
			    ::print_indent (f_out, line);
			    fprintf (f_out, "    fprintf (fp, \"%s", formats[i]);
    
			    ::print_indent (f_out, line);
			    fprintf (f_out, "%s\"", sep);
			    fprintf (f_out, ", minimum_%s[0][y]);\n", vars[i]);
    
			    ::print_indent (f_out, line);
			    fprintf (f_out, "else\n");
    
			    ::print_indent (f_out, line);
			    fprintf (f_out, "    fprintf (fp, \"");
			    fprintf (f_out, "%s\"", sep);
			    fprintf (f_out, ");\n");
    
			    line_no += 4;
			}
			else
			{
			    ::print_indent (f_out, line);
			    fprintf (f_out, "fprintf (fp, \"%s", formats[i]);
			    fprintf (f_out, "%s\"", sep);
			    fprintf (f_out, ", minimum_%s[0][y]);\n", vars[i]);
    
			    ++line_no;
			}
		    }
		}
	    }
	    continue;
	}

        if (strstr (line, "$maxima$"))
	{
	    if (show_summary)
	    {
		for (int i = 1;  i <= n_vars;  i++)
		{
		    if (starts_with (vars[i], "//")) continue;
    
		    if (strciequal (include_in_output[i], "Y"))
		    {
			char* sep = (i < n_vars) ? (char*)"\\t" : (char*)"\\n";
    
			if (is_random (vars[i]))
			{
			    ::print_indent (f_out, line);
			    fprintf (f_out, "if (y > 0)\n");
    
			    ::print_indent (f_out, line);
			    fprintf (f_out, "    fprintf (fp, \"%s", formats[i]);
    
			    ::print_indent (f_out, line);
			    fprintf (f_out, "%s\"", sep);
			    fprintf (f_out, ", maximum_%s[0][y]);\n", vars[i]);
    
			    ::print_indent (f_out, line);
			    fprintf (f_out, "else\n");
    
			    ::print_indent (f_out, line);
			    fprintf (f_out, "    fprintf (fp, \"");
			    fprintf (f_out, "%s\"", sep);
			    fprintf (f_out, ");\n");
    
			    line_no += 4;
			}
			else
			{
			    ::print_indent (f_out, line);
			    fprintf (f_out, "fprintf (fp, \"%s", formats[i]);
			    fprintf (f_out, "%s\"", sep);
			    fprintf (f_out, ", maximum_%s[0][y]);\n", vars[i]);
    
			    ++line_no;
			}
		    }
		}
	    }
	    continue;
	}

        if (strstr (line, "$period-0-values$"))
	{
	    for (int i = 1;  i <= n_vars;  i++)
	    {
		if (starts_with (vars[i], "//")) continue;
		if (is_random (vars[i])) continue;
		if (!compute[i]) continue;

		print_indent ();

		char* y0;
		strcpy (&y0, year_0[i]);
		trim (y0);

		replace_string_in_string (y0, "\"\"", "");

		if (!strchr (y0, '.')) strcat (&y0, ".0");

	        fprintf (f_out, "%s[th][0] = %s;\n", vars[i], y0);
		++line_no;

		free (y0);
	    }
	    continue;
	}

        if (strstr (line, "$minima-inits$"))
	{
	    if (show_summary)
	    {
		for (int i = 1;  i <= n_vars;  i++)
		{
		    if (starts_with (vars[i], "//")) continue;
		    if (strciequal (include_in_output[i], "N")) continue;
    
		    print_indent ();
		    fprintf (f_out, 
			"    minimum_%s[th][y] = numeric_limits<%s>::max();\n", 
			vars[i], types[i]);
    
		    ++line_no;
		}
	    }
	    continue;
	}

        if (strstr (line, "$maxima-inits$"))
	{
	    if (show_summary)
	    {
		for (int i = 1;  i <= n_vars;  i++)
		{
		    if (starts_with (vars[i], "//")) continue;
		    if (strciequal (include_in_output[i], "N")) continue;
	
		    print_indent ();
		    fprintf (f_out, 
			"    maximum_%s[th][y] = -numeric_limits<%s>::max();\n", 
			vars[i], types[i]);
	
		    ++line_no;
		}
	    }
	    continue;
	}

        if (strstr (line, "$do-year-inits$"))
	{
	    for (int ii = 1;  ii <= projection_years;  ii++)
	    {
		print_indent ();
		fprintf (f_out, "    do_years[%d] = yr_%d;\n", ii, ii);
		++line_no;
	    }
	    continue;
	}

        if (strstr (line, "$compute$"))
	{
	    for (int ii = 1;  ii <= projection_years;  ii++)
	    {
		print_indent ();
		fprintf (f_out, "do_years[%d](%d, th);\n", ii, ii);
		++line_no;
	    }
	    continue;
	}

        if (strstr (line, "$assignments$"))
	{
	    for (int ii = 1;  ii <= projection_years;  ii++)
	    {
		print_indent ();
		fprintf (f_out, "void yr_%d (const int y, const int th)\n", ii);
		++line_no;

		print_indent ();
		fprintf (f_out, "{\n");
		++line_no;

		for (int i = 1;  i <= n_vars;  i++)
		{
		    if (starts_with (vars[i], "//")) continue;
		    if (is_random (vars[i])) continue;
		    if (!compute[i]) continue;

		    char* val = threaded_val (years[ii][i]);

		    replace_string_in_string (val, "\"\"", "");

		    char* rep;
		    strcpy (&rep, "[");
		    numcat (&rep, ii);
		    strcat (&rep, "]");

		    replace_string_in_string (val, "[y]", rep);

		    strcpy (&rep, "[");
		    numcat (&rep, ii-1);
		    strcat (&rep, "]");

		    replace_string_in_string (val, "[y-1]", rep);

		    floatise (val);

		    print_indent ();
		    fprintf (f_out, "    %s[th][%d] = %s;\n", vars[i], ii, val);

		    ++line_no;

		    free (val);

		    fprintf (f_map, "s %d %d %d [%s]\n", 
			    line_no, i+1, ii+11, years[ii][i]);

		    if (mins_set[i])
		    {
			print_indent ();
		        fprintf (f_out, "    if (%s[th][%d] < %s) %s[th][%d] = %s;\n",
			    vars[i], ii, mins[i], vars[i], ii, mins[i]);
			++line_no;

			fprintf (f_map, "s %d %d %d [%s]\n", 
			    line_no, i+1, ii+11, years[ii][i]);
		    }

		    if (maxs_set[i])
		    {
			print_indent ();
		        fprintf (f_out, "    if (%s[th][%d] > %s) %s[th][%d] = %s;\n",
			    vars[i], ii, maxs[i], vars[i], ii, maxs[i]);
			++line_no;

			fprintf (f_map, "s %d %d %d [%s]\n", 
			    line_no, i+1, ii+11, years[ii][i]);
		    }
		}

		print_indent ();
		fprintf (f_out, "}\n");
		++line_no;
	    }
	    continue;
	}

        if (strstr (line, "$body-2-defs$"))
	{
	    fprintf (f_out, "char bufy[n_years+1][80];\n");
	    ++line_no;

	    for (int i = 1;  i <= n_vars;  i++)
	    {
		if (!is_random (vars[i])) continue;

		if (strciequal (include_in_output[i], "Y"))
		{
		    ::print_indent (f_out, line);
		    fprintf (f_out, "char buf_%s[n_years+1][80];\n", vars[i]);
		    ++line_no;
		}
	    }

	    continue;
	}

        if (strstr (line, "$output-gen$"))
	{
	    for (int i = 1;  i <= n_vars;  i++)
	    {
		if (is_random (vars[i])) continue;
		if (starts_with (vars[i], "//")) continue;

		if (strciequal (include_in_output[i], "Y"))
		{
		    ::print_indent (f_out, line);

		    fprintf (f_out, "\"%s\\t\"\n", 
			formats[i]);

		    ++line_no;
		}
	    }

	    ::print_indent (f_out, line);
	    fprintf (f_out, "\"\\t\"\n");

	    for (int i = 1;  i <= n_vars;  i++)
	    {
		if (is_random (vars[i])) continue;
		if (starts_with (vars[i], "//")) continue;

		if (strciequal (include_in_output[i], "Y"))
		{
		    ::print_indent (f_out, line);

		    fprintf (f_out, ",%s[th][y]\n", 
			vars[i]);

		    ++line_no;
		}
	    }
	    continue;
	}

        if (strstr (line, "$output-print$"))
	{
	    for (int i = 1;  i <= n_vars;  i++)
	    {
		if (!is_random (vars[i])) continue;
		if (starts_with (vars[i], "//")) continue;

		if (strciequal (include_in_output[i], "Y"))
		{
		    ::print_indent (f_out, line);

		    fprintf (f_out, 
	"fwrite(buf_%s[y], strlen(buf_%s[y]), 1, fp);\n",
			vars[i], vars[i]);

		    ++line_no;
		}
	    }

	    continue;
	}


        if (strstr (line, "$headers$"))
	{
	    char* header;
	    strcpy (&header, "");

	    for (int i = 1;  i <= n_vars;  i++)
	    {
		const bool include = 
		    (strciequal (include_in_output[i], "Y")
		    && !starts_with (vars[i], "//"));

		if (include) strcat (&header, descriptions[i], "\\t");
	    }

	    replace_string_in_string (line, "$headers$", header);
	    free (header);
	}

        if (strstr (line, "$threshold-expression-1["))
	{
	    char* where_var = strstr (line, "$threshold-expression-1[");

	    char* var;
	    strcpy (&var, where_var);
	    *(strchr (var, ']')+2) = 0;

	    char* idx;
	    strcpy (&idx, where_var+23);

	    char ind[20];
	    sscanf (idx+1, "%[^]]", ind);

	    char* expression;
	    strcpy (&expression, threaded_val(metrics[0].expression));

	    char rep[20];
	    sprintf (rep, "[%s]", ind);

	    replace_string_in_string (expression, "[y]", rep);

	    replace_string_in_string (line, var, expression);

	    fprintf (f_map, "m %d %d %d [%s]\n", 
		line_no, metrics[0].row-1, 3, metrics[0].expression);
    }

    if (strstr (line, "$threshold-expression-2["))
    {
	char* where_var = strstr (line, "$threshold-expression-2[");

	char* var;
	strcpy (&var, where_var);
	*(strchr (var, ']')+2) = 0;

	char* idx;
	    strcpy (&idx, where_var+23);

	    char ind[20];
	    sscanf (idx+1, "%[^]]", ind);

	    char* expression;
	    strcpy (&expression, threaded_val(metrics[1].expression));

	    char rep[20];
	    sprintf (rep, "[%s]", ind);

	    replace_string_in_string (expression, "[y]", rep);

	    replace_string_in_string (line, var, expression);

	    fprintf (f_map, "m %d %d %d [%s]\n", 
		line_no, metrics[1].row-1, 3, metrics[1].expression);
	}

        if (strstr (line, "$threshold-expression-3["))
	{
	    char* where_var = strstr (line, "$threshold-expression-3[");

	    char* var;
	    strcpy (&var, where_var);
	    *(strchr (var, ']')+2) = 0;

	    char* idx;
	    strcpy (&idx, where_var+23);

	    char ind[20];
	    sscanf (idx+1, "%[^]]", ind);

	    char* expression;
	    strcpy (&expression, threaded_val(metrics[2].expression));

	    char rep[20];
	    sprintf (rep, "[%s]", ind);

	    replace_string_in_string (expression, "[y]", rep);

	    replace_string_in_string (line, var, expression);

	    fprintf (f_map, "m %d %d %d [%s]\n", 
		line_no, metrics[2].row-1, 3, metrics[2].expression);
	}

        if (strstr (line, "$threshold-expression-4["))
	{
	    char* where_var = strstr (line, "$threshold-expression-4[");

	    char* var;
	    strcpy (&var, where_var);
	    *(strchr (var, ']')+2) = 0;

	    char* idx;
	    strcpy (&idx, where_var+23);

	    char ind[20];
	    sscanf (idx+1, "%[^]]", ind);

	    char* expression;
	    strcpy (&expression, threaded_val(metrics[3].expression));

	    bool bad = strequal (ind, "0") && strstr (expression, "y-");

	    if (bad)
	        expression = (char*)"0";
	    else
	    {
	    char rep[20];
	    sprintf (rep, "[%s]", ind);

	    replace_string_in_string (expression, "[y]", rep);
	    }

	    replace_string_in_string (line, var, expression);

	    fprintf (f_map, "m %d %d %d [%s]\n", 
		line_no, metrics[3].row-1, 3, metrics[3].expression);
	}

        if (strstr (line, "$threshold-expression-5["))
	{
	    char* where_var = strstr (line, "$threshold-expression-5[");

	    char* var;
	    strcpy (&var, where_var);
	    *(strchr (var, ']')+2) = 0;

	    char* idx;
	    strcpy (&idx, where_var+23);

	    char ind[20];
	    sscanf (idx+1, "%[^]]", ind);

	    char* expression;
	    strcpy (&expression, threaded_val(metrics[4].expression));

	    bool bad = strequal (ind, "0") && strstr (expression, "y-");

	    if (bad)
	        expression = (char*)"0";
	    else
	    {
	    char rep[20];
	    sprintf (rep, "[%s]", ind);

	    replace_string_in_string (expression, "[y]", rep);
	    }

	    replace_string_in_string (line, var, expression);

	    fprintf (f_map, "m %d %d %d [%s]\n", 
		line_no, metrics[4].row-1, 3, metrics[4].expression);
	}

        if (strstr (line, "$thresh-relation$"))
	{
	    replace_string_in_string (line, "$thresh-relation$", 
		metrics[0].comparison);
	}

        if (strstr (line, "$thresholdlevel$"))
	{
	    bool percent_mode = (last_char(metrics[0].threshold_level) == '%');

	    if (percent_mode) last_char(metrics[0].threshold_level) = 0;

	    double val = atof (metrics[0].threshold_level);
	    if (percent_mode) val /= 100;

	    char thresholdlevel[80];
	    sprintf (thresholdlevel, "%.4f", val);

	    replace_string_in_string (line, "$thresholdlevel$", 
		thresholdlevel);
	}

        if (strstr (line, "$initial-year$"))
	    replace_string_in_string (line, "$initial-year$", initial_year);

        if (strstr (line, "$threshold-label$"))
	    replace_string_in_string (line, "$threshold-label$", 
	    	threshold_label);

        if (strstr (line, "$government-entity$"))
	    replace_string_in_string (line, "$government-entity$", 
		government_entity);
        if (strstr (line, "$model-description$"))
	    replace_string_in_string (line, "$model-description$", 
		model_description);
        if (strstr (line, "$currency-units-in$"))
	    replace_string_in_string (line, "$currency-units-in$", 
		currency_units_in);

        if (strstr (line, "$metrics-1-description$"))
	    replace_string_in_string (line, "$metrics-1-description$",
	        metrics[0].description);
        if (strstr (line, "$metrics-2-description$"))
	    replace_string_in_string (line, "$metrics-2-description$",
	        metrics[1].description);
        if (strstr (line, "$metrics-3-description$"))
	    replace_string_in_string (line, "$metrics-3-description$",
	        metrics[2].description);
        if (strstr (line, "$metrics-4-description$"))
	    replace_string_in_string (line, "$metrics-4-description$",
	        metrics[3].description);
        if (strstr (line, "$metrics-5-description$"))
	    replace_string_in_string (line, "$metrics-5-description$",
		metrics[4].description);

	if (strstr (line, "$print-ratios("))
	{
	    char* where_var = strstr (line, "$print-ratios(");

	    char* var;
	    strcpy (&var, where_var);
	    *(strchr (var, ')')+2) = 0;

	    int ind;
	    sscanf (where_var+14, "%d", &ind);

	    char* print_condition =
		strstr (metrics[ind-1].expression, "y-1") ? (char*)"false" : (char*)"true";

	    replace_string_in_string (line, var, print_condition);
	}

	if (strstr (line, "$if-ratio2-valid$"))
	    replace_string_in_string (line, "$if-ratio2-valid$",
		n_metrics >= 2 ? "" : "//" );

	if (strstr (line, "$if-ratio3-valid$"))
	    replace_string_in_string (line, "$if-ratio3-valid$",
		n_metrics >= 3 ? "" : "//" );
	if (strstr (line, "$if-ratio4-valid$"))
	    replace_string_in_string (line, "$if-ratio4-valid$",
		n_metrics >= 4 ? "" : "//" );
	if (strstr (line, "$if-ratio5-valid$"))
	    replace_string_in_string (line, "$if-ratio5-valid$",
		n_metrics >= 5 ? "" : "//" );

	if (strstr (line, "$random-headers$"))
	{
	    for (int i = 1;  i <= n_vars;  i++)
	    {
		if (!is_random (vars[i])) continue;

		print_indent ();

		char* main_title;
		char* suffix;
		get_title (vars[i], main_title, suffix);

		fprintf (f_out, "fprintf (fp, \"%s %s\\t\");\n", main_title, suffix);
		++line_no;
	    }

	    continue;
	}

	if (strstr (line, "$unirandom-definitions$"))
	{
	    for (int i = 1;  i <= n_vars;  i++)
	    {
		if (!is_random (vars[i])) continue;

		if (starts_with (vars[i], (char*)"unirandom"))
		{
		    print_indent ();
		    fprintf (f_out, "        boost::uniform_real < > gen_%s (%s, %s);\n", vars[i], mins[i], maxs[i]);

		    print_indent ();
		    fprintf (f_out, "        boost::variate_generator < boost::lagged_fibonacci1279&, boost::uniform_real < > > rand_%s (rng, gen_%s);\n", vars[i], vars[i]);
		    fprintf (f_out, "\n");
		    line_no += 3;
		}
	    }

	    continue;
	}

	if (strstr (line, "$normrandom-definitions$"))
	{
	    for (int i = 1;  i <= n_vars;  i++)
	    {
		if (!is_random (vars[i])) continue;

		if (starts_with (vars[i], (char*)"normrandom"))
		{
		    print_indent ();
		    fprintf (f_out, "        boost::normal_distribution < > gen_%s (%s, %s);\n", vars[i], means[i], std_sigmas[i]);

		    print_indent ();
		    fprintf (f_out, "        boost::variate_generator < boost::lagged_fibonacci1279&, boost::normal_distribution < > > rand_%s (rng, gen_%s);\n", vars[i], vars[i]);
		    fprintf (f_out, "\n");
		    line_no += 3;
		}
	    }

	    continue;
	}

	if (strstr (line, "$cauchyrandom-definitions$"))
	{
	    for (int i = 1;  i <= n_vars;  i++)
	    {
		if (!is_random (vars[i])) continue;

		if (starts_with (vars[i], (char*)"cauchyrandom"))
		{
		    print_indent ();
		    fprintf (f_out, "        boost::cauchy_distribution < > gen_%s (%s, %s);\n", vars[i], means[i], std_sigmas[i]);

		    print_indent ();
		    fprintf (f_out, "        boost::variate_generator < boost::lagged_fibonacci1279&, boost::cauchy_distribution < > > rand_%s (rng, gen_%s);\n", vars[i], vars[i]);
		    fprintf (f_out, "\n");
		    line_no += 3;
		}
	    }

	    continue;
	}

	if (strstr (line, "$unirandom-assignments$"))
	{
	    for (int i = 1;  i <= n_vars;  i++)
	    {
		if (!is_random (vars[i])) continue;

		if (starts_with (vars[i], (char*)"unirandom"))
		{
		    ::print_indent (f_out, line);
		    fprintf (f_out, "    %s[th][y] = rand_%s();\n", vars[i], vars[i]);
		    ++line_no;
		}

	    }

	    continue;
	}

	if (strstr (line, "$normrandom-assignments$"))
	{
	    for (int i = 1;  i <= n_vars;  i++)
		if (starts_with (vars[i], (char*)"normrandom"))
		{
		    ::print_indent (f_out, line);
		    fprintf (f_out, "for (;;)\n");
		    ::print_indent (f_out, line);
		    fprintf (f_out, "{\n");
		    ::print_indent (f_out, line);
		    fprintf (f_out, "    %s[th][y] = rand_%s();\n", 
			vars[i], vars[i]);
		    line_no += 3;

		    if (mins_set[i]) 
		    {
			::print_indent (f_out, line);
			fprintf (f_out, "    if (%s[th][y] < %s) continue;\n", 
			    vars[i], mins[i]);
			++line_no;
		    }
		    if (maxs_set[i])
		    {
			::print_indent (f_out, line);
			fprintf (f_out, "    if (%s[th][y] > %s) continue;\n", 
			    vars[i], maxs[i]);
			++line_no;
		    }

		    ::print_indent (f_out, line);
		    fprintf (f_out, "    break;\n");
		    ::print_indent (f_out, line);
		    fprintf (f_out, "}\n");
		    line_no += 2;
		}

	    continue;
	}

	if (strstr (line, "$cauchyrandom-assignments$"))
	{
	    for (int i = 1;  i <= n_vars;  i++)
		if (starts_with (vars[i], (char*)"cauchyrandom"))
		{
		    ::print_indent (f_out, line);
		    fprintf (f_out, "for (;;)\n");
		    ::print_indent (f_out, line);
		    fprintf (f_out, "{\n");
		    ::print_indent (f_out, line);
		    fprintf (f_out, "    %s[th][y] = rand_%s();\n", 
			vars[i], vars[i]);
		    line_no += 3;

		    if (mins_set[i]) 
		    {
			::print_indent (f_out, line);
			fprintf (f_out, "    if (%s[th][y] < %s) continue;\n", 
			    vars[i], mins[i]);
			++line_no;
		    }
		    if (maxs_set[i])
		    {
			::print_indent (f_out, line);
			fprintf (f_out, "    if (%s[th][y] > %s) continue;\n", 
			    vars[i], maxs[i]);
			++line_no;
		    }

		    ::print_indent (f_out, line);
		    fprintf (f_out, "    break;\n");
		    ::print_indent (f_out, line);
		    fprintf (f_out, "}\n");
		    line_no += 2;
		}

	    continue;
	}

	if (strstr (line, "$store-y0-random-values$"))
	{
	    for (int i = 1;  i <= n_vars;  i++)
	    {
		if (!is_random (vars[i])) continue;

		if (strciequal (include_in_output[i], "Y"))
		{
		    ::print_indent (f_out, line);
		    fprintf (f_out, "sprintf(buf_%s[y],\"\\t\");\n", vars[i]);

		    ++line_no;
		}
	    }

	    continue;
	}


	if (strstr (line, "$store-random-values$"))
	{
	    for (int i = 1;  i <= n_vars;  i++)
	    {
		if (!is_random (vars[i])) continue;

		if (strciequal (include_in_output[i], "Y"))
		{
		    ::print_indent (f_out, line);
		    fprintf (f_out, "sprintf(buf_%s[y],\"%%5.4f\\t\",%s[th][y]);\n", 
		    vars[i], vars[i]);
		    ++line_no;
		}
	    }

	    continue;
	}

	if (strstr (line, "$adjustments$"))
	{
	    if (have_adjustments)
		do_adjustments (line_no);

	    continue;
	}

        if (strstr (line, "$n-ratings$"))
	    replace_string_in_string (line, "$n-ratings$", n_ratings);

        if (strstr (line, "$ratings$"))
	{
	    char* txt_ratings;
	    strcpy (&txt_ratings, "");

	    for (int i = 2;  i <= n_ratings+1;  i++)
	    {
		if (i > 2) chrcat (&txt_ratings, ',');
	        strcat (&txt_ratings, "\"", ratings[i], "\"");
	    }

	    replace_string_in_string (line, "$ratings$", txt_ratings);
	    free (txt_ratings);
	}

        if (strstr (line, "$rating-grid$"))
	{
	    char* grid;
	    strcpy (&grid, "");

	    for (int i = 1;  i <= projection_years;  i++)
	    {
	        chrcat (&grid, '{');

		for (int j = 2;  j <= n_ratings;  j++)
		{
		    if (j > 2) chrcat (&grid, ',');
		    strcat (&grid, ratings_grid[i][j]);
		}

	        chrcat (&grid, '}');

		if (i < projection_years) 
		    strcat (&grid, ",\n");
	    }

	    replace_string_in_string (line, "$rating-grid$", grid);
	    free (grid);
	}

        if (strstr (line, "$default-probability-code$"))
	{
	    char* code_to_use = def_prob_code;

	    if (strequal (code_to_use, "\"\"")) code_to_use = (char*)"";

	    replace_string_in_string (line, "$default-probability-code$", 
		threaded_val(code_to_use));
	}

	if (!print_off) 
	{
	    fprintf (f_out, "%s\n", line);
	    ++line_no;
	}

	record_indent (line);
    }
}
Exemplo n.º 16
0
static bool read_float(double *result, bool local)
{
	LOCAL_INFO *local_info;
	char point;
	char thsep;
	int ndigit_thsep;
	bool first_thsep;
	int c, n;

	uint64_t mantisse, mantisse_int;
	int ndigit_frac;
	bool frac;
	bool frac_null;
	bool nozero;

	int nexp;
	bool nexp_minus;

	local_info = LOCAL_get(local);
	point = local_info->decimal_point;
	thsep = local_info->thousand_sep;
	ndigit_thsep = 0;
	first_thsep = FALSE;

	c = last_char();
	
	/* Integer and decimal part */

	n = 0;
	mantisse = 0;
	mantisse_int = 0;
	frac = FALSE;
	frac_null = TRUE;
	ndigit_frac = 0;
	nexp = 0;
	nexp_minus = FALSE;
	nozero = FALSE;
	
	for(;;)
	{
		if (c == point)
		{
			if (frac)
				break;
			c = get_char();
			frac = TRUE;
			mantisse_int = mantisse;
		}

		if (local && !frac)
		{
			if (c == thsep && (ndigit_thsep == 3 || (!first_thsep && ndigit_thsep >= 1 && ndigit_thsep <= 3)))
			{
				c = get_char();
				first_thsep = TRUE;
				ndigit_thsep = 0;
			}
		}
		
		if (!isdigit(c) || (c < 0))
			break;
		
		if (c != '0')
			nozero = TRUE;
		
		if (nozero)
			n++;
		
		if (n > MAX_FLOAT_DIGIT)
		{
			if (n == (MAX_FLOAT_DIGIT + 1) && (c >= '5'))
				mantisse++;
			if (!frac)
				ndigit_frac--;
			c = get_char();
			continue;
		}

		if (c == '0')
			mantisse *= 10;
		else
		{
			if (frac)
				frac_null = FALSE;
			mantisse = mantisse * 10 + (c - '0');
		}
		
		if (frac)
			ndigit_frac++;
		else if (local)
			ndigit_thsep++;

		c = get_char();

		if (c == 'e' || c == 'E')
			break;

		if (c < 0)
			goto __END;
	}

	/* Exponent */

	if (c == 'e' || c == 'E')
	{
		c = get_char();

		if (c == '+' || c == '-')
		{
			if (c == '-')
				nexp_minus = TRUE;

			c = get_char();
		}

		if (!isdigit(c) || (c < 0))
			return TRUE;

		for(;;)
		{
			nexp = nexp * 10 + (c - '0');
			if (nexp > DBL_MAX_10_EXP)
				return TRUE;

			c = get_char();
			if (!isdigit(c) || (c < 0))
				break;
		}
		
		if (nexp_minus)
			nexp = (-nexp);
	}

	if (c >= 0 && !isspace(c))
		return TRUE;

__END:
	
	if (local && first_thsep && ndigit_thsep != 3)
		return TRUE;

	if (frac && frac_null)
		mantisse = mantisse_int;
	else
		nexp -= ndigit_frac;

	//fprintf(stderr, "%.24g %d\n", (double)mantisse, nexp);
	//*result = mulpow10((double)mantisse, nexp);
	*result = (double)mantisse * pow10(nexp);
	
	return FALSE;
}
Exemplo n.º 17
0
bool NUMBER_from_string(int option, const char *str, int len, VALUE *value)
{
	int c;
	int64_t val = 0;
	double dval = 0.0;
	TYPE type;
	int base = 10;
	bool minus = FALSE;
	int pos;

	buffer_init(str, len);
	
	jump_space();

	c = get_char();

	if (c == '+' || c == '-')
	{
		minus = (c == '-');
		c = get_char();
	}

	if (option & NB_READ_INT_LONG)
	{
		if (option & NB_READ_HEX_BIN)
		{
			if (c == '&')
			{
				c = get_char();

				if (c == 'H' || c == 'h')
				{
					base = 16;
					c = get_char();
				}
				else if (c == 'O' || c == 'o')
				{
					base = 8;
					c = get_char();
				}
				else if (c == 'X' || c == 'x')
				{
					base = 2;
					c = get_char();
				}
				else
					base = 16;
			}
			else if (c == '%')
			{
				base = 2;
				c = get_char();
			}
		}
	}

	if (c < 0)
		return TRUE;

	if (c == '-' || c == '+')
		return TRUE;

	errno = 0;
	pos = COMMON_pos - 1;

	if (!read_integer(base, minus, &val, (option & NB_LOCAL) != 0))
	{
		if (minus) val = (-val);
		
		if ((option & NB_READ_INTEGER) && IS_PURE_INTEGER(val))
		{
			type = T_INTEGER;
			goto __END;
		}
		else if ((option & NB_READ_LONG))
		{
			type = T_LONG;
			goto __END;
		}
		else if ((option & NB_READ_FLOAT) && base == 10)
		{
			type = T_FLOAT;
			dval = (double)val;
			goto __END;
		}
	}

	if ((option & NB_READ_FLOAT) && base == 10)
	{
		COMMON_pos = pos;
		get_char();
		if (!read_float(&dval, (option & NB_LOCAL) != 0))
		{
			if (minus) dval = (-dval);
			type = T_FLOAT;
			goto __END;
		}
	}

	return TRUE;

__END:

	if (last_char() >= 0) //(c >= 0 && !isspace(c))
		return TRUE;

	value->type = type;

	if (type == T_INTEGER)
		value->_integer.value = val;
	else if (type == T_LONG)
		value->_long.value = val;
	else
		value->_float.value = dval;

	//fprintf(stderr, "return FALSE\n");
	return FALSE;
}
Exemplo n.º 18
0
/*
void Template::process ()
*/
void Template::process (const bool have_adjustments)
// end
{
    bool print_off = false;
    int line_no = 0;

    for (int iii = 1;  iii <= n_lines;  iii++)
    {
        char* line = lines[iii];

        if (strstr (line, "$number-of-trials$"))
	    replace_string_in_string (line, "$number-of-trials$", 
		number_of_trials);

        if (strstr (line, "$n-years$"))
	    replace_string_in_string (line, "$n-years$", projection_years);

        if (strstr (line, "$declarations$"))
	{
	    for (int i = 1;  i <= n_vars;  i++)
	    {
	        if (starts_with (vars[i], "//")) continue;

		print_indent ();

		if (is_random (vars[i]))
		    fprintf (f_out, "%s %s [%d+1];\n", 
			"double", vars[i], projection_years);
		else
		    fprintf (f_out, "%s %s [%d+1];\n", 
			types[i], vars[i], projection_years);

		++line_no;
	    }
	    continue;
	}

        if (strstr (line, "$period-0-values$"))
	{
	    for (int i = 1;  i <= n_vars;  i++)
	    {
		if (starts_with (vars[i], "//")) continue;
		if (is_random (vars[i])) continue;

		print_indent ();

		char* y0;
		strcpy (&y0, year_0[i]);
		trim (y0);

		if (!strchr (y0, '.')) strcat (&y0, ".0");

	        fprintf (f_out, "%s[0] = %s;\n", vars[i], y0);
		++line_no;

		free (y0);
	    }
	    continue;
	}

        if (strstr (line, "$assignments$"))
	{
	    for (int ii = 1;  ii <= projection_years;  ii++)
	    {
		print_indent ();
		if (ii > 1) fprintf (f_out, "else ");
		fprintf (f_out, "if (y == %d)\n", ii);
		++line_no;

		print_indent ();
		fprintf (f_out, "{\n");
		++line_no;

		for (int i = 1;  i <= n_vars;  i++)
		{
		    if (starts_with (vars[i], "//")) continue;
		    if (is_random (vars[i])) continue;

		    char* val;
		    strcpy (&val, years[ii][i]);

		    floatise (val);

		    print_indent ();
		    fprintf (f_out, "    %s[y] = %s;\n", vars[i], val);
		    ++line_no;

		    free (val);

		    fprintf (f_map, "s %d %d %d [%s]\n", 
			    line_no, i+1, ii+11, years[ii][i]);

		    if (mins_set[i])
		    {
			print_indent ();
		        fprintf (f_out, "    if (%s[y] < %s) %s[y] = %s;\n",
			    vars[i], mins[i], vars[i], mins[i]);
			++line_no;

			fprintf (f_map, "s %d %d %d [%s]\n", 
			    line_no, i+1, ii+11, years[ii][i]);
		    }

		    if (maxs_set[i])
		    {
			print_indent ();
		        fprintf (f_out, "    if (%s[y] > %s) %s[y] = %s;\n",
			    vars[i], maxs[i], vars[i], maxs[i]);
			++line_no;

			fprintf (f_map, "s %d %d %d [%s]\n", 
			    line_no, i+1, ii+11, years[ii][i]);
		    }
		}

		print_indent ();
		fprintf (f_out, "}\n");
		++line_no;
	    }
	    continue;
	}

        if (strstr (line, "$output$"))
	{
	    for (int i = 1;  i <= n_vars;  i++)
	    {
		if (is_random (vars[i])) continue;
		if (starts_with (vars[i], "//")) continue;

		if (strciequal (include_in_output[i], "Y"))
		{
		    print_indent ();

		    fprintf (f_out, "fprintf(fp,\"%s\\t\",%s[y]);\n", 
			formats[i], vars[i]);

		    ++line_no;
		}
	    }
	    continue;
	}

        if (strstr (line, "$headers$"))
	{
	    char* header;
	    strcpy (&header, "");

	    for (int i = 1;  i <= n_vars;  i++)
	    {
		const bool include = 
		    (strciequal (include_in_output[i], "Y")
		    && !starts_with (vars[i], "//"))
		    || is_random (vars[i]); 

		if (include) strcat (&header, descriptions[i], "\\t");
	    }

	    replace_string_in_string (line, "$headers$", header);
	    free (header);
	}

        if (strstr (line, "$threshold-expression-1["))
	{
	    char* where_var = strstr (line, "$threshold-expression-1[");

	    char* var;
	    strcpy (&var, where_var);
	    *(strchr (var, ']')+2) = 0;

	    char* idx;
	    strcpy (&idx, where_var+23);

	    char ind[20];
	    sscanf (idx+1, "%[^]]", ind);

	    char* expression;
	    strcpy (&expression, metrics[0].expression);

	    char rep[20];
	    sprintf (rep, "[%s]", ind);

	    replace_string_in_string (expression, "[y]", rep);

	    replace_string_in_string (line, var, expression);

	    fprintf (f_map, "m %d %d %d [%s]\n", 
		line_no, metrics[0].row-1, 3, metrics[0].expression);
    }

    if (strstr (line, "$threshold-expression-2["))
    {
	char* where_var = strstr (line, "$threshold-expression-2[");

	char* var;
	strcpy (&var, where_var);
	*(strchr (var, ']')+2) = 0;

	char* idx;
	    strcpy (&idx, where_var+23);

	    char ind[20];
	    sscanf (idx+1, "%[^]]", ind);

	    char* expression;
	    strcpy (&expression, metrics[1].expression);

	    char rep[20];
	    sprintf (rep, "[%s]", ind);

	    replace_string_in_string (expression, "[y]", rep);

	    replace_string_in_string (line, var, expression);

	    fprintf (f_map, "m %d %d %d [%s]\n", 
		line_no, metrics[1].row-1, 3, metrics[1].expression);
	}

        if (strstr (line, "$threshold-expression-3["))
	{
	    char* where_var = strstr (line, "$threshold-expression-3[");

	    char* var;
	    strcpy (&var, where_var);
	    *(strchr (var, ']')+2) = 0;

	    char* idx;
	    strcpy (&idx, where_var+23);

	    char ind[20];
	    sscanf (idx+1, "%[^]]", ind);

	    char* expression;
	    strcpy (&expression, metrics[2].expression);

	    char rep[20];
	    sprintf (rep, "[%s]", ind);

	    replace_string_in_string (expression, "[y]", rep);

	    replace_string_in_string (line, var, expression);

	    fprintf (f_map, "m %d %d %d [%s]\n", 
		line_no, metrics[2].row-1, 3, metrics[2].expression);
	}

        if (strstr (line, "$threshold-expression-4["))
	{
	    char* where_var = strstr (line, "$threshold-expression-4[");

	    char* var;
	    strcpy (&var, where_var);
	    *(strchr (var, ']')+2) = 0;

	    char* idx;
	    strcpy (&idx, where_var+23);

	    char ind[20];
	    sscanf (idx+1, "%[^]]", ind);

	    char* expression;
	    strcpy (&expression, metrics[3].expression);

	    bool bad = strequal (ind, "0") && strstr (expression, "y-");

	    if (bad)
	        expression = (char*)"0";
	    else
	    {
	    char rep[20];
	    sprintf (rep, "[%s]", ind);

	    replace_string_in_string (expression, "[y]", rep);
	    }

	    replace_string_in_string (line, var, expression);

	    fprintf (f_map, "m %d %d %d [%s]\n", 
		line_no, metrics[3].row-1, 3, metrics[3].expression);
	}

        if (strstr (line, "$threshold-expression-5["))
	{
	    char* where_var = strstr (line, "$threshold-expression-5[");

	    char* var;
	    strcpy (&var, where_var);
	    *(strchr (var, ']')+2) = 0;

	    char* idx;
	    strcpy (&idx, where_var+23);

	    char ind[20];
	    sscanf (idx+1, "%[^]]", ind);

	    char* expression;
	    strcpy (&expression, metrics[4].expression);

	    bool bad = strequal (ind, "0") && strstr (expression, "y-");

	    if (bad)
	        expression = (char*)"0";
	    else
	    {
	    char rep[20];
	    sprintf (rep, "[%s]", ind);

	    replace_string_in_string (expression, "[y]", rep);
	    }

	    replace_string_in_string (line, var, expression);

	    fprintf (f_map, "m %d %d %d [%s]\n", 
		line_no, metrics[4].row-1, 3, metrics[4].expression);
	}

        if (strstr (line, "$thresh-relation$"))
	{
	    replace_string_in_string (line, "$thresh-relation$", 
		metrics[0].comparison);
	}

        if (strstr (line, "$thresholdlevel$"))
	{
	    bool percent_mode = (last_char(metrics[0].threshold_level) == '%');

	    if (percent_mode) last_char(metrics[0].threshold_level) = 0;

	    double val = atof (metrics[0].threshold_level);
	    if (percent_mode) val /= 100;

	    char thresholdlevel[80];
	    sprintf (thresholdlevel, "%.4f", val);

	    replace_string_in_string (line, "$thresholdlevel$", 
		thresholdlevel);
	}

	/*
        if (strstr (line, "$u-min-rnd$"))
	    replace_string_in_string (line, "$u-min-rnd$", u_min_rand);
        if (strstr (line, "$u-max-rnd$"))
	    replace_string_in_string (line, "$u-max-rnd$", u_max_rand);

        if (strstr (line, "$n-min-rnd$"))
	    replace_string_in_string (line, "$n-min-rnd$", n_min_rand);
        if (strstr (line, "$n-max-rnd$"))
	    replace_string_in_string (line, "$n-max-rnd$", n_max_rand);
        if (strstr (line, "$n-rand-mean$"))
	    replace_string_in_string (line, "$n-rand-mean$", n_rand_mean);
        if (strstr (line, "$n-std-or-sigma$"))
	    replace_string_in_string (line, "$n-std-or-sigma$", n_std_or_sigma);


        if (strstr (line, "$c-min-rnd$"))
	    replace_string_in_string (line, "$c-min-rnd$", c_min_rand);
        if (strstr (line, "$c-max-rnd$"))
	    replace_string_in_string (line, "$c-max-rnd$", c_max_rand);
        if (strstr (line, "$c-rand-mean$"))
	    replace_string_in_string (line, "$c-rand-mean$", c_rand_mean);
        if (strstr (line, "$c-std-or-sigma$"))
	    replace_string_in_string (line, "$c-std-or-sigma$", c_std_or_sigma);
	*/

	/*
	for (int i = 1;  i <= n_vars;  i++)
	{
	    if (strequal (vars[i], "normrandom"))
	    {
		if (strstr (line, "#if-n-min-set#"))
		{
		    replace_string_in_string (line, "#if-n-min-set#", "");
		    if (!mins_set[i]) print_off = true;
		}
		if (strstr (line, "#endif-n-min-set#"))
		{
		    replace_string_in_string (line, "#endif-n-min-set#", "");
		    print_off = false;
		}

		if (strstr (line, "#if-n-max-set#"))
		{
		    replace_string_in_string (line, "#if-n-max-set#", "");
		    if (!maxs_set[i]) print_off = true;
		}
		if (strstr (line, "#endif-n-max-set#"))
		{
		    replace_string_in_string (line, "#endif-n-max-set#", "");
		    print_off = false;
		}
	    }

	    else if (strequal (vars[i], "cauchyrandom"))
	    {
		if (strstr (line, "#if-c-min-set#"))
		{
		    replace_string_in_string (line, "#if-c-min-set#", "");
		    if (!mins_set[i]) print_off = true;
		}
		if (strstr (line, "#endif-c-min-set#"))
		{
		    replace_string_in_string (line, "#endif-c-min-set#", "");
		    print_off = false;
		}

		if (strstr (line, "#if-c-max-set#"))
		{
		    replace_string_in_string (line, "#if-c-max-set#", "");
		    if (!maxs_set[i]) print_off = true;
		}
		if (strstr (line, "#endif-c-max-set#"))
		{
		    replace_string_in_string (line, "#endif-c-max-set#", "");
		    print_off = false;
		}
	    }
	}
	*/

        if (strstr (line, "$initial-year$"))
	    replace_string_in_string (line, "$initial-year$", initial_year);

        if (strstr (line, "$threshold-label$"))
	    replace_string_in_string (line, "$threshold-label$", 
	    	threshold_label);

        if (strstr (line, "$government-entity$"))
	    replace_string_in_string (line, "$government-entity$", 
		government_entity);
        if (strstr (line, "$model-description$"))
	    replace_string_in_string (line, "$model-description$", 
		model_description);
        if (strstr (line, "$currency-units-in$"))
	    replace_string_in_string (line, "$currency-units-in$", 
		currency_units_in);
        if (strstr (line, "$trials$"))
	    replace_string_in_string (line, "$trials$", 
		number_of_trials);
        if (strstr (line, "$run-date-time$"))
	    replace_string_in_string (line, "$run-date-time$",
		timestamp());

        if (strstr (line, "$metrics-1-description$"))
	    replace_string_in_string (line, "$metrics-1-description$",
	        metrics[0].description);
        if (strstr (line, "$metrics-2-description$"))
	    replace_string_in_string (line, "$metrics-2-description$",
	        metrics[1].description);
        if (strstr (line, "$metrics-3-description$"))
	    replace_string_in_string (line, "$metrics-3-description$",
	        metrics[2].description);
        if (strstr (line, "$metrics-4-description$"))
	    replace_string_in_string (line, "$metrics-4-description$",
	        metrics[3].description);
        if (strstr (line, "$metrics-5-description$"))
	    replace_string_in_string (line, "$metrics-5-description$",
		metrics[4].description);

	if (strstr (line, "$print-ratios("))
	{
	    char* where_var = strstr (line, "$print-ratios(");

	    char* var;
	    strcpy (&var, where_var);
	    *(strchr (var, ')')+2) = 0;

	    int ind;
	    sscanf (where_var+14, "%d", &ind);

	    char* print_condition =
		strstr (metrics[ind-1].expression, "y-1") ? (char*)"false" : (char*)"true";

	    replace_string_in_string (line, var, print_condition);
	}

	if (strstr (line, "$if-ratio2-valid$"))
	    replace_string_in_string (line, "$if-ratio2-valid$",
		n_metrics >= 2 ? "" : "//" );

	if (strstr (line, "$if-ratio3-valid$"))
	    replace_string_in_string (line, "$if-ratio3-valid$",
		n_metrics >= 3 ? "" : "//" );
	if (strstr (line, "$if-ratio4-valid$"))
	    replace_string_in_string (line, "$if-ratio4-valid$",
		n_metrics >= 4 ? "" : "//" );
	if (strstr (line, "$if-ratio5-valid$"))
	    replace_string_in_string (line, "$if-ratio5-valid$",
		n_metrics >= 5 ? "" : "//" );

	if (strstr (line, "$random-headers$"))
	{
	    for (int i = 1;  i <= n_vars;  i++)
	    {
		if (!is_random (vars[i])) continue;

		print_indent ();

		char* main_title;
		char* suffix;
		get_title (vars[i], main_title, suffix);

		fprintf (f_out, "fprintf (fp, \"%s %s\\t\");\n", main_title, suffix);
		++line_no;
	    }

	    continue;
	}

	if (strstr (line, "$unirandom-declarations$"))
	{
	    for (int i = 1;  i <= n_vars;  i++)
	    {
		if (!is_random (vars[i])) continue;

		if (starts_with (vars[i], (char*)"unirandom"))
		{
		    print_indent ();
		    fprintf (f_out, "        boost::uniform_real < > gen_%s (%s, %s);\n", vars[i], mins[i], maxs[i]);

		    print_indent ();
		    fprintf (f_out, "        boost::variate_generator < boost::lagged_fibonacci1279&, boost::uniform_real < > > rand_%s (rng, gen_%s);\n", vars[i], vars[i]);
		    line_no += 2;
		}
	    }

	    continue;
	}

	if (strstr (line, "$normrandom-declarations$"))
	{
	    for (int i = 1;  i <= n_vars;  i++)
	    {
		if (!is_random (vars[i])) continue;

		if (starts_with (vars[i], (char*)"normrandom"))
		{
		    print_indent ();
		    fprintf (f_out, "        boost::normal_distribution < > gen_%s (%s, %s);\n", vars[i], means[i], std_sigmas[i]);

		    print_indent ();
		    fprintf (f_out, "        boost::variate_generator < boost::lagged_fibonacci1279&, boost::normal_distribution < > > rand_%s (rng, gen_%s);\n", vars[i], vars[i]);
		    line_no += 2;
		}
	    }

	    continue;
	}

	if (strstr (line, "$cauchyrandom-declarations$"))
	{
	    for (int i = 1;  i <= n_vars;  i++)
	    {
		if (!is_random (vars[i])) continue;

		if (starts_with (vars[i], (char*)"cauchyrandom"))
		{
		    print_indent ();
		    fprintf (f_out, "        boost::cauchy_distribution < > gen_%s (%s, %s);\n", vars[i], means[i], std_sigmas[i]);

		    print_indent ();
		    fprintf (f_out, "        boost::variate_generator < boost::lagged_fibonacci1279&, boost::cauchy_distribution < > > rand_%s (rng, gen_%s);\n", vars[i], vars[i]);
		    line_no += 2;
		}
	    }

	    continue;
	}

	if (strstr (line, "$unirandom-assignments$"))
	{
	    for (int i = 1;  i <= n_vars;  i++)
	    {
		if (!is_random (vars[i])) continue;

		if (starts_with (vars[i], (char*)"unirandom"))
		{
		    ::print_indent (f_out, line);
		    fprintf (f_out, "    %s[y] = rand_%s();\n", vars[i], vars[i]);
		    ++line_no;
		}

	    }

	    continue;
	}

	if (strstr (line, "$normrandom-assignments$"))
	{
	    for (int i = 1;  i <= n_vars;  i++)
		if (starts_with (vars[i], (char*)"normrandom"))
		{
		    ::print_indent (f_out, line);
		    fprintf (f_out, "for (;;)\n");
		    ::print_indent (f_out, line);
		    fprintf (f_out, "{\n");
		    ::print_indent (f_out, line);
		    fprintf (f_out, "%s[y] = rand_%s();\n", vars[i], vars[i]);
		    line_no += 3;

		    if (mins_set[i]) 
		    {
			::print_indent (f_out, line);
			fprintf (f_out, 
			    "if (%s[y] < %s) continue;\n", vars[i], mins[i]);
			++line_no;
		    }
		    if (maxs_set[i])
		    {
			::print_indent (f_out, line);
			fprintf (f_out, 
			    "if (%s[y] > %s) continue;\n", vars[i], maxs[i]);
			++line_no;
		    }

		    ::print_indent (f_out, line);
		    fprintf (f_out, "break;\n");
		    ::print_indent (f_out, line);
		    fprintf (f_out, "}\n");
		    line_no += 2;
		}

	    continue;
	}

	if (strstr (line, "$cauchyrandom-assignments$"))
	{
	    for (int i = 1;  i <= n_vars;  i++)
		if (starts_with (vars[i], (char*)"cauchyrandom"))
		{
		    ::print_indent (f_out, line);
		    fprintf (f_out, "for (;;)\n");
		    ::print_indent (f_out, line);
		    fprintf (f_out, "{\n");
		    ::print_indent (f_out, line);
		    fprintf (f_out, "%s[y] = rand_%s();\n", vars[i], vars[i]);
		    line_no += 3;

		    if (mins_set[i]) 
		    {
			::print_indent (f_out, line);
			fprintf (f_out, 
			    "if (%s[y] < %s) continue;\n", vars[i], mins[i]);
			++line_no;
		    }
		    if (maxs_set[i])
		    {
			::print_indent (f_out, line);
			fprintf (f_out, 
			    "if (%s[y] > %s) continue;\n", vars[i], maxs[i]);
			++line_no;
		    }

		    ::print_indent (f_out, line);
		    fprintf (f_out, "break;\n");
		    ::print_indent (f_out, line);
		    fprintf (f_out, "}\n");
		    line_no += 2;
		}

	    continue;
	}

	if (strstr (line, "$print-random-values$"))
	{
	    for (int i = 1;  i <= n_vars;  i++)
	    {
		if (!is_random (vars[i])) continue;

		::print_indent (f_out, line);
		fprintf (f_out, "    fprintf(fp,\"%%5.4f\\t\",%s[y]);\n", vars[i]);
		++line_no;
	    }

	    continue;
	}

	if (strstr (line, "$print-y0-random-values$"))
	{
	    for (int i = 1;  i <= n_vars;  i++)
	    {
		if (!is_random (vars[i])) continue;

		::print_indent (f_out, line);
		fprintf (f_out, "    fprintf(fp,\"\\t\");\n");
		++line_no;
	    }

	    continue;
	}

// by RW 31/05/12
	/*
	do_adjustments ();
	*/
	if (strstr (line, "$adjustments$"))
	{
	    if (have_adjustments)
		do_adjustments ();

	    continue;
	}
// end

	/*
	if (strstr (line, "$rating-equivalent"))
	    do_rating_equivalent (line);
	*/

        if (strstr (line, "$n-ratings$"))
	    replace_string_in_string (line, "$n-ratings$", n_ratings);

        if (strstr (line, "$ratings$"))
	{
	    char* txt_ratings;
	    strcpy (&txt_ratings, "");

	    for (int i = 2;  i <= n_ratings+1;  i++)
	    {
		if (i > 2) chrcat (&txt_ratings, ',');
	        strcat (&txt_ratings, "\"", ratings[i], "\"");
	    }

	    replace_string_in_string (line, "$ratings$", txt_ratings);
	    free (txt_ratings);
	}

        if (strstr (line, "$rating-grid$"))
	{
	    char* grid;
	    strcpy (&grid, "");

	    for (int i = 1;  i <= projection_years;  i++)
	    {
	        chrcat (&grid, '{');

		for (int j = 2;  j <= n_ratings;  j++)
		{
		    if (j > 2) chrcat (&grid, ',');
		    strcat (&grid, ratings_grid[i][j]);
		}

	        chrcat (&grid, '}');

		if (i < projection_years) 
		    strcat (&grid, ",\n");
	    }

	    replace_string_in_string (line, "$rating-grid$", grid);
	    free (grid);
	}

        if (strstr (line, "$default-probability-code$"))
	    replace_string_in_string (line, "$default-probability-code$", 
		def_prob_code);

	if (!print_off) 
	{
	    fprintf (f_out, "%s\n", line);
	    ++line_no;
	}

	record_indent (line);

//	printf ("-- %d [%s]\n", line_no, line);
    }
}
Exemplo n.º 19
0
int main (int argc, char **argv)
{
    // Optimize allocation for Tornado (2GB hash allocated after 1GB dictionary)
    AllocTopDown = FALSE;

    // Operation mode
    OPMODE global_mode=AUTO;

    // Record that stores all the info required for ReadWriteCallback
    static Results r;
    r.use_cpu_time = r.quiet_title = r.quiet_header = r.quiet_progress = r.quiet_result = FALSE;

    // Default compression parameters are equivalent to option -5
    r.method = std_Tornado_method [default_Tornado_method];

    // Delete successfully (de)compressed input files
    bool delete_input_files = FALSE;

    // Count of files to process
    int fcount=0;

    // Output path/filename
    const char *output_filename = NULL;

    // Process options until "--"
    // 1. First, process -1..-16 option if any
    for (char **argv_ptr = argv; *++argv_ptr!=NULL; ) {
        char *param = *argv_ptr;
        if (*param == '-') {
            param++;
                 if (strcasecmp(param,"-")==0)   break;   // "--" is a "stop processing" option
            else if (isdigit(*param))            r.method = std_Tornado_method[check_parse_int (param, 1, elements(std_Tornado_method)-1, param-1)];
        }
    }
    // 2. Second, process rest of options
    for (char **argv_ptr = argv; *++argv_ptr!=NULL; ) {
        char *param = *argv_ptr;
        if (param[0] != '-' || param[1]=='\0') {
            fcount++;
        } else { param++;  int error=0;
                 if (strcasecmp(param,"-")==0)      break;
            else if (strcasecmp(param,"") ==0)      continue;
            else if (strcasecmp(param,"z")==0)      global_mode=_COMPRESS;
            else if (strcasecmp(param,"d")==0)      global_mode=_DECOMPRESS;
            else if (strcasecmp(param,"delete")==0) delete_input_files=TRUE;
            else if (strcasecmp(param,"t")==0)      output_filename="";
            else if (strcasecmp(param,"q")==0)      r.quiet_title = r.quiet_header = r.quiet_progress = r.quiet_result = TRUE;
#ifndef FREEARC_NO_TIMING
            else if (strcasecmp(param,"cpu")==0)    r.use_cpu_time=TRUE;
#endif
            else if (strcasecmp(param,"h")==0)      global_mode=HELP;
            else if (strcasecmp(param,"b")==0)      r.method.buffer = MAX_BUFFER_SIZE;         // set buffer size to the maximum supported by LZ coders
            else if (strcasecmp(param,"x")==0)      r.method.caching_finder = CACHING_MF4;
            else if (strcasecmp(param,"xx")==0)     r.method.caching_finder = CYCLED_MF4;
            else if (strcasecmp(param,"x+")==0)     r.method.caching_finder = CACHING_MF4;
            else if (strcasecmp(param,"x-")==0)     r.method.caching_finder = NON_CACHING_MF;
            else if (strcasecmp(param,"t+")==0)     r.method.find_tables = TRUE;
            else if (strcasecmp(param,"t-")==0)     r.method.find_tables = FALSE;
            else if (strcasecmp(param,"t1")==0)     r.method.find_tables = TRUE;
            else if (strcasecmp(param,"t0")==0)     r.method.find_tables = FALSE;
            else if (strcasecmp(param,"s")==0)      r.method.hash3 = 1;
            else if (strcasecmp(param,"ss")==0)     r.method.hash3 = 2;
            else if (strcasecmp(param,"s+")==0)     r.method.hash3 = 1;
            else if (strcasecmp(param,"s-")==0)     r.method.hash3 = 0;
            else if (start_with(param,"fb"))        r.method.fast_bytes = check_parse_int (param+2, MIN_FAST_BYTES, MAX_FAST_BYTES, *argv_ptr);
#ifdef FREEARC_WIN
            else if (strcasecmp(param,"slp-")==0)   DefaultLargePageMode = DISABLE;
            else if (strcasecmp(param,"slp" )==0)   DefaultLargePageMode = TRY;
            else if (strcasecmp(param,"slp+")==0)   DefaultLargePageMode = FORCE;
#endif
            else if (start_with(param,"rem"))       /* ignore option */;
            else if (isdigit(*param))            ; // -1..-16 option is already processed :)
            else switch( tolower(*param++) ) {
                case 'b': r.method.buffer          = check_parse_mem (param, MIN_BUFFER_SIZE, MAX_BUFFER_SIZE,    *argv_ptr);  break;
                case 'h': r.method.hashsize        = check_parse_mem (param, MIN_HASH_SIZE,   MAX_HASH_SIZE,      *argv_ptr);  break;
                case 'l': r.method.hash_row_width  = check_parse_int (param, 1,               MAX_HASH_ROW_WIDTH, *argv_ptr);  break;
                case 'u': r.method.update_step     = check_parse_int (param, 1,               MAX_UPDATE_STEP,    *argv_ptr);  break;
                case 'c': r.method.encoding_method = check_parse_int (param, BYTECODER,       ARICODER,           *argv_ptr);  break;
                case 's': r.method.hash3           = check_parse_int (param, 0,               2,                  *argv_ptr);  break;
                case 'p': r.method.match_parser    = parseInt (param, &error);  break;
                case 'x': r.method.caching_finder  = parseInt (param, &error);  break;
                case 'o': output_filename          = param;                     break;
                case 'q':
#ifndef FREEARC_NO_TIMING
                          r.quiet_title            = strchr (param, 't');
                          r.quiet_progress         = strchr (param, 'p');
#endif
                          r.quiet_header           = strchr (param, 'h');
                          r.quiet_result           = strchr (param, 'r');
                          break;
                case 'a': switch( tolower(*param++) ) {
                            case 'h': r.method.auxhash_size      = check_parse_mem (param, MIN_HASH_SIZE, MAX_HASH_SIZE, *argv_ptr);  goto check_for_errors;
                            case 'l': r.method.auxhash_row_width = check_parse_int (param, 1,        MAX_HASH_ROW_WIDTH, *argv_ptr);  goto check_for_errors;
                          }
                          // 'a' should be the last case!
                default : fprintf (stderr, "ERROR: unknown option '%s'\n", *argv_ptr);
                          exit(FREEARC_EXIT_ERROR);
            }
check_for_errors:
            if (error) {
                fprintf (stderr, "ERROR: bad option format: '%s'\n", *argv_ptr);
                exit(FREEARC_EXIT_ERROR);
            }
            if (!(r.method.match_parser==GREEDY || r.method.match_parser==LAZY  || r.method.match_parser==OPTIMAL)) {
                fprintf (stderr, "ERROR: bad option value: '%s'\n", *argv_ptr);
                exit(FREEARC_EXIT_ERROR);
            }
            int mf = r.method.caching_finder;
            r.method.caching_finder = mf = (mf==CACHING_MF4_DUB? CACHING_MF4 : (mf==CYCLED_MF4_DUB? CYCLED_MF4 : mf));
            if (!(mf==NON_CACHING_MF || (CYCLED_MF4<=mf && mf<=CYCLED_MF7) || (CACHING_MF4<=mf && mf<=CACHING_MF7) || (BT_MF4<=mf && mf<=BT_MF7))) {
                fprintf (stderr, "ERROR: non-existing match finder: '%s'\n", *argv_ptr);
                exit(FREEARC_EXIT_ERROR);
            }
        }
    }

    // No files to compress: read from stdin and write to stdout
    if (global_mode!=HELP && fcount==0 &&
       (global_mode!=AUTO  ||  !isatty(0) && !isatty(1)) ) {

        static char *_argv[] = {argv[0], (char*)"-", NULL};
        argv = _argv;
        fcount = 1;

    } else if (global_mode==HELP || fcount==0) {
        char h[100], ah[100], b[100], MinHashSizeStr[100], MaxHashSizeStr[100], MinBufSizeStr[100], MaxBufSizeStr[100];
        showMem64 (r.method.hashsize, h);
        showMem64 (r.method.auxhash_size, ah);
        showMem64 (r.method.buffer, b);
        showMem64 (MIN_HASH_SIZE, MinHashSizeStr);
        showMem64 (MAX_HASH_SIZE+1, MaxHashSizeStr);
        showMem64 (MIN_BUFFER_SIZE, MinBufSizeStr);
        showMem64 (MAX_BUFFER_SIZE, MaxBufSizeStr);
        printf( "Tornado compressor v0.6  (c) [email protected]  http://freearc.org  2014-03-08\n"
                "\n"
                " Usage: tor [options and files in any order]\n"
                "   -#      -- compression level (1..%d), default %d\n", int(elements(std_Tornado_method))-1, default_Tornado_method);
        printf( "   -z      -- force compression\n"
                "   -d      -- force decompression\n"
                "   -oNAME  -- output filename/directory (default %s/%s)\n", COMPRESS_EXT, DECOMPRESS_EXT);
        printf( "   -t      -- test (de)compression (redirect output to nul)\n"
                "   -delete -- delete successfully (de)compressed input files\n"
#ifdef FREEARC_NO_TIMING
                "   -q      -- be quiet; -q[hr]* disables header/results individually\n"
#else
                "   -q      -- be quiet; -q[thpr]* disables title/header/progress/results individually\n"
                "   -cpu    -- compute raw CPU time (for benchmarking)\n"
#endif
#ifdef FREEARC_WIN
                "   -slp[+/-/]   -- force/disable/try(default) large pages support (2mb/4mb)\n"
#endif
                "   -rem... -- command-line remark\n"
                "   -h      -- display this help\n"
                "   --      -- stop flags processing\n"
                " \"-\" used as filename means stdin/stdout\n"
                "\n"
                " Advanced compression parameters:\n"
                "   -b#     -- buffer size (%s..%s), default %s\n", MinBufSizeStr, MaxBufSizeStr, b);
        printf( "   -h#     -- hash size (%s..%s-1), default %s\n", MinHashSizeStr, MaxHashSizeStr, h);
        printf( "   -l#     -- length of hash row (1..%d), default %d\n", int(MAX_HASH_ROW_WIDTH), r.method.hash_row_width);
        printf( "   -ah#    -- auxiliary hash size (%s..%s-1), default %s\n", MinHashSizeStr, MaxHashSizeStr, ah);
        printf( "   -al#    -- auxiliary hash row length (1..%d), default %d\n", int(MAX_HASH_ROW_WIDTH), r.method.auxhash_row_width);
        printf( "   -u#     -- update step (1..%d), default %d\n", int(MAX_UPDATE_STEP), r.method.update_step);
        printf( "   -c#     -- coder (1-bytes, 2-bits, 3-huf, 4-arith), default %d\n", r.method.encoding_method);
        printf( "   -p#     -- parser (1-greedy, 2-lazy, 4-optimal), default %d\n", r.method.match_parser);
        printf( "   -x#     -- match finder (0: non-caching ht4, 4-7: cycling cached ht4..ht7,\n"
                "                            14-17: shifting cached ht4..ht7, 24-27: bt4..bt7), default %d\n", r.method.caching_finder);
        printf( "   -s#     -- 2/3-byte hash (0-disabled, 1-fast, 2-max), default %d\n", r.method.hash3);
        printf( "   -t#     -- table diffing (0-disabled, 1-enabled), default %d\n", r.method.find_tables);
        printf( "   -fb#    -- fast bytes in the optimal parser (%d..%d), default %d\n", int(MIN_FAST_BYTES), int(MAX_FAST_BYTES), r.method.fast_bytes);
        printf( "\n"
                " Predefined methods:\n");
        for (int i=1; i<elements(std_Tornado_method); i++)
        {
            printf("   %-8d-- %s\n", -i, name(std_Tornado_method[i]));
        }
        exit(EXIT_SUCCESS);
    }



    // (De)compress all files given on cmdline
    bool parse_options=TRUE;  // options will be parsed until "--"
    Install_signal_handler(signal_handler);

    for (char **parameters = argv; *++parameters!=NULL; )
    {
        // If options are still parsed and this argument starts with "-" - it's an option
        if (parse_options && parameters[0][0]=='-' && parameters[0][1]) {
            if (strequ(*parameters,"--"))  parse_options=FALSE;
            continue;
        }

        // Save input filename
        r.filename = *parameters;

        // Select operation mode if it was not specified on cmdline
        r.mode = global_mode != AUTO?                  global_mode :
                 end_with (r.filename, COMPRESS_EXT)?  _DECOMPRESS  :
                                                       _COMPRESS;
        // Extension that should be added to output filenames
        const char *MODE_EXT  =  r.mode==_COMPRESS? COMPRESS_EXT : DECOMPRESS_EXT;

        // Construct output filename
        if (r.mode==BENCHMARK  ||  output_filename && strequ (output_filename, "")) {  // Redirect output to nul
            strcpy (r.outname, "");
        } else if (output_filename) {
            if (strequ(output_filename,"-"))
                strcpy (r.outname, output_filename);
            else if (is_path_char (last_char (output_filename)))
                {sprintf(r.outname, "%s%s", output_filename, drop_dirname(r.filename));  goto add_remove_ext;}
            else if (dir_exists (output_filename))
                {sprintf(r.outname, "%s%c%s", output_filename, PATH_DELIMITER, drop_dirname(r.filename));  goto add_remove_ext;}
            else
                strcpy (r.outname, output_filename);
        } else if (strequ(r.filename,"-")) {
            strcpy (r.outname, r.filename);
        } else {
            // No output filename was given on cmdline:
            //    on compression   - add COMPRESS_EXT
            //    on decompression - remove COMPRESS_EXT (and add DECOMPRESS_EXT if file already exists)
            strcpy (r.outname, r.filename);
add_remove_ext: // Remove COMPRESS_EXT on the end of name or add DECOMPRESS_EXT (unless we are in _COMPRESS mode)
            if (r.mode!=_COMPRESS && end_with (r.outname, COMPRESS_EXT)) {
                r.outname [strlen(r.outname) - strlen(COMPRESS_EXT)] = '\0';
                if (file_exists (r.outname))
                    strcat(r.outname, MODE_EXT);
            } else {
                strcat(r.outname, MODE_EXT);
            }
        }

        // Open input file
        r.fin = strequ (r.filename, "-")? stdin : file_open_read_binary(r.filename);
        if (r.fin == NULL) {
            fprintf (stderr, "\n Can't open %s for read\n", r.filename);
            exit(FREEARC_EXIT_ERROR);
        }
        set_binary_mode (r.fin);

        // Open output file
        if (*r.outname) {
          r.fout = strequ (r.outname, "-")? stdout : file_open_write_binary(r.outname);
          if (r.fout == NULL) {
              fprintf (stderr, "\n Can't open %s for write\n", r.outname);
              exit(FREEARC_EXIT_ERROR);
          }
          set_binary_mode (r.fout);
        } else {
          r.fout = NULL;
        }

        // Prepare to (de)compression
        int result;  char filesize_str[100];
        start_print_stats(r);

        // Perform actual (de)compression
        switch (r.mode) {
        case _COMPRESS: {
            if (!r.quiet_header && r.filesize >= 0)
                fprintf (stderr, "Compressing %s bytes with %s\n", show3(r.filesize,filesize_str), name(r.method));
            PackMethod m = r.method;
            if (r.filesize >= 0)
                m.buffer = mymin (m.buffer, r.filesize+LOOKAHEAD*2);
            result = tor_compress (m, ReadWriteCallback, &r, NULL, -1);
            break; }

        case _DECOMPRESS: {
            //if (!r.quiet_header && !strequ (r.outname, "-"))   fprintf (stderr, "Unpacking %.3lf mb\n", double(r.filesize)/1000/1000);
            result = tor_decompress (ReadWriteCallback, &r, NULL, -1);
            break; }
        }

        // Finish (de)compression
        print_final_stats(r);
        fclose (r.fin);
        if (r.fout)  fclose (r.fout);

        if (result == FREEARC_OK)  {
            if (delete_input_files && !strequ(r.filename,"-"))    delete_file(r.filename);
        } else {
            if (!strequ(r.outname,"-") && !strequ(r.outname,""))  delete_file(r.outname);
            switch (result) {
            case FREEARC_ERRCODE_INVALID_COMPRESSOR:
                fprintf (stderr, "\nThis compression mode isn't supported by small Tornado version, use full version instead!");
                break;
            case FREEARC_ERRCODE_NOT_ENOUGH_MEMORY:
                fprintf (stderr, "\nNot enough memory for (de)compression!");
                break;
            case FREEARC_ERRCODE_READ:
                fprintf (stderr, "\nRead error! Bad media?");
                break;
            case FREEARC_ERRCODE_WRITE:
                fprintf (stderr, "\nWrite error! Disk full?");
                break;
            case FREEARC_ERRCODE_BAD_COMPRESSED_DATA:
                fprintf (stderr, "\nData can't be decompressed!");
                break;
            default:
                fprintf (stderr, "\n(De)compression failed with error code %d!", result);
                break;
            }
            exit(FREEARC_EXIT_ERROR);
        }

        // going to next file...
    }

    return EXIT_SUCCESS;
}
Exemplo n.º 20
0
_nc_get_token(bool silent)
{
    static const char terminfo_punct[] = "@%&*!#";
    long number;
    int type;
    int ch;
    char *numchk;
    char numbuf[80];
    unsigned found;
    static char buffer[MAX_ENTRY_SIZE];
    char *ptr;
    int dot_flag = FALSE;
    long token_start;

    if (pushtype != NO_PUSHBACK) {
	int retval = pushtype;

	_nc_set_type(pushname);
	DEBUG(3, ("pushed-back token: `%s', class %d",
		  _nc_curr_token.tk_name, pushtype));

	pushtype = NO_PUSHBACK;
	pushname[0] = '\0';

	/* currtok wasn't altered by _nc_push_token() */
	return (retval);
    }

    if (end_of_stream())
	return (EOF);

  start_token:
    token_start = stream_pos();
    while ((ch = next_char()) == '\n' || iswhite(ch))
	continue;

    ch = eat_escaped_newline(ch);

    if (ch == EOF)
	type = EOF;
    else {
	/* if this is a termcap entry, skip a leading separator */
	if (separator == ':' && ch == ':')
	    ch = next_char();

	if (ch == '.'
#if NCURSES_EXT_FUNCS
	    && !_nc_disable_period
#endif
	    ) {
	    dot_flag = TRUE;
	    DEBUG(8, ("dot-flag set"));

	    while ((ch = next_char()) == '.' || iswhite(ch))
		continue;
	}

	if (ch == EOF) {
	    type = EOF;
	    goto end_of_token;
	}

	/* have to make some punctuation chars legal for terminfo */
	if (!isalnum(ch)
#if NCURSES_EXT_FUNCS
	    && !(ch == '.' && _nc_disable_period)
#endif
	    && !strchr(terminfo_punct, (char) ch)) {
	    if (!silent)
		_nc_warning("Illegal character (expected alphanumeric or %s) - %s",
			    terminfo_punct, unctrl((chtype) ch));
	    _nc_panic_mode(separator);
	    goto start_token;
	}

	ptr = buffer;
	*(ptr++) = ch;

	if (first_column) {
	    char *desc;

	    _nc_comment_start = token_start;
	    _nc_comment_end = _nc_curr_file_pos;
	    _nc_start_line = _nc_curr_line;

	    _nc_syntax = ERR;
	    while ((ch = next_char()) != '\n') {
		if (ch == EOF)
		    _nc_err_abort("premature EOF");
		else if (ch == ':' && last_char() != ',') {
		    _nc_syntax = SYN_TERMCAP;
		    separator = ':';
		    break;
		} else if (ch == ',') {
		    _nc_syntax = SYN_TERMINFO;
		    separator = ',';
		    /*
		     * Fall-through here is not an accident.  The idea is that
		     * if we see a comma, we figure this is terminfo unless we
		     * subsequently run into a colon -- but we don't stop
		     * looking for that colon until hitting a newline.  This
		     * allows commas to be embedded in description fields of
		     * either syntax.
		     */
		    /* FALLTHRU */
		} else
		    ch = eat_escaped_newline(ch);

		*ptr++ = ch;
	    }
	    ptr[0] = '\0';
	    if (_nc_syntax == ERR) {
		/*
		 * Grrr...what we ought to do here is barf, complaining that
		 * the entry is malformed.  But because a couple of name fields
		 * in the 8.2 termcap file end with |\, we just have to assume
		 * it's termcap syntax.
		 */
		_nc_syntax = SYN_TERMCAP;
		separator = ':';
	    } else if (_nc_syntax == SYN_TERMINFO) {
		/* throw away trailing /, *$/ */
		for (--ptr; iswhite(*ptr) || *ptr == ','; ptr--)
		    continue;
		ptr[1] = '\0';
	    }

	    /*
	     * This is the soonest we have the terminal name fetched.  Set up
	     * for following warning messages.
	     */
	    ptr = strchr(buffer, '|');
	    if (ptr == (char *) NULL)
		ptr = buffer + strlen(buffer);
	    ch = *ptr;
	    *ptr = '\0';
	    _nc_set_type(buffer);
	    *ptr = ch;

	    /*
	     * Compute the boundary between the aliases and the description
	     * field for syntax-checking purposes.
	     */
	    desc = strrchr(buffer, '|');
	    if (!silent && desc) {
		if (*desc == '\0')
		    _nc_warning("empty longname field");
		else if (strchr(desc, ' ') == (char *) NULL)
		    _nc_warning("older tic versions may treat the description field as an alias");
	    }
	    if (!desc)
		desc = buffer + strlen(buffer);

	    /*
	     * Whitespace in a name field other than the long name can confuse
	     * rdist and some termcap tools.  Slashes are a no-no.  Other
	     * special characters can be dangerous due to shell expansion.
	     */
	    for (ptr = buffer; ptr < desc; ptr++) {
		if (isspace(CharOf(*ptr))) {
		    if (!silent)
			_nc_warning("whitespace in name or alias field");
		    break;
		} else if (*ptr == '/') {
		    if (!silent)
			_nc_warning("slashes aren't allowed in names or aliases");
		    break;
		} else if (strchr("$[]!*?", *ptr)) {
		    if (!silent)
			_nc_warning("dubious character `%c' in name or alias field", *ptr);
		    break;
		}
	    }

	    ptr = buffer;

	    _nc_curr_token.tk_name = buffer;
	    type = NAMES;
	} else {
	    while ((ch = next_char()) != EOF) {
		if (!isalnum(ch)) {
		    if (_nc_syntax == SYN_TERMINFO) {
			if (ch != '_')
			    break;
		    } else {	/* allow ';' for "k;" */
			if (ch != ';')
			    break;
		    }
		}
		*(ptr++) = ch;
	    }

	    *ptr++ = '\0';
	    switch (ch) {
	    case ',':
	    case ':':
		if (ch != separator)
		    _nc_err_abort("Separator inconsistent with syntax");
		_nc_curr_token.tk_name = buffer;
		type = BOOLEAN;
		break;
	    case '@':
		if ((ch = next_char()) != separator && !silent)
		    _nc_warning("Missing separator after `%s', have %s",
				buffer, unctrl((chtype) ch));
		_nc_curr_token.tk_name = buffer;
		type = CANCEL;
		break;

	    case '#':
		found = 0;
		while (isalnum(ch = next_char())) {
		    numbuf[found++] = ch;
		    if (found >= sizeof(numbuf) - 1)
			break;
		}
		numbuf[found] = '\0';
		number = strtol(numbuf, &numchk, 0);
		if (!silent) {
		    if (numchk == numbuf)
			_nc_warning("no value given for `%s'", buffer);
		    if ((*numchk != '\0') || (ch != separator))
			_nc_warning("Missing separator");
		}
		_nc_curr_token.tk_name = buffer;
		_nc_curr_token.tk_valnumber = number;
		type = NUMBER;
		break;

	    case '=':
		ch = _nc_trans_string(ptr, buffer + sizeof(buffer));
		if (!silent && ch != separator)
		    _nc_warning("Missing separator");
		_nc_curr_token.tk_name = buffer;
		_nc_curr_token.tk_valstring = ptr;
		type = STRING;
		break;

	    case EOF:
		type = EOF;
		break;
	    default:
		/* just to get rid of the compiler warning */
		type = UNDEF;
		if (!silent)
		    _nc_warning("Illegal character - %s", unctrl((chtype) ch));
	    }
	}			/* end else (first_column == FALSE) */
    }				/* end else (ch != EOF) */

  end_of_token:

#ifdef TRACE
    if (dot_flag == TRUE)
	DEBUG(8, ("Commented out "));

    if (_nc_tracing >= DEBUG_LEVEL(7)) {
	switch (type) {
	case BOOLEAN:
	    _tracef("Token: Boolean; name='%s'",
		    _nc_curr_token.tk_name);
	    break;

	case NUMBER:
	    _tracef("Token: Number;  name='%s', value=%d",
		    _nc_curr_token.tk_name,
		    _nc_curr_token.tk_valnumber);
	    break;

	case STRING:
	    _tracef("Token: String;  name='%s', value=%s",
		    _nc_curr_token.tk_name,
		    _nc_visbuf(_nc_curr_token.tk_valstring));
	    break;

	case CANCEL:
	    _tracef("Token: Cancel; name='%s'",
		    _nc_curr_token.tk_name);
	    break;

	case NAMES:

	    _tracef("Token: Names; value='%s'",
		    _nc_curr_token.tk_name);
	    break;

	case EOF:
	    _tracef("Token: End of file");
	    break;

	default:
	    _nc_warning("Bad token type");
	}
    }
#endif

    if (dot_flag == TRUE)	/* if commented out, use the next one */
	type = _nc_get_token(silent);

    DEBUG(3, ("token: `%s', class %d",
	      _nc_curr_token.tk_name != 0 ? _nc_curr_token.tk_name :
	      "<null>",
	      type));

    return (type);
}
Exemplo n.º 21
0
Arquivo: xp-actions.c Projeto: mpw/p2
BOOLEAN
not_concrete (char *s)
{
    char c = last_char(s);
    return(c == '#');
}
Exemplo n.º 22
0
/**
 * Sends the HTTP Response along with the web
 * content to the socket file descriptor.
 *
 * @param fileToSend: location of file to display to client
 * @param sock: Socket file descriptor
 * @param home: 
 * @param content:
 * @param response: Holds information regarding HTTP request
 */
void SendDataBin(char *fileToSend, int sock, char *home, 
		char *content, HTTP_Response *response) {
    
	char *fullPathToFile; //[256];
	char Header[1024];
	char buffer[256];
	char *endChar;
	int size;

	bzero(Header, sizeof(Header));

	/*
	 * Build the full path to the file
	 */

	bool hasSlash;
	if ((last_char(content) == "/") || (first_char(fileToSend) == '/'))
		hasSlash = TRUE;
	else
		hasSlash = FALSE;
	
	size = strlen(home) + strlen(content) + strlen(fileToSend);
	if (hasSlash)
	{
		size += 2;
		fullPathToFile = malloc(size);
		sprintf(fullPathToFile, "%s/%s%s", home, content, fileToSend);
	}	
	else
	{
		size += 3;
		fullPathToFile = malloc(size);
		sprintf(fullPathToFile, "%s/%s/%s", home, content, fileToSend);
	}
	//int file_open = open(fullPathToFile, O_RDONLY);
	int fileType = TypeOfFile(fullPathToFile);

	printf("Filetype: %d\n", fileType);

	if ((fileType == FILE_NOT_FOUND) || (fileType == ERROR_FILE))
	{
		//Requested page not available.
		bzero(fullPathToFile, strlen(fullPathToFile));
		size = strlen(home) + strlen(content);

		if (last_char(content) == "/")
		{
			size += 16;
			fullPathToFile = realloc(fullPathToFile, size);
			sprintf(fullPathToFile, "%s/%snot_found.html", home, content);
		}
		else
		{
			size += 17;
			fullPathToFile = realloc(fullPathToFile, size);
			sprintf(fullPathToFile, "%s/%s/not_found.html", home, content);
		}
	}
	else if (fileType == DIRECTORY)
	{
		//Append index.html
		size += 10;
		fullPathToFile = realloc(fullPathToFile, size);
		strcat(fullPathToFile, "index.html");
	}
	
	printf("File to open: %s\n", fullPathToFile);
	int file_open;
	FILE *file_fd = NULL;

	if (fileType == EXECUTABLE_FILE)
	{
		if ((file_fd = popen(fullPathToFile, "r")) == NULL) 
		{
			perror("popen");
			exit(-1);
		}

		file_open = fileno(file_fd);  //Convert from FILE to file descriptor integer
	}
	else
	{
		file_open = open(fullPathToFile, O_RDONLY);
	}

	if (file_open < 0)
	{
		perror("Open file error");
		exit(-1);
	}

	long unsigned filesize  = lseek(file_open, (off_t)0, SEEK_END);
	lseek(file_open, (off_t)0, SEEK_SET); //Set back to starting position
	
	setupHeader(Header, response, filesize);

	printf("\nHeader:\n%s\n\n", Header);	

	/*
	 * Send the header, open the requested file.
	 * Send requested file, close file.
	 */

	printf("file: %s\n", fullPathToFile);
	write(sock, Header, strlen(Header));

	// If request was for HEAD, do not send the body information
	if (strncmp(response -> HTTP_Type, "HEAD", 4) == 0)
	{
		free (fullPathToFile);
		fullPathToFile = NULL;
		return;
	}


	bzero(buffer, sizeof(buffer));
	while (read(file_open, buffer, sizeof(buffer)) > 0)
	{
		write(sock, buffer, sizeof(buffer));
		bzero(buffer, sizeof(buffer));
	}
	
	if (fileType == EXECUTABLE_FILE)
		pclose(file_fd);
	else
		close(file_open);

	// Deallocate allocated pointers
	free (fullPathToFile);
	fullPathToFile = NULL;
}