Beispiel #1
0
static bool parse_line(config_file_t *conf,
      struct config_entry_list *list, char *line, config_file_cb_t *cb)
{
   char *comment   = NULL;
   char *key_tmp   = NULL;
   size_t cur_size = 8;
   size_t idx      = 0;
   char *key       = (char*)malloc(9);

   if (!key)
      return false;

   comment = strip_comment(line);

   /* Starting line with #include includes config files. */
   if (comment == line)
   {
      comment++;
      if (strstr(comment, "include ") == comment)
      {
         char *line = comment + strlen("include ");
         char *path = extract_value(line, false);

         if (path)
         {
            if (conf->include_depth >= MAX_INCLUDE_DEPTH)
               fprintf(stderr, "!!! #include depth exceeded for config. Might be a cycle.\n");
            else
               add_sub_conf(conf, path, cb);
            free(path);
         }
         goto error;
      }
   }

   /* Skips to first character. */
   while (isspace((int)*line))
      line++;

   while (isgraph((int)*line))
   {
      if (idx == cur_size)
      {
         cur_size *= 2;
         key_tmp = (char*)realloc(key, cur_size + 1);

         if (!key_tmp)
            goto error;

         key = key_tmp;
      }

      key[idx++] = *line++;
   }
   key[idx]      = '\0';
   list->key     = key;

   list->value   = extract_value(line, true);

   if (!list->value)
   {
      list->key = NULL;
      goto error;
   }

   return true;

error:
   free(key);
   return false;
}
Beispiel #2
0
static int init(struct sr_input *in, const char *filename)
{
	int res;
	struct context *ctx;
	const char *param;
	GIOStatus status;
	gsize i, term_pos;
	char probe_name[SR_MAX_PROBENAME_LEN + 1];
	struct sr_probe *probe;
	char **columns;
	gsize num_columns;
	char *ptr;

	if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
		sr_err("Context malloc failed.");
		return SR_ERR_MALLOC;
	}

	/* Create a virtual device. */
	in->sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, NULL, NULL, NULL);
	in->internal = ctx;

	/* Set default samplerate. */
	ctx->samplerate = 0;

	/*
	 * Enable auto-detection of the number of probes in multi column mode
	 * and enforce the specification of the number of probes in single
	 * column mode.
	 */
	ctx->num_probes = 0;

	/* Set default delimiter. */
	if (!(ctx->delimiter = g_string_new(","))) {
		sr_err("Delimiter malloc failed.");
		free_context(ctx);
		return SR_ERR_MALLOC;
	}

	/*
	 * Set default comment prefix. Note that an empty comment prefix
	 * disables removing of comments.
	 */
	if (!(ctx->comment = g_string_new(""))) {
		sr_err("Comment malloc failed.");
		free_context(ctx);
		return SR_ERR_MALLOC;
	}

	/* Enable multi column mode by default. */
	ctx->multi_column_mode = TRUE;

	/* Use first column as default single column number. */
	ctx->single_column = 0;

	/*
	 * In multi column mode start parsing sample data at the first column
	 * and in single column mode at the first bit.
	 */
	ctx->first_probe = 0;

	/* Start at the beginning of the file. */
	ctx->start_line = 1;

	/* Disable the usage of the first line as header by default. */
	ctx->header = FALSE;

	/* Set default format for single column mode. */
	ctx->format = FORMAT_BIN;

	if (!(ctx->buffer = g_string_new(""))) {
		sr_err("Line buffer malloc failed.");
		free_context(ctx);
		return SR_ERR_MALLOC;
	}

	if (in->param) {
		if ((param = g_hash_table_lookup(in->param, "samplerate"))) {
			res = sr_parse_sizestring(param, &ctx->samplerate);

			if (res != SR_OK) {
				sr_err("Invalid samplerate: %s.", param);
				free_context(ctx);
				return SR_ERR_ARG;
			}
		}

		if ((param = g_hash_table_lookup(in->param, "numprobes")))
			ctx->num_probes = g_ascii_strtoull(param, NULL, 10);

		if ((param = g_hash_table_lookup(in->param, "delimiter"))) {
			if (!strlen(param)) {
				sr_err("Delimiter must be at least one character.");
				free_context(ctx);
				return SR_ERR_ARG;
			}

			if (!g_ascii_strcasecmp(param, "\\t"))
				g_string_assign(ctx->delimiter, "\t");
			else
				g_string_assign(ctx->delimiter, param);
		}

		if ((param = g_hash_table_lookup(in->param, "comment")))
			g_string_assign(ctx->comment, param);

		if ((param = g_hash_table_lookup(in->param, "single-column"))) {
			ctx->single_column = g_ascii_strtoull(param, &ptr, 10);
			ctx->multi_column_mode = FALSE;

			if (param == ptr) {
				sr_err("Invalid single-colum number: %s.",
					param);
				free_context(ctx);
				return SR_ERR_ARG;
			}
		}

		if ((param = g_hash_table_lookup(in->param, "first-probe")))
			ctx->first_probe = g_ascii_strtoull(param, NULL, 10);

		if ((param = g_hash_table_lookup(in->param, "startline"))) {
			ctx->start_line = g_ascii_strtoull(param, NULL, 10);

			if (ctx->start_line < 1) {
				sr_err("Invalid start line: %s.", param);
				free_context(ctx);
				return SR_ERR_ARG;
			}
		}

		if ((param = g_hash_table_lookup(in->param, "header")))
			ctx->header = sr_parse_boolstring(param);

		if ((param = g_hash_table_lookup(in->param, "format"))) {
			if (!g_ascii_strncasecmp(param, "bin", 3)) {
				ctx->format = FORMAT_BIN;
			} else if (!g_ascii_strncasecmp(param, "hex", 3)) {
				ctx->format = FORMAT_HEX;
			} else if (!g_ascii_strncasecmp(param, "oct", 3)) {
				ctx->format = FORMAT_OCT;
			} else {
				sr_err("Invalid format: %s.", param);
				free_context(ctx);
				return SR_ERR;
			}
		}
	}

	if (ctx->multi_column_mode)
		ctx->first_column = ctx->first_probe;
	else
		ctx->first_column = ctx->single_column;

	if (!ctx->multi_column_mode && !ctx->num_probes) {
		sr_err("Number of probes needs to be specified in single column mode.");
		free_context(ctx);
		return SR_ERR;
	}

	if (!(ctx->channel = g_io_channel_new_file(filename, "r", NULL))) {
		sr_err("Input file '%s' could not be opened.", filename);
		free_context(ctx);
		return SR_ERR;
	}

	while (TRUE) {
		ctx->line_number++;
		status = g_io_channel_read_line_string(ctx->channel,
			ctx->buffer, &term_pos, NULL);

		if (status == G_IO_STATUS_EOF) {
			sr_err("Input file is empty.");
			free_context(ctx);
			return SR_ERR;
		}

		if (status != G_IO_STATUS_NORMAL) {
			sr_err("Error while reading line %zu.",
				ctx->line_number);
			free_context(ctx);
			return SR_ERR;
		}

		if (ctx->start_line > ctx->line_number) {
			sr_spew("Line %zu skipped.", ctx->line_number);
			continue;
		}

		/* Remove line termination character(s). */
		g_string_truncate(ctx->buffer, term_pos);

		if (!ctx->buffer->len) {
			sr_spew("Blank line %zu skipped.", ctx->line_number);
			continue;
		}

		/* Remove trailing comment. */
		strip_comment(ctx->buffer, ctx->comment);

		if (ctx->buffer->len)
			break;

		sr_spew("Comment-only line %zu skipped.", ctx->line_number);
	}

	/*
	 * In order to determine the number of columns parse the current line
	 * without limiting the number of columns.
	 */
	if (!(columns = parse_line(ctx, -1))) {
		sr_err("Error while parsing line %zu.", ctx->line_number);
		free_context(ctx);
		return SR_ERR;
	}

	num_columns = g_strv_length(columns);

	/* Ensure that the first column is not out of bounds. */
	if (!num_columns) {
		sr_err("Column %zu in line %zu is out of bounds.",
			ctx->first_column, ctx->line_number);
		g_strfreev(columns);
		free_context(ctx);
		return SR_ERR;
	}

	if (ctx->multi_column_mode) {
		/*
		 * Detect the number of probes in multi column mode
		 * automatically if not specified.
		 */
		if (!ctx->num_probes) {
			ctx->num_probes = num_columns;
			sr_info("Number of auto-detected probes: %zu.",
				ctx->num_probes);
		}

		/*
		 * Ensure that the number of probes does not exceed the number
		 * of columns in multi column mode.
		 */
		if (num_columns < ctx->num_probes) {
			sr_err("Not enough columns for desired number of probes in line %zu.",
				ctx->line_number);
			g_strfreev(columns);
			free_context(ctx);
			return SR_ERR;
		}
	}

	for (i = 0; i < ctx->num_probes; i++) {
		if (ctx->header && ctx->multi_column_mode && strlen(columns[i]))
			snprintf(probe_name, sizeof(probe_name), "%s",
				columns[i]);
		else
			snprintf(probe_name, sizeof(probe_name), "%zu", i);

		probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, probe_name);

		if (!probe) {
			sr_err("Probe creation failed.");
			free_context(ctx);
			g_strfreev(columns);
			return SR_ERR;
		}

		in->sdi->probes = g_slist_append(in->sdi->probes, probe);
	}

	g_strfreev(columns);

	/*
	 * Calculate the minimum buffer size to store the sample data of the
	 * probes.
	 */
	ctx->sample_buffer_size = (ctx->num_probes + 7) >> 3;

	if (!(ctx->sample_buffer = g_try_malloc(ctx->sample_buffer_size))) {
		sr_err("Sample buffer malloc failed.");
		free_context(ctx);
		return SR_ERR_MALLOC;
	}

	return SR_OK;
}
Beispiel #3
0
static bool parse_line(config_file_t *conf,
      struct config_entry_list *list, char *line)
{
   char *comment   = NULL;
   char *key       = (char*)malloc(9);
   char *key_tmp   = NULL;
   size_t cur_size = 8;
   size_t idx      = 0;

   if (!key)
      return false;

   if (!line || !*line)
   {
      free(key);
      return false;
   }

   comment = strip_comment(line);

   /* Starting line with # and include includes config files. */
   if ((comment == line) && (conf->include_depth < MAX_INCLUDE_DEPTH))
   {
      comment++;
      if (strstr(comment, "include ") == comment)
      {
         add_sub_conf(conf, comment + strlen("include "));
         free(key);
         return false;
      }
   }
   else if (conf->include_depth >= MAX_INCLUDE_DEPTH)
   {
      fprintf(stderr, "!!! #include depth exceeded for config. Might be a cycle.\n");
   }

   /* Skips to first character. */
   while (isspace((int)*line))
      line++;

   while (isgraph((int)*line))
   {
      if (idx == cur_size)
      {
         cur_size *= 2;
         key_tmp = (char*)realloc(key, cur_size + 1);

         if (!key_tmp)
         {
            free(key);
            return false;
         }

         key = key_tmp;
      }

      key[idx++] = *line++;
   }
   key[idx] = '\0';
   list->key = key;
   list->key_hash = djb2_calculate(key);

   list->value = extract_value(line, true);
   if (!list->value)
   {
      list->key = NULL;
      free(key);
      return false;
   }

   return true;
}
Beispiel #4
0
int process_file(FILE *ifp)
{
  char *ibuf, *config = NULL;
  byte **rom = NULL;       /* pointers to ROM images */
  byte *data = NULL;      /* data accumulators      */
  int line = 0, first = 1, nroms = 0, abits = 0, length = 0, res = 0;
  int ix;

  /* Allocate space to read strings into */
  if((ibuf = calloc(MAXLINE + 1, sizeof(char))) == NULL) {
    fprintf(stderr, "Insufficient memory to process file\n");
    return 1; /* out of memory */
  }

  /* Read strings from the input file */
  while(read_line(ifp, ibuf, MAXLINE)) {
    ++line;
    strip_comment(ibuf);

    /* Blank lines are skipped in all cases */
    if(is_blank(ibuf))
      continue;

    strip_whitespace(ibuf);

    /*
      When we see the first line, we need to accumulate some other
      info we're going to need later.  This includes the number of
      address bits (abits), and the number of ROMs (nroms).  We also
      need to save a copy of the configuration line for later, since
      ibuf will be overwritten with each line that is consumed.

      We also range-check the values of abits and nroms, and allocate
      memory for the ROM images we will build during the reading of
      the rest of the file.  We save the length of the configuration
      line (length) so that we can syntax check subsequent lines in
      an efficent manner.
     */
    if(first) {

      /* Check structural validity of configuration line */
      if(!valid_string(ibuf, "0123456789Aa")) {
        fprintf(stderr,
                "Line %d: invalid character in configuration\n",
                line);
        res = 1;
        goto CLEANUP;
      }

      /* Count number of ROM cells and address (state) bits */
      nroms = count_roms(ibuf);
      abits = count_addr(ibuf);

      /* Complain if we didn't get at least 1 ROM, or if we got too many */
      if(nroms < 1) {
        fprintf(stderr, "Line %d: must specify at least 1 ROM number\n",
                line);
        res = 2;
        goto CLEANUP;
      } else if(nroms > NUM_ROMS) {
        fprintf(stderr, "Line %d: cannot specify more than %d ROMs\n",
                line, NUM_ROMS);
        res = 2;
        goto CLEANUP;
      }

      /* Complain if we got no address bits, or more than MAXBITS */
      if(abits < 1 || abits > MAXBITS) {
        fprintf(stderr, "Line %d: must have between 1-%d state bits\n",
                line, MAXBITS);
        res = 3;
        goto CLEANUP;
      }

      /* Okay, the config is alright, save it for later ... */
      if((config = copy_string(ibuf)) == NULL) {
        fprintf(stderr, "Insufficient memory to process file\n");
        res = 1;
        goto CLEANUP;
      }

      /* Hang onto the length, we'll need it later */
      length = strlen(config);

      /* Allocate space for the ROMs and their accumulators */
      if(!alloc_roms(&rom, nroms, config, abits)) {
        fprintf(stderr, "Insufficient memory to process file\n");
        res = 1;
        goto CLEANUP;
      }
      if((data = calloc(nroms, sizeof(byte))) == NULL) {
        fprintf(stderr, "Insufficient memory to process file\n");
        res = 1;
        goto CLEANUP;
      }

      first = 0;
      continue;
    } /* end if(first) */

    /* Translate output "don't care" values to regular bits */
    translate(ibuf, OUTPUT_DC, g_odcv);

    /* Anything bad left in the string? */
    if(!valid_string(ibuf, "01Xx")) {
      fprintf(stderr, "Line %d: invalid character in data\n", line);
      res = 1;
      goto CLEANUP;
    }

    /* Make sure we got enough fields to satisfy the template */
    if(strlen(ibuf) != length) {
      fprintf(stderr,
              "Line %d: wrong number of fields (wanted %u, got %u)\n",
              line, (unsigned) length, (unsigned) strlen(ibuf));
      res = 4;
      goto CLEANUP;
    }

    /* Grab all the data out of the line, escaping on error */
    if(!parse_data(ibuf, line, config, abits, data)) {
      res = 5;
      goto CLEANUP;
    }

    /* Write the data into the ROM images.  We know which ROMs
       to use by checking the pointers in the ROM image array,
       and the accumulator is already set to go
     */
    for(ix = 0; ix < nroms; ix++) {
      if(rom[ix]) {
        write_range(rom[ix], ibuf, abits, data[ix]);
      }
    }

    /* Clear out accumulators for the next round */
    memset(data, 0, nroms);

  } /* end while(read_line(...)) */

  /* If we didn't get a first line at all, the file was logically
     empty (i.e., not even a configuration!) */
  if(first) {
    fprintf(stderr, "No configuration line was found\n");
    res = 7;

  } else {

    /* Having accumulated all the data into the ROM images, we now
       will dump them out into the appropriate files */
    if(!dump_roms(rom, nroms, abits, g_fmt))
      res = 6;
  }

 CLEANUP:
  free(ibuf);

  if(config)
    free(config);

  if(rom) {
    free_roms(rom, nroms);
    free(rom);
  }

  if(data)
    free(data);

  return res;

} /* end process_file() */
Beispiel #5
0
/** Parse text file and create
 * list elements for each instruction
 * appends instructions to the end of the list it gets
 * so that you can call this more than once, to include
 * more than one file
 * if you call it passing a root that is null, it will return
 * the beginning of the list
 * if you pass it something other than null, it returns a pointer
 * to the last link of the list
 */
struct instruction *parse_source(FILE *infile, struct instruction* initial_root, struct tab_entry *tabroot)
{

    struct instruction *cur_old =NULL, *cur = NULL;
    struct instruction *inst_root;
    char buffer[INSTRUCTION_BUFFER_SIZE];
    char b[INSTRUCTION_BUFFER_SIZE];
    char *buf, *ptr;
    int instructions = 0;
    int cur_op_num;

    inst_root = initial_root;
    buf = b;

    while (fgets(buffer, INSTRUCTION_BUFFER_SIZE, infile))
    {
        strip_comment(buffer);
        linenumber++;

        if (cur == NULL)
            cur = new_instruction();

        if (isblank(buffer[0]) || (buffer[0] == '\n') || (buffer[0] == '#') || (buffer[0] == '.')) {
            if (buffer[0] == '#') //make preprocessor directives .<directive>
                buffer[0] = '.';

            /** split line, get instruction and operands */
            if ((buf = (char *) strtok(buffer, whitespace))) {
                instructions++;

                if (!inst_root)
                    inst_root = cur;

                if (cur_old)
                    cur_old->next = cur;

                capitalize(buf);

                strncpy(cur->mnumonic, buf, MNUMONIC_TXT_LENGTH);

                get_operands(cur);
            }

            calculate_opcode(tabroot, cur);

            cur_old = cur;
            cur = new_instruction();

        } else {
            /* see if it's a valid label */
            if (strlen(buffer) > 0) {
              if ((buf = (char *) strtok(buffer, whitespace))) {
                  instructions++;

                  if (!inst_root)
                      inst_root = cur;

                  if (cur_old)
                      cur_old->next = cur;

                  capitalize(buf);

                  strncpy(cur->mnumonic, buf, MNUMONIC_TXT_LENGTH);

                  get_operands(cur);
              }
              // buffer holds untokenized string with charcter at *[0]
                if (validate_label(buffer)) {
                    add_label(buffer, cur);
                } else {
                    do_error_msg(ERR_BADLABEL);
                }

            }
        }
    }

    /* return either the head or the tail */
    return initial_root == NULL ? inst_root : cur;
}
Beispiel #6
0
bool Inicializer::initConfig(FILE** infile, std::vector<Particle > &pvec) {

    int err,fields,tmp_type;
    long j,current;
    char * line, line2[STRLEN];
    size_t line_size = (STRLEN + 1) * sizeof(char);
    line = (char *) malloc(line_size);
    //Particle chorig[MAXCHL];

    double maxlength = 0.0;
    for(int i = 0; i < MAXT; i++){
        if(maxlength < topo.ia_params[i][i].len[0])
            maxlength = topo.ia_params[i][i].len[0];
    }

    if(myGetLine(&line, &line_size, *infile) == -1){
        fprintf (stderr, "ERROR: Could not read box size (Inicializer::initConfig)\n\n");
        return false;
    }
    strip_comment(line);
    trim(line);
#ifdef WEDGE
    double angle, innerR, outerR;
    Vector box;
    if (sscanf(line, "%le %le %le %le", &outerR, &innerR, &box.z, &angle) != 4) {
        if(myGetLine(&line, &line_size, infile) == -1){
            fprintf (stderr, "ERROR: Could not read box size.\n\n");
            return false;
        }
        aftercommand(line2,line,BOXSEP);
        strip_comment(line2);
        trim(line2);
        if (sscanf(line2, "%le %le %le %le", &box.z, &angle, &outerR, &innerR) != 4) {
            fprintf (stderr, "ERROR: Could not read box size.\n\n");
            return false;
        }
    }

    conf->geo = Wedge(box.z, angle, outerR, innerR); //(double box.z, double angle, double outerR, double innerR)
#else
    Vector box;

    if (sscanf(line, "%le %le %le", &(box.x), &(box.y), &(box.z) ) != 3) {
        if(myGetLine(&line, &line_size, *infile) == -1){
            fprintf (stderr, "ERROR: Could not read box size2.\n\n");
            return false;
        }
        aftercommand(line2,line,BOXSEP);
        strip_comment(line2);
        trim(line2);
        if (sscanf(line2, "%le %le %le", &(box.x), &(box.y), &(box.z) ) != 3) {
            fprintf (stderr, "ERROR: Could not read box size3.\n\n");
            return false;
        }
    }

    conf->geo = Cuboid(box);
#endif
    if (conf->geo.box.x < maxlength * 2.0 + 2.0) {
        printf ("WARNING: x (%f) geo.box length is less than two spherocylinders long (%f).\n\n", conf->geo.box.x, maxlength * 2.0 + 2.0);
        exit(1);
    }
    if (conf->geo.box.y < maxlength * 2.0 + 2.0) {
        printf ("WARNING: y (%f) geo.box length is less than two spherocylinders long (%f).\n\n", conf->geo.box.y, maxlength * 2.0 + 2.0);
        exit(1);
    }
    if (conf->geo.box.z < maxlength * 2.0 + 2.0) {
        printf ("WARNING: z (%f) geo.box length is less than two spherocylinders long (%f).\n\n", conf->geo.box.z, maxlength * 2.0 + 2.0);
        exit(1);
    }

    DEBUG_INIT("Position of the particle");
    for(unsigned int i=0; i < pvec.size(); i++) {
        if(myGetLine(&line, &line_size, *infile) == -1){
            break;
        }
        strip_comment(line);
        trim(line);

        fields = sscanf(line, "%le %le %le %le %le %le %le %le %le %d",
                        &pvec[i].pos.x, &pvec[i].pos.y, &pvec[i].pos.z,
                        &pvec[i].dir.x, &pvec[i].dir.y, &pvec[i].dir.z,
                        &pvec[i].patchdir[0].x, &pvec[i].patchdir[0].y, &pvec[i].patchdir[0].z,
                        &pvec[i].switched);

        pvec[i].patchdir[1].x = pvec[i].patchdir[1].y = pvec[i].patchdir[1].z =0;
        pvec[i].chdir[0].x = pvec[i].chdir[0].y = pvec[i].chdir[0].z =0;
        pvec[i].chdir[1].x = pvec[i].chdir[1].y = pvec[i].chdir[1].z =0;
        DEBUG_INIT("Line: %s\nNumber of Fields: %d", line, fields);
        if (fields == 9){
            pvec[i].switched = 0;
            fprintf(stdout, "WARNING: Particle %u is assumed to be not switched!\n", i+1);
            fields++;
        }
        if (fields != 10) {
            fprintf (stderr, "ERROR: Could not read coordinates for particle %u.\n \
                    Did you specify box size at the begining?\n\n", i+1);
            free(line);
            exit (1);
        }
        /* Scale position vector to the unit cube */
#ifdef WEDGE
        pvec[i].pos.x /= conf->geo.box.x;
        pvec[i].pos.y /= conf->geo.box.y;
        pvec[i].pos.z /= conf->geo.box.z;

        conf->geo.usePBC(&pvec[i]);
#else

        // For analysis of sheet
        //conf->geo.usePBC2(&pvec[i]); // range 0 - box

        pvec[i].pos.x /= conf->geo.box.x;
        pvec[i].pos.y /= conf->geo.box.y;
        pvec[i].pos.z /= conf->geo.box.z;

        // for compatibility unfortunately
        conf->geo.usePBC(&pvec[i]);
#endif

        if ((topo.ia_params[pvec[i].type][pvec[i].type].geotype[0]<SP)&&( DOT(pvec[i].dir, pvec[i].dir) < ZEROTOL )) {
            //DEBUG_INIT("Geotype = %d < %d", conf->pvec[i].geotype,SP);
            fprintf (stderr,
                    "ERROR: Null direction vector supplied for particle %u.\n\n", i+1);
            free(line);
            return false;
        } else {
            pvec[i].dir.normalise();
        }

        if ((topo.ia_params[pvec[i].type][pvec[i].type].geotype[0]<SP)&&( DOT(pvec[i].patchdir[0], pvec[i].patchdir[0]) < ZEROTOL )) {
            fprintf (stderr,
                    "ERROR: Null patch vector supplied for particle %u.\n\n", i+1);
            free(line);
            return false;
        } else {
            ortogonalise(&pvec[i].patchdir[0],&pvec[i].dir);
            pvec[i].patchdir[0].normalise();
        }
        // Switch the type
        if(pvec[i].switched){
            if(pvec[i].switchtype == 0){
                fprintf(stderr, "ERROR: Particle %u switched even though it has no switchtype", i);
                free(line);
                exit(1);
            }
            tmp_type = pvec[i].type;
            pvec[i].type = pvec[i].switchtype;
            pvec[i].switchtype = tmp_type;
        }

        DEBUG_INIT("%ld:\t%lf\t%lf\t%lf", i, pvec[i].pos.x, pvec[i].pos.y, pvec[i].pos.z);

    }
Beispiel #7
0
static void rd_nm2type_file(const char *fn, int *nnm, t_nm2type **nmp)
{
    FILE         *fp;
    gmx_bool      bCont;
    char          libfilename[128];
    char          format[128], f1[128];
    char          buf[1024], elem[16], type[16], nbbuf[16], **newbuf;
    int           i, nb, nnnm, line = 1;
    double        qq, mm, *blen;
    t_nm2type    *nm2t = NULL;

    fp = fflib_open(fn);
    if (NULL == fp)
    {
        gmx_fatal(FARGS, "Can not find %s in library directory", fn);
    }

    nnnm = *nnm;
    nm2t = *nmp;
    do
    {
        /* Read a line from the file */
        bCont = (fgets2(buf, 1023, fp) != NULL);

        if (bCont)
        {
            /* Remove comment */
            strip_comment(buf);
            if (sscanf(buf, "%s%s%lf%lf%d", elem, type, &qq, &mm, &nb) == 5)
            {
                /* If we can read the first four, there probably is more */
                srenew(nm2t, nnnm+1);
                snew(nm2t[nnnm].blen, nb);
                if (nb > 0)
                {
                    snew(newbuf, nb);
                    strcpy(format, "%*s%*s%*s%*s%*s");
                    for (i = 0; (i < nb); i++)
                    {
                        /* Complicated format statement */
                        strcpy(f1, format);
                        strcat(f1, "%s%lf");
                        if (sscanf(buf, f1, nbbuf, &(nm2t[nnnm].blen[i])) != 2)
                        {
                            gmx_fatal(FARGS, "Error on line %d of %s", line, libfilename);
                        }
                        newbuf[i] = strdup(nbbuf);
                        strcat(format, "%*s%*s");
                    }
                }
                else
                {
                    newbuf = NULL;
                }
                nm2t[nnnm].elem   = strdup(elem);
                nm2t[nnnm].type   = strdup(type);
                nm2t[nnnm].q      = qq;
                nm2t[nnnm].m      = mm;
                nm2t[nnnm].nbonds = nb;
                nm2t[nnnm].bond   = newbuf;
                nnnm++;
            }
            line++;
        }
    }
    while (bCont);
    gmx_ffclose(fp);

    *nnm = nnnm;
    *nmp = nm2t;
}