示例#1
0
void fprint_pdb_nopar(FILE * fop, struct atomgrp *ag, const char *inf)
{
    FILE *fp = myfopen(inf, "r");

    char *line = NULL;
    size_t len = 0;
    char c;

    // read every line of the pdb file
    int atomi = 0;
    while (getline(&line, &len, fp) != -1) {
        if (strncmp(line, "ATOM  ", 6) != 0
                && strncmp(line, "HETATM", 6) != 0) {
            fprintf(fop, "%s", line);
            continue;
        }
        c = line[54];
        sprintf(line + 30, "%8.3f", ag->atoms[atomi].X);
        sprintf(line + 38, "%8.3f", ag->atoms[atomi].Y);
        sprintf(line + 46, "%8.3f", ag->atoms[atomi].Z);
        atomi++;
        line[54] = c;
        fprintf(fop, "%s", line);
    }
    if (line)
        free(line);
    myfclose(fp);
}
示例#2
0
文件: srcfile.c 项目: meesokim/z88dk
/* Open the source file for reading, closing any previously open file.
   If dir_list is not NULL, calls search_file() to search the file in dir_list */
Bool SrcFile_open( SrcFile *self, char *filename, UT_array *dir_list )
{
    char *filename_path;
	
	/* close last file */
	if (self->file != NULL)
	{
		myfclose(self->file);
		self->file = NULL;
	}

	/* search path, add to strpool */
	filename_path = search_file(filename, dir_list);

	/* check for recursive includes, return if found */
	if (!check_recursive_include(self, filename_path))
		return FALSE;
	
	self->filename = filename_path;

    /* open new file in binary mode, for cross-platform newline processing */
    self->file = myfopen( self->filename, "rb" );

	/* init current line */
    str_clear( self->line );
    self->line_nr = 0;

	if (self->file)
		return TRUE;
	else
		return FALSE;		/* error opening file */
}
示例#3
0
文件: errors.c 项目: meesokim/z88dk
void close_error_file( void )
{
	struct stat st;
	int stat_res;

    init_module();

    /* close current file if any */
	if (error_file.file != NULL)
	{
		myfclose(error_file.file);

		/* delete file if no errors found */
		if (error_file.filename != NULL)
		{
			stat_res = stat(error_file.filename, &st);
			if (stat_res == 0 && st.st_size == 0)
				remove(error_file.filename);
		}
	}

    /* reset */
    error_file.file		= NULL;
    error_file.filename	= NULL;        /* filename kept in strpool, no leak */
}
示例#4
0
文件: srcfile.c 项目: meesokim/z88dk
static void free_file_stack_elem( void *_elem )
{
	FileStackElem *elem = _elem;
	
	if ( elem->file != NULL )
		myfclose( elem->file );
	m_free( elem );
}
示例#5
0
void fprint_pdb(struct atomgrp *ag, struct prm *prm, const char *path)
{
    FILE *fp = myfopen(path, "w");

    //struct atomgrp** ags = (struct atomgrp**) _mol_malloc (2 * sizeof (struct atomgrp*));
    struct atomgrp **ags = extract_nitrogen_residues(ag, prm);
    //ags[0] = copy_atomgrp (ag);
    //ags[1] = NULL; // flag the end with NULL

    int sum_atomi = 0;
    int agi = 0;
    while (ags[agi] != NULL) {
        int atomi;
        for (atomi = 0; atomi < ags[agi]->natoms; atomi++, sum_atomi++) {
            char atomname[5];
            fprintf(fp, "%-6s", "ATOM");	// atom number
            fprintf(fp, "%5d", sum_atomi + 1);	// atom number
            //fprintf (fp, "  "); // 2 spaces
            fprintf(fp, " ");	// 1 space

            if (strlen
                    (prm->atoms[ags[agi]->atoms[atomi].atom_typen].
                     typemin) == 4) {
                strcpy(atomname,
                       prm->atoms[ags[agi]->atoms[atomi].
                                  atom_typen].typemin);
            } else {
                atomname[0] = ' ';
                strcpy(atomname + 1,
                       prm->atoms[ags[agi]->atoms[atomi].
                                  atom_typen].typemin);
            }
            fprintf(fp, "%-4.4s", atomname);	// atom typemin
            fprintf(fp, " ");	// Alternate location indicator
            fprintf(fp, "%-3s", prm->atoms[ags[agi]->atoms[atomi].atom_typen].typemaj);	// atom typemaj

            fprintf(fp, " ");	// 1 space
            fprintf(fp, "%1s", "A");	// chain id
            fprintf(fp, "%4d", agi + 1);	// residue number

            fprintf(fp, " ");	// code for insertion of residues
            fprintf(fp, "   ");	// 3 spaces

            fprintf(fp, "%8.3f", ags[agi]->atoms[atomi].X);	// X coordinate
            fprintf(fp, "%8.3f", ags[agi]->atoms[atomi].Y);	// Y coordinate
            fprintf(fp, "%8.3f", ags[agi]->atoms[atomi].Z);	// Z coordinate
            fprintf(fp, "  1.00  1.00      AAAA");	// segid
            fprintf(fp, "\n");	// new line
        }
        agi++;
    }
    fprintf(fp, "END\n");	//END

    //free (ags);
    free_atomgrps(ags);

    myfclose(fp);
}
示例#6
0
文件: srcfile.c 项目: meesokim/z88dk
void SrcFile_fini( SrcFile *self )
{
    if ( self->file != NULL )
        myfclose( self->file );

	str_delete(self->line);
    OBJ_DELETE( self->line_stack );
    OBJ_DELETE( self->file_stack );
}
void fprint_ms_nopar (struct atomgrp* ag, const char* inf, const char* ouf)
{
	FILE* fp = myfopen (inf, "r");
	FILE* fop = myfopen (ouf, "w");

	char* line = NULL;
	size_t len = 0;
	
	
	// read every line of the pdb file
	int atomi = 0;
	while (getline (&line, &len, fp) != -1)
	{
		float attl;
		if (strncmp (line, "ATOM  ", 6) != 0 && strncmp (line, "HETATM", 6) != 0)
		{
			fprintf (fop,"%s",line);
			continue;
		}
		sprintf(line+30,"%8.3f",ag->atoms[atomi].X );
		sprintf(line+38,"%8.3f",ag->atoms[atomi].Y );
		sprintf(line+46,"%8.3f",ag->atoms[atomi].Z );
		attl=0.0;
		if (ag->atoms[atomi].attl > 0.0)
		{
			attl += ag->atoms[atomi].attl;
		}
		if (ag->atoms[atomi].sa > 0)
		{
			attl += (float) ag->atoms[atomi].sa;
		}
		if (attl > 9.0)
		{
			fprintf (stderr, "warning: attraction level is > 9.0 for an atom\n");
		}
		sprintf(line+54,"%6.1f   \n",attl );
		atomi++;
		fprintf (fop,"%s",line);
	}
	if (line)
	        free (line);
	myfclose (fp);
	myfclose (fop);
}
示例#8
0
文件: move.cpp 项目: ampereira/F2Dock
struct rotset* read_rotset (const char* path)
{
	FILE* fp = myfopen (path, "r"); // open parms file

	char* line = NULL;
	size_t len = 0;

	int nrots = 0; // number of sets of rots
	while (getline (&line, &len, fp) != -1)
	{
		if (! iswhiteline (line))
		{
			nrots++;
		}
	}
	rewind (fp); // reset the file pointer

	struct rotset* rotset = (struct rotset*) _mol_malloc (sizeof (struct rotset));
	rotset->nrots = nrots; // save the value
	rotset->rmats = (struct rmatrix*) _mol_malloc (rotset->nrots * sizeof (struct rmatrix));

	int i = 0;
	while (getline (&line, &len, fp) != -1)
	{
		int tmpi; // tmp roti
		if (iswhiteline (line))
		{
			continue;
		}
		if (sscanf (line,	"%d %f %f %f %f %f %f %f %f %f",
							&tmpi,
							&rotset->rmats[i].a11, &rotset->rmats[i].a12, &rotset->rmats[i].a13,
							&rotset->rmats[i].a21, &rotset->rmats[i].a22, &rotset->rmats[i].a23,
							&rotset->rmats[i].a31, &rotset->rmats[i].a32, &rotset->rmats[i].a33
							) < 10)
		{
			fprintf (stderr, "read error on file %s\n", path);
			fprintf (stderr, "expecting one index and 9 floats per line %d\n", i);
			exit (EXIT_FAILURE);
		}
		if (tmpi != i)
		{
			fprintf (stderr, "read error on file %s\n", path);
			fprintf (stderr, "expecting index %d, found index %d\n", i, tmpi);
			exit (EXIT_FAILURE);
		}
		i++;
	}
	if (line)
		free (line);
	myfclose (fp);

	return rotset;
}
示例#9
0
文件: move.cpp 项目: ampereira/F2Dock
struct axisset* read_axisset (const char* path)
{
	FILE* fp = myfopen (path, "r"); // open parms file

	char* line = NULL;
	size_t len = 0;

	int naxes = 0; // number of sets of rots
	while (getline (&line, &len, fp) != -1)
	{
		if (! iswhiteline (line))
		{
			naxes++;
		}
	}
	rewind (fp); // reset the file pointer

	struct axisset* axisset = (struct axisset*) _mol_malloc (sizeof (struct axisset));
	axisset->naxes = naxes; // save the value
	axisset->axes = (struct axis*) _mol_malloc (axisset->naxes * sizeof (struct axis));

	int i = 0;
	while (getline (&line, &len, fp) != -1)
	{
		int tmpi; // tmp roti
		if (iswhiteline (line))
		{
			continue;
		}
		if (sscanf (line,	"%d %f %f %f",
							&tmpi,
							&axisset->axes[i].a,
							&axisset->axes[i].b,
							&axisset->axes[i].c
							) < 4)
		{
			fprintf (stderr, "read error on file %s\n", path);
			fprintf (stderr, "expecting one index and 3 floats per line %d\n", i);
			exit (EXIT_FAILURE);
		}
		if (tmpi != i)
		{
			fprintf (stderr, "read error on file %s\n", path);
			fprintf (stderr, "expecting index %d, found index %d\n", i, tmpi);
			exit (EXIT_FAILURE);
		}
		i++;
	}
	if (line)
		free (line);
	myfclose (fp);

	return axisset;
}
示例#10
0
文件: 8_3.c 项目: RalfMBecker/K-R
int main(){

  printf("**Not fully tested, eg, max of 20 file open and such.\n");
  printf("**Buffering set small so replenishing of buffer is checked.\n");

  int c;

  myFILE *fp1;
  myFILE *fp2;
  char namein[MAXWORD];
  char nameout[MAXWORD];

  strcpy(namein, "8_1.c");
  strcpy(nameout, "testfile");

  myfflush(NULL);
  if ( ( fp1 = myfopen(namein, "r") ) == NULL ){
    fprintf(stderr, "Error: can't open file %s for reading\n", namein);
    exit(1);
  }
  else if ( ( fp2 = myfopen(nameout, "w") ) == NULL ){
    fprintf(stderr, "Error: can't open file %s for writing\n", nameout);
    exit(2);
  }

  while ( ( c = getc(fp1) ) != EOF )
    putc(c, fp2);
	     
  if ( myfclose(fp1) == EOF){
    fprintf(stderr, "Error: can't close file %s.\n", namein); 
    exit(3);
  }
  else if ( myfclose(fp1) == EOF){
    fprintf(stderr, "Error: can't close file %s.\n", nameout);
    exit(4);
  } 

  return 0;
}
示例#11
0
/* _______________________________________________________________ */
int main(int argc, char* argv[]) {
    MY_FILE* fsrc;
    MY_FILE* fdst;
    if (argc != 3 && (argc != 4 || !streq(argv[1], "--append")))
        error("Utilisation: ./mycp [--append] SOURCE DEST");
    if (argc == 3) {
        if ((fsrc = myfopen(argv[1], "r")) == MY_NULL)
            error("Erreur d'ouverture du fichier source.");
        if ((fdst = myfopen(argv[2], "w")) == MY_NULL)
            error("Erreur d'ouverture du fichier destination.");
    } else {
        if ((fsrc = myfopen(argv[2], "r")) == MY_NULL)
            error("Erreur d'ouverture du fichier source.");
        if ((fdst = myfopen(argv[3], "a")) == MY_NULL)
            error("Erreur d'ouverture du fichier destination.");
    }
    copy(fsrc, fdst);
    if (myfclose(fsrc) != 0)
        error("Erreur de fermeture du fichier source.");
    if (myfclose(fdst) != 0)
        error("Erreur de fermeture du fichier destination.");
    return 0;
}
示例#12
0
文件: status.cpp 项目: fishman/asfr
int initstatus(struct JOB_PARM *My_Job)
{
	int i;
	char *DashPtr = strrchr(My_Job->URL, '/');
	if( DashPtr == NULL ) DashPtr = My_Job->URL;
	else DashPtr += 1;

	strcpy(My_Job->StaFileName, DashPtr);
	strcat(My_Job->StaFileName, ".resume");

	My_Job->StaFile = fopen(My_Job->StaFileName, "r+");
	if(My_Job->StaFile != NULL)	/* A sta file exists */
	{
		fgetline(My_Job->StaFile, My_Job->URL);
		fgetline(My_Job->StaFile, My_Job->Proxy);
		fgetline(My_Job->StaFile, My_Job->Username);
		fgetline(My_Job->StaFile, My_Job->Password);
		fscanf(My_Job->StaFile, "{%d %d %ld %ld}\n", &My_Job->MaxThreads, &My_Job->MaxTime, &My_Job->SpecOffset, &My_Job->SpecLength);
		if(My_Job->MaxThreads > MAX_THREADS) My_Job->MaxThreads=MAX_THREADS;
		if(My_Job->MaxThreads < 1) My_Job->MaxThreads=1;
		for(i=0 ; i<My_Job->MaxThreads ; i++)
		{
			if( My_Job->ti[i] != NULL ) free(My_Job->ti[i]);
			if( (My_Job->ti[i] = (struct THREAD_INFO *)malloc(sizeof(struct THREAD_INFO))) == NULL) return 1;
			memset(My_Job->ti[i], 0, sizeof(struct THREAD_INFO));
		}

		for(i=0 ; i<My_Job->MaxThreads ; i++)
			fscanf(My_Job->StaFile, "{%ld %ld}\n", &My_Job->ti[i]->SourOffset, &My_Job->ti[i]->DestLength);

		myfclose( & My_Job->StaFile);
	}
	else /* Create one */
	{
		if(My_Job->MaxThreads > MAX_THREADS) My_Job->MaxThreads=MAX_THREADS;
		if(My_Job->MaxThreads < 1) My_Job->MaxThreads=1;
		for(i=0 ; i<My_Job->MaxThreads ; i++)
		{
			if( My_Job->ti[i] != NULL ) free(My_Job->ti[i]);
			if( (My_Job->ti[i] = (struct THREAD_INFO *)malloc(sizeof(struct THREAD_INFO))) == NULL) return -1;
			memset(My_Job->ti[i], 0, sizeof(struct THREAD_INFO));
		}
		savestatus(My_Job);
	}
	return 0;
}
示例#13
0
文件: srcfile.c 项目: meesokim/z88dk
Bool SrcFile_pop( SrcFile *self )
{
	FileStackElem *elem;
	
	if ( List_empty( self->file_stack ) )
		return FALSE;
		
	if ( self->file != NULL )
		myfclose( self->file );
		
	elem = List_pop( self->file_stack );
	self->file		= elem->file;
	self->filename	= elem->filename;
	self->line_nr	= elem->line_nr;
	
	m_free( elem );
	return TRUE;
}
示例#14
0
文件: sasa.cpp 项目: ampereira/F2Dock
int* read_sasa (const char* path)
{
	FILE* fp = myfopen (path, "r");

	int nsasas = 100; // just a guess, realloc as necessary
	int* sasas = (int*) _mol_malloc (sizeof (int) * nsasas);

	char* line = NULL;
	size_t len = 0;


	int i = 0;
	while (getline (&line, &len, fp) != -1)
	{
		if (i+1 > nsasas)
		{
			nsasas *= 2;
			sasas = (int*) _mol_realloc (sasas, sizeof (int) * nsasas);
		}

		if (sscanf (line, "%d", &sasas[i]) < 1)
		{
			fprintf (stderr, "error: in file %s line %s: incorrect sasa line\n", path, line);
			exit (EXIT_FAILURE);
		}
		if (sasas[i] != 0 && sasas[i] != 1)
		{
			fprintf (stderr, "error: in file %s line %s integer should be 0 or 1\n", path, line);
			exit (EXIT_FAILURE);
		}

		i++;
	}
	if (line)
		free (line);
	myfclose (fp);

	// final realloc of the arrays to make them tight
	nsasas = i;
	sasas = (int*) _mol_realloc (sasas, sizeof (int) * (nsasas+1)); // one extra for -1
	sasas[nsasas] = -1;

	return sasas;
}
示例#15
0
文件: mapfile.c 项目: meesokim/z88dk
void write_map_file( void )
{
    char *filename;
    FILE *file;
    SymbolHash *map_symtab;

    /* use first module filename to create global map file */
    filename = get_map_filename( get_first_module( NULL )->filename ); /* set '.map' extension */

    /* Create MAP file */
    file = myfopen( filename, "w" );           
	if (file)
	{
		if (opts.verbose)
			puts("Creating map...");

		/* BUG_0036, BUG_0051 */
		map_symtab = select_symbols(cond_all_symbols);

		if (SymbolHash_empty(map_symtab))
		{
			fputs("None.\n", file);
		}
		else
		{
			/* Write map symbols alphabetically */
			SymbolHash_sort(map_symtab, SymbolHash_by_name);
			write_map_syms(file, map_symtab);

			fputs("\n\n", file);

			/* Write map symbols numerically */
			SymbolHash_sort(map_symtab, SymbolHash_by_value);
			write_map_syms(file, map_symtab);
		}

		OBJ_DELETE(map_symtab);

		myfclose(file);
	}
}
示例#16
0
文件: status.cpp 项目: fishman/asfr
int savestatus(struct JOB_PARM *My_Job)
{
	int i;

	char Proxy[256], Username[128], Password[128];
	if(strlen(My_Job->Proxy)) strcpy(Proxy, My_Job->Proxy);
	else strcpy(Proxy, "null");
	if(strlen(My_Job->Username)) strcpy(Username, My_Job->Username);
	else strcpy(Username, "null");
	if(strlen(My_Job->Password)) strcpy(Password, My_Job->Password);
	else strcpy(Password, "null");

	if( (My_Job->StaFile = fopen(My_Job->StaFileName, "wb")) == NULL ) return 1;
	fprintf(My_Job->StaFile, "{%s}\n{%s}\n{%s}\n{%s}\n{%d %d %ld %ld}", My_Job->URL, Proxy, Username, Password,
		My_Job->MaxThreads, My_Job->MaxTime, My_Job->SpecOffset, My_Job->SpecLength);
	My_Job->StaDataPtr = ftell(My_Job->StaFile);

	for(i=0 ; i<My_Job->MaxThreads ; i++)
		fprintf(My_Job->StaFile, "\n{%ld %ld}", My_Job->ti[i]->SourOffset, My_Job->ti[i]->DestLength);

	myfclose( & My_Job->StaFile);
	return 0;
}
示例#17
0
struct atomgrp *read_pdb(const char *path, struct prm *prm)
{
    FILE *fp = myfopen(path, "r");

    struct atomgrp *ag =
        (struct atomgrp *)_mol_calloc(1, sizeof(struct atomgrp));

    char *line = NULL;
    size_t len = 0;

    char atypemaj[5];
    char atypemin[5];

    // read every line of the pdb file
    int atomi = 0;
    ag->natoms = 100;	// just a guess, realloc as necessary
    ag->atoms =
        (struct atom *)_mol_malloc(sizeof(struct atom) * ag->natoms);
    while (getline(&line, &len, fp) != -1) {
        if (strncmp(line, "ATOM  ", 6) != 0)	// check for ATOM line
            continue;

        if (atomi + 1 > ag->natoms) {
            ag->natoms *= 2;
            ag->atoms =
                (struct atom *)_mol_realloc(ag->atoms,
                                            sizeof(struct atom) *
                                            ag->natoms);
        }

        /*
           // init bonds
           ag->atoms[atomi].bonds[0] = -1;
           ag->atoms[atomi].bonds[1] = -1;
           ag->atoms[atomi].bonds[2] = -1;
           ag->atoms[atomi].bonds[3] = -1;
         */

        // init sa
        ag->atoms[atomi].sa = -1;
        // init mask
        ag->atoms[atomi].mask = 0;
        // init attl
        ag->atoms[atomi].attl = 0.0;
        ag->atoms[atomi].nbondis = 0;
        ag->atoms[atomi].nbonds = 0;
        ag->atoms[atomi].nangs = 0;
        ag->atoms[atomi].ntors = 0;
        ag->atoms[atomi].nimps = 0;

        if (sscanf(line, "%*s %*d %4s %4s", atypemin, atypemaj) < 2) {
            fprintf(stderr,
                    "error: in file %s line %s: incorrect atom line\n",
                    path, line);
            exit(EXIT_FAILURE);
        }

        ag->atoms[atomi].atom_typen = atomid(prm, atypemaj, atypemin);
        if (ag->atoms[atomi].atom_typen == -1)	// val not found
        {
            if (atypemin[0] == 'H')	// try one more time for hydrogen
            {
                atypemin[1] = '\0';
                ag->atoms[atomi].atom_typen =
                    atomid(prm, atypemaj, atypemin);
            }
            if (ag->atoms[atomi].atom_typen == -1)	// val still not found
            {
                fprintf(stderr,
                        "error: in file %s line %s: atom type number of %s %s not defined in prm\n",
                        path, line, atypemaj, atypemin);
                exit(EXIT_FAILURE);
            }
        }

        if (!strcmp(atypemin, "C") || !strcmp(atypemin, "CA")
                || !strcmp(atypemin, "N") || !strcmp(atypemin, "O")
                || !strcmp(atypemin, "H"))
            ag->atoms[atomi].backbone = true;
        else
            ag->atoms[atomi].backbone = false;

        sscanf(&line[30], "%8lf%8lf%8lf",
               &ag->atoms[atomi].X, &ag->atoms[atomi].Y,
               &ag->atoms[atomi].Z);
        sscanf(&line[60], "%6lf", &ag->atoms[atomi].B);

        ag->atoms[atomi].icode = line[26];
        line[26] = 0;
        errno = 0;
        ag->atoms[atomi].res_seq = atoi(&line[22]);
        if (errno) {
            perror("atoi");
            exit(EXIT_FAILURE);
        }

        ag->atoms[atomi].base = ag->atoms[atomi].base2 = -1;

        atomi++;
    }
    if (line)
        free(line);
    myfclose(fp);

    // final realloc of the arrays to make them tight
    ag->natoms = atomi;
    ag->atoms =
        (struct atom *)_mol_realloc(ag->atoms,
                                    sizeof(struct atom) * ag->natoms);

    ag->is_psf_read = false;
    ag->prm = prm;

//      check_atomgrp (ag, prm);

    return ag;
}
示例#18
0
/**
 * v4l_open_vidpipe
 *
 */
static int v4l_open_vidpipe(void)
{
    int pipe_fd = -1;
    char pipepath[255];
    char buffer[255];
    char *major;
    char *minor;
    struct utsname uts;

    if (uname(&uts) < 0) {
        MOTION_LOG(CRT, TYPE_VIDEO, SHOW_ERRNO, "%s: Unable to execute uname");
        return -1;
    }

    major = strtok(uts.release, ".");
    minor = strtok(NULL, ".");

    if ((major == NULL) || (minor == NULL) || (strcmp(major, "2"))) {
        MOTION_LOG(CRT, TYPE_VIDEO, SHOW_ERRNO, "%s: Unable to decipher OS version");
        return -1;
    }

    if (strcmp(minor, "5") < 0) {
        FILE *vloopbacks;
        char *loop;
        char *input;
        char *istatus;
        char *output;
        char *ostatus;

        vloopbacks = fopen("/proc/video/vloopback/vloopbacks", "r");

        if (!vloopbacks) {
            MOTION_LOG(CRT, TYPE_VIDEO, SHOW_ERRNO, "%s: Failed to open "
                       "'/proc/video/vloopback/vloopbacks'");
            return -1;
        }

        /* Read vloopback version*/
        if (!fgets(buffer, sizeof(buffer), vloopbacks)) {
            MOTION_LOG(CRT, TYPE_VIDEO, SHOW_ERRNO, "%s: Unable to read vloopback version");
            return -1;
        }

        fprintf(stderr, "\t%s", buffer);

        /* Read explanation line */

        if (!fgets(buffer, sizeof(buffer), vloopbacks)) {
            MOTION_LOG(CRT, TYPE_VIDEO, SHOW_ERRNO, "%s: Unable to read vloopback"
                       " explanation line");
            return -1;
        }

        while (fgets(buffer, sizeof(buffer), vloopbacks)) {
            if (strlen(buffer) > 1) {
                buffer[strlen(buffer)-1] = 0;
                loop = strtok(buffer, "\t");
                input = strtok(NULL, "\t");
                istatus = strtok(NULL, "\t");
                output = strtok(NULL, "\t");
                ostatus = strtok(NULL, "\t");

                if (istatus[0] == '-') {
                    snprintf(pipepath, sizeof(pipepath), "/dev/%s", input);
                    pipe_fd = open(pipepath, O_RDWR);

                    if (pipe_fd >= 0) {
                        MOTION_LOG(NTC, TYPE_VIDEO, NO_ERRNO, "%s: \tInput:  /dev/%s "
                                   "\tOutput: /dev/%s",  input, output);
                        break;
                    }
                }
            }
        }

        myfclose(vloopbacks);
    } else {
        DIR *dir;
        struct dirent *dirp;
        const char prefix[] = "/sys/class/video4linux/";
        char *ptr, *io;
        int fd;
        int low = 9999;
        int tfd;
        int tnum;

        if ((dir = opendir(prefix)) == NULL) {
            MOTION_LOG(CRT, TYPE_VIDEO, SHOW_ERRNO, "%s: Failed to open '%s'",
                       prefix);
            return -1;
        }

        while ((dirp = readdir(dir)) != NULL) {
            if (!strncmp(dirp->d_name, "video", 5)) {
                strncpy(buffer, prefix, sizeof(buffer));
                strncat(buffer, dirp->d_name, sizeof(buffer) - strlen(buffer));
                strncat(buffer, "/name", sizeof(buffer) - strlen(buffer));

                if ((fd = open(buffer, O_RDONLY)) >= 0) {
                    if ((read(fd, buffer, sizeof(buffer)-1)) < 0) {
                        close(fd);
                        continue;
                    }

                    ptr = strtok(buffer, " ");

                    if (strcmp(ptr, "Video")) {
                        close(fd);
                        continue;
                    }

                    major = strtok(NULL, " ");
                    minor = strtok(NULL, " ");
                    io  = strtok(NULL, " \n");

                    if (strcmp(major, "loopback") || strcmp(io, "input")) {
                        close(fd);
                        continue;
                    }

                    if ((ptr = strtok(buffer, " ")) == NULL) {
                        close(fd);
                        continue;
                    }

                    tnum = atoi(minor);

                    if (tnum < low) {
                        mystrcpy(buffer, "/dev/");
                        strncat(buffer, dirp->d_name, sizeof(buffer) - strlen(buffer));
                        if ((tfd = open(buffer, O_RDWR)) >= 0) {
                            strncpy(pipepath, buffer, sizeof(pipepath));

                            if (pipe_fd >= 0)
                                close(pipe_fd);

                            pipe_fd = tfd;
                            low = tnum;
                        }
                    }
                    close(fd);
                }
            }
        }

        closedir(dir);

        if (pipe_fd >= 0)
            MOTION_LOG(NTC, TYPE_VIDEO, NO_ERRNO, "%s: Opened %s as input",
                       pipepath);
    }

    return pipe_fd;
}
示例#19
0
void write_pdb_nopar(struct atomgrp *ag, const char *inf, const char *ouf)
{
    FILE *fop = myfopen(ouf, "w");
    fprint_pdb_nopar(fop, ag, inf);
    myfclose(fop);
}
示例#20
0
void SymbolEscapedThroughFunctionCall() {
  FILE *F = fopen("myfile.txt", "w");
  myfclose(F);
  return; // no warning
}
示例#21
0
struct atomgrp *read_pdb_nopar(const char *path)
{
    FILE *fp = myfopen(path, "r");

    struct atomgrp *ag =
        (struct atomgrp *)_mol_calloc(1, sizeof(struct atomgrp));

    char *line = NULL;
    size_t len = 0;

    // read every line of the pdb file
    int atomi = 0;
    ag->natoms = 100;	// just a guess, realloc as necessary
    ag->atoms =
        (struct atom *)_mol_malloc(sizeof(struct atom) * ag->natoms);
    while (getline(&line, &len, fp) != -1) {
        char *atom_name;
        if (strncmp(line, "ATOM  ", 6) != 0
                && strncmp(line, "HETATM", 6) != 0)
            continue;
        if (atomi + 1 > ag->natoms) {
            ag->natoms *= 2;
            ag->atoms =
                (struct atom *)_mol_realloc(ag->atoms,
                                            sizeof(struct atom) *
                                            ag->natoms);
        }
        // init bonds
        /*
                        ag->atoms[atomi].bonds[0] = -1;
                        ag->atoms[atomi].bonds[1] = -1;
                        ag->atoms[atomi].bonds[2] = -1;
                        ag->atoms[atomi].bonds[3] = -1;
        */

        // init sa
        ag->atoms[atomi].sa = -1;
        // init mask
//                ag->atoms[atomi].mask = 0;
        // init attl
//                ag->atoms[atomi].attl = 0.0;

        ag->atoms[atomi].atom_typen = 1;

        if (!strncmp(line + 13, "C ", 2)
                || !strncmp(line + 13, "CA ", 3)
                || !strncmp(line + 13, "N ", 2)
                || !strncmp(line + 13, "O ", 2)
                || !strncmp(line + 13, "H ", 2))
            ag->atoms[atomi].backbone = true;
        else
            ag->atoms[atomi].backbone = false;

        atom_name = _mol_calloc(5, sizeof(char));
        strncpy(atom_name, line + 12, 4);
        rstrip(atom_name);
        while (isspace(*atom_name)) {
            memmove(atom_name, atom_name + 1, 4);
        }
        ag->atoms[atomi].name = atom_name;

        sscanf(&line[30], "%8lf%8lf%8lf",
               &ag->atoms[atomi].X, &ag->atoms[atomi].Y,
               &ag->atoms[atomi].Z);
        sscanf(&line[60], "%6lf", &ag->atoms[atomi].B);

        ag->atoms[atomi].icode = line[26];
        line[26] = 0;
        errno = 0;
        ag->atoms[atomi].res_seq = atoi(&line[22]);
        if (errno) {
            perror("atoi");
            exit(EXIT_FAILURE);
        }

        ag->atoms[atomi].base = ag->atoms[atomi].base2 = -1;
        ag->atoms[atomi].element = NULL;

        atomi++;
    }
    if (line)
        free(line);
    myfclose(fp);

    // final realloc of the arrays to make them tight
    ag->natoms = atomi;
    ag->atoms =
        (struct atom *)_mol_realloc(ag->atoms,
                                    sizeof(struct atom) * ag->natoms);

    ag->is_psf_read = false;
    ag->prm = NULL;

    return ag;
}
示例#22
0
文件: ffmpeg.c 项目: doczii/motion
/**
 * file_close
 */
static int file_close(URLContext *h)
{
    FILE *fh = (FILE *)h->priv_data;
    return myfclose(fh);
}
struct atomgrp* read_ms_nopar (const char* path)
{
	FILE* fp = myfopen (path, "r");

	struct atomgrp* ag = (struct atomgrp*) _mol_calloc (1, sizeof (struct atomgrp));

	char* line = NULL;
	size_t len = 0;

	// read every line of the pdb file
	int atomi = 0;
	ag->natoms = 100; // just a guess, realloc as necessary
	ag->atoms = (struct atom*) _mol_malloc (sizeof (struct atom) * ag->natoms);
	while (getline (&line, &len, fp) != -1)
	{
		if (strncmp (line, "ATOM  ", 6) != 0) // check for ATOM line
			continue;

		if (atomi+1 > ag->natoms)
		{
			ag->natoms *= 2;
			ag->atoms = (struct atom*) _mol_realloc (ag->atoms, sizeof (struct atom) * ag->natoms);
		}

		/*
		// init bonds
		ag->atoms[atomi].bonds[0] = -1;
		ag->atoms[atomi].bonds[1] = -1;
		ag->atoms[atomi].bonds[2] = -1;
		ag->atoms[atomi].bonds[3] = -1;
		*/

		// init sa
		ag->atoms[atomi].sa = -1;
		// init mask
		ag->atoms[atomi].mask = 0;

        ag->atoms[atomi].atom_typen = 1;
		ag->atoms[atomi].X = atof (&line[30]);
		ag->atoms[atomi].Y = atof (&line[38]);
		ag->atoms[atomi].Z = atof (&line[46]);
		//if (line[57] != '0' && line[57] != '1') // verify that sa is 1 or 0
		if (! (line[57] >= '0' && line[57] <= '9')) // verify that sa is 1 or 0
		{
			fprintf (stderr, "error: file %s does not appear to be an ms file\n", path);
			exit (EXIT_FAILURE);
		}
		//ag->atoms[atomi].sa = atoi (&line[57]);
		ag->atoms[atomi].attl = atof (&line[57]);
		if (ag->atoms[atomi].attl > 0.0)
		{
			ag->atoms[atomi].sa = 1;
		}
		else
		{
			ag->atoms[atomi].sa = 0;
		}

		atomi++;
	}
	if (line)
		free (line);
	myfclose (fp);

	// final realloc of the arrays to make them tight
	ag->natoms = atomi;
	ag->atoms = (struct atom*) _mol_realloc (ag->atoms, sizeof (struct atom) * ag->natoms);

	return ag;
}
示例#24
0
文件: srcfile.c 项目: meesokim/z88dk
/* get the next line of input, normalize end of line termination (i.e. convert
   "\r", "\r\n" and "\n\r" to "\n"
   Calls the new_line_cb call back and returns the pointer to the null-terminated 
   text data in Str *line, including the final "\n".
   Returns NULL on end of file. */
char *SrcFile_getline( SrcFile *self )
{
    int c, c1;
    Bool found_newline;
    char *line;

    /* clear result string */
    str_clear( self->line );

    /* check for line stack */
    if ( ! List_empty( self->line_stack ) )
    {
        line = List_pop( self->line_stack );

        /* we own the string now and need to release memory */
		str_set( self->line, line );
        m_free( line );

        /* dont increment line number as we are still on same file input line */
        return str_data(self->line);
    }

    /* check for EOF condition */
    if ( self->file == NULL )
        return NULL;

    /* read characters */
    found_newline = FALSE;
    while ( ! found_newline && ( c = getc( self->file ) ) != EOF )
    {
        switch ( c )
        {
        case '\r':
        case '\n':
            c1 = getc( self->file );

            if ( ( c1 == '\r' || c1 == '\n' ) &&	/* next char also newline */
                    c1 != c )						/* "\r\n" or "\n\r" */
            {
                /* c1 will be discarded */
            }
            else								/* not composite newline - push back */
            {
                if ( c1 != EOF )
                {
                    ungetc( c1, self->file );	/* push back except EOF */
                }
            }

            /* normalize newline and fall through to default */
            found_newline = TRUE;
            c = '\n';

        default:
            str_append_char( self->line, c );
        }
    }

    /* terminate string if needed */
    if ( str_len(self->line) > 0 && ! found_newline )
        str_append_char( self->line, '\n' );

	/* signal new line, even empty one, to show end line in list */
    self->line_nr++;
	call_new_line_cb( self->filename, self->line_nr, str_data(self->line) );

	/* check for end of file
	   even if EOF found, we need to return any chars in line first */
    if ( str_len(self->line) > 0 )		
    {
        return str_data(self->line);
    }
    else
    {
        /* EOF - close file */
        myfclose( self->file );				/* close input */
        self->file = NULL;

//		call_new_line_cb( NULL, 0, NULL );
        return NULL;						/* EOF */
    }
}
void fprint_ms (struct atomgrp* ag, struct prm* prm, const char* path)
{
	FILE* fp = myfopen (path, "w");

	//struct atomgrp** ags = (struct atomgrp**) _mol_malloc (2 * sizeof (struct atomgrp*));
	struct atomgrp** ags = extract_nitrogen_residues (ag, prm);
	//ags[0] = copy_atomgrp (ag);
	//ags[1] = NULL; // flag the end with NULL

	int sum_atomi = 0;
	int agi = 0;
	while (ags[agi] != NULL)
	{
		int atomi;
		for (atomi = 0; atomi < ags[agi]->natoms; atomi++, sum_atomi++)
		{
			float attl;
            char atomname[5];
			fprintf (fp, "%-6s", "ATOM"); // atom number
			fprintf (fp, "%5d", sum_atomi+1); // atom number
			fprintf (fp, " "); // 1 space

            if (strlen(prm->atoms[ags[agi]->atoms[atomi].atom_typen].typemin) == 4) {
                strcpy(atomname, prm->atoms[ags[agi]->atoms[atomi].atom_typen].typemin);
            } else {
                atomname[0] = ' ';
                strcpy(atomname+1, prm->atoms[ags[agi]->atoms[atomi].atom_typen].typemin);
            }
			fprintf (fp, "%-4.4s", atomname); // atom typemin
			fprintf (fp, " "); // Alternate location indicator

			if (ags[agi]->atoms[atomi].mask)
			{
				fprintf (fp, "%-3.3s", "DED"); // atom typemaj
			}
			else
			{
				fprintf (fp, "%-3.3s", prm->atoms[ags[agi]->atoms[atomi].atom_typen].typemaj); // atom typemaj
			}

			fprintf (fp, " "); // 1 space
			fprintf (fp, "%1s", "A"); // chain id
			fprintf (fp, "%4d", agi+1); // residue number

			fprintf (fp, " "); // code for insertion of residues
			fprintf (fp, "   "); // 3 spaces

			fprintf (fp, "%8.3f", ags[agi]->atoms[atomi].X); // X coordinate
			fprintf (fp, "%8.3f", ags[agi]->atoms[atomi].Y); // Y coordinate
			fprintf (fp, "%8.3f", ags[agi]->atoms[atomi].Z); // Z coordinate

			fprintf (fp, "   "); // 3 spaces
			//fprintf (fp, "%1d", ags[agi]->atoms[atomi].sa); // sa boolean
			attl=0.0;
			if (ags[agi]->atoms[atomi].attl > 0.0)
			{
				attl += ags[agi]->atoms[atomi].attl;
			}
			if (ags[agi]->atoms[atomi].sa > 0)
			{
				attl += (float) ags[agi]->atoms[atomi].sa;
			}
			if (attl > 9.0)
			{
				fprintf (stderr, "warning: attraction level is > 9.0 for an atom\n");
			}
			fprintf (fp, "%.1f", attl); // attraction level

			//fprintf (fp, "%8d", ags[agi]->atoms[atomi].atom_typen); // atom type number

			fprintf (fp, "   "); // 3 spaces
			/*
			// bonding
			int bi = 0;
			while (bi < 4 && ags[agi]->atoms[atomi].bonds[bi] != -1)
			{
				fprintf (fp, "%6d", ags[agi]->atoms[atomi].bonds[bi]+1); // atom type number
				bi++;
			}
			*/

			fprintf (fp, "\n"); // new line
		}
		agi++;
	}

	//free (ags);
	free_atomgrps (ags);

	myfclose (fp);
}
struct atomgrp* read_ms (const char* path, struct prm* prm)
{
	FILE* fp = myfopen (path, "r");

	struct atomgrp* ag = (struct atomgrp*) _mol_calloc (1, sizeof (struct atomgrp));

	char* line = NULL;
	size_t len = 0;

	// tmp scanf vals
	char atypemaj[5];
	char atypemin[5];

	// read every line of the pdb file
	int atomi = 0;
	ag->natoms = 100; // just a guess, realloc as necessary
	ag->atoms = (struct atom*) _mol_malloc (sizeof (struct atom) * ag->natoms);
	while (getline (&line, &len, fp) != -1)
	{
		if (strncmp (line, "ATOM  ", 6) != 0) // check for ATOM line
			continue;

		if (atomi+1 > ag->natoms)
		{
			ag->natoms *= 2;
			ag->atoms = (struct atom*) _mol_realloc (ag->atoms, sizeof (struct atom) * ag->natoms);
		}

		/*
		// init bonds
		ag->atoms[atomi].bonds[0] = -1;
		ag->atoms[atomi].bonds[1] = -1;
		ag->atoms[atomi].bonds[2] = -1;
		ag->atoms[atomi].bonds[3] = -1;
		*/

		// init sa
		ag->atoms[atomi].sa = -1;
		// init mask
		ag->atoms[atomi].mask = 0;
		ag->atoms[atomi].nbondis = 0;
		ag->atoms[atomi].nbonds = 0;
		ag->atoms[atomi].nangs = 0;
		ag->atoms[atomi].ntors = 0;
		ag->atoms[atomi].nimps = 0;

		if (sscanf (line, "%*s %*d %4s %4s", atypemin, atypemaj) < 2)
		{
			fprintf (stderr, "begin error\n");
			fprintf (stderr, "in function read_ms\n");
			fprintf (stderr, "incorrect format for ATOM line\n");
			fprintf (stderr, "at file:\n");
			fprintf (stderr, "%s\n", path);
			fprintf (stderr, "at line:\n");
			fprintf (stderr, "%s\n", line);
			fprintf (stderr, "end error\n");
			exit (EXIT_FAILURE);
		}

		if (strncmp (atypemin, "HT", 2) == 0)
			continue; // ignore terminal hydrogens
		if (strncmp (atypemin, "OCT1", 4) == 0)
		{
			atypemin[1] = '\0';
		}
		if (strncmp (atypemin, "OCT2", 4) == 0)
			continue;


		ag->atoms[atomi].atom_typen = atomid (prm, atypemaj, atypemin);
		if (ag->atoms[atomi].atom_typen == -1) // val not found
		{
			if (atypemin[0] == 'H') // try one more time for hydrogen
			{
				atypemin[1] = '\0';
				ag->atoms[atomi].atom_typen = atomid (prm, atypemaj, atypemin);
			}
			if (ag->atoms[atomi].atom_typen == -1) // val still not found
			{
				fprintf (stderr, "error: in file %s line %s: atom type number of %s %s not defined in prm\n", path, line, atypemaj, atypemin);
				exit (EXIT_FAILURE);
			}
		}

		ag->atoms[atomi].X = atof (&line[30]);
		ag->atoms[atomi].Y = atof (&line[38]);
		ag->atoms[atomi].Z = atof (&line[46]);
		//if (line[57] != '0' && line[57] != '1') // verify that sa is 1 or 0
		if (! (line[57] >= '0' && line[57] <= '9')) // verify that sa is 1 or 0
		{
			fprintf (stderr, "error: file %s does not appear to be an ms file\n", path);
			exit (EXIT_FAILURE);
		}
		//ag->atoms[atomi].sa = atoi (&line[57]);
		ag->atoms[atomi].attl = atof (&line[57]);
		if (ag->atoms[atomi].attl > 0.0)
		{
			ag->atoms[atomi].sa = 1;
		}
		else
		{
			ag->atoms[atomi].sa = 0;
		}

		atomi++;
	}
	if (line)
		free (line);
	myfclose (fp);

	// final realloc of the arrays to make them tight
	ag->natoms = atomi;
	assert (ag->natoms > 0);
	ag->atoms = (struct atom*) _mol_realloc (ag->atoms, sizeof (struct atom) * ag->natoms);

	check_atomgrp (ag, prm);

	return ag;
}
示例#27
0
文件: mol2.cpp 项目: ampereira/F2Dock
int read_hybridization_states_from_mol2( const char* mol2file, const char* pdbfile, struct atomgrp* ag )
{
   char buffer[ linesize + 1 ];
   int mol_info_found = 0;

   FILE* fp = myfopen( mol2file, "r" );
   
   if ( fp == NULL )
     {
       print_error( "Failed to open %s!", mol2file );
       return 0;
     }
     
   while ( fgets( buffer, linesize, fp ) != NULL )
     {
       if ( strstr( buffer, "<TRIPOS>MOLECULE" ) != NULL )     
         {
           if ( fgets( buffer, linesize, fp ) == NULL )
             {
               print_error( "Failed to read %s!", mol2file );
               return 0;
             }
           
           char fname1[ linesize + 1 ], fname2[ linesize + 1 ];
           
           extract_filename( pdbfile, fname1 );
           extract_filename( buffer, fname2 );           
           
           if ( strcmp( fname1, fname2 ) )
             {
               print_error( "%s was generated from %s, not %s!", mol2file, fname2, fname1 );
               return 0;
             }
             
           if ( fgets( buffer, linesize, fp ) == NULL )
             {
               print_error( "Failed to read %s!", mol2file );
               return 0;
             } 
             
           int nat = atoi( buffer );                         
           
           if ( nat != ag->natoms )
             {
               print_error( "%s has %d atoms instead of %d atoms!", mol2file, nat, ag->natoms );
               return 0;
             }       
             
           mol_info_found = 1;       
         }

       if ( strstr( buffer, "<TRIPOS>ATOM" ) != NULL )     
         {
           if ( !mol_info_found )
             {
               print_error( "MOLECULE Record missing in %s!", mol2file );
               return 0;
             }
           
           for ( int i = 0; i < ag->natoms; i++ )
             {
               if ( fgets( buffer, linesize, fp ) == NULL )
                 {
                   print_error( "Failed to read %s!", mol2file );
                   return 0;
                 }
                 
               int atom_id;
               char atom_name[ 20 ], atom_type[ 20 ];
               double x, y, z;
               
               if ( sscanf( buffer, "%d %s %lf %lf %lf %s", &atom_id, atom_name, &x, &y, &z, atom_type ) != 6 )
                 {
                   print_error( "Failed to read ATOM record from %s!", mol2file );
                   return 0;                 
                 }  

               if ( ( x != ag->atoms[ i ].X ) || ( y != ag->atoms[ i ].Y ) || ( z != ag->atoms[ i ].Z ) )  
                 {
                   print_error( "Coordinates of atom %d in %s do not match with those in %s!", atom_id, mol2file, pdbfile );
                   return 0;                                  
                 }
                 
               ag->atoms[ i ].hybridization = UNKNOWN_HYBRID;
                              
               if ( atom_type[ 1 ] == '.' )
                 {
                   switch ( atom_type[ 2 ] )
                     {
                       case '4' : 
                       case '3' : ag->atoms[ i ].hybridization = SP3_HYBRID; break;
                       
                       case '2' : ag->atoms[ i ].hybridization = SP2_HYBRID; break;
                       
                       case '1' : ag->atoms[ i ].hybridization = SP1_HYBRID; break;
                       
                       case 'a' : if ( atom_type[ 3 ] == 'r' ) ag->atoms[ i ].hybridization = RING_HYBRID; 
                                  break;
                                  
                       case 'c' : if ( ( atom_type[ 3 ] == 'o' ) && ( atom_type[ 4 ] == '2' ) ) ag->atoms[ i ].hybridization = SP2_HYBRID; 
                                  break;                                  
                     } 
                 }
                 
//               printf( "%d %s ( %d, %d )\n", i + 1, atom_type, ag->atoms[ i ].hprop, ag->atoms[ i ].hybridization );                 
             }                
         }
     }
   
   myfclose( fp );
   
   return 1;
}
示例#28
0
/*-----------------------------------------------------------------------------
*   read/write whole code area to an open file
*----------------------------------------------------------------------------*/
void fwrite_codearea(char *filename, FILE **pbinfile, FILE **prelocfile)
{
	STR_DEFINE(new_name, FILENAME_MAX);
	Section *section;
	SectionHashElem *iter;
	int section_size;
	int cur_section_block_size;
	int cur_addr;

	init_module();

	cur_addr = -1;
	cur_section_block_size = 0;
	for (section = get_first_section(&iter); section != NULL;
		section = get_next_section(&iter))
	{
		section_size = get_section_size(section);

		if (cur_addr < 0)
			cur_addr = section->addr;

		/* bytes from this section */
		if (section_size > 0)
		{
			if (section->name && *section->name)					/* only if section name not empty */
			{
				/* change current file if address changed, or option --split-bin, or section_split */
				if ((!opts.relocatable && opts.split_bin) ||
					section->section_split ||
					cur_addr != section->addr ||
					(section != get_first_section(NULL) && section->origin >= 0))
				{
					str_set(new_name, path_remove_ext(filename));	/* "test" */
					str_append_char(new_name, '_');
					str_append(new_name, section->name);

					myfclose(*pbinfile);
					*pbinfile = myfopen(get_bin_filename(str_data(new_name)), "wb");
					if (!*pbinfile)
						break;

					if (*prelocfile) {
						myfclose(*prelocfile);
						*prelocfile = myfopen(get_reloc_filename(str_data(new_name)), "wb");
						cur_section_block_size = 0;
					}

					cur_addr = section->addr;
				}
			}

			xfput_chars(*pbinfile, (char *)ByteArray_item(section->bytes, 0), section_size);

			if (*prelocfile) {
				unsigned i;
				for (i = 0; i < intArray_size(section->reloc); i++) {
					xfput_uint16(*prelocfile, *(intArray_item(section->reloc, i)) + cur_section_block_size);
				}
			}

			cur_section_block_size += section_size;
		}

		cur_addr += section_size;
	}

	STR_DELETE(new_name);
}