Ejemplo n.º 1
0
static int decode_C1(void)
{
	int	secoff,tmpoff,c;

	tmpoff = fgetc(fin);
	if( tmpoff == EOF)
	{
		show_file_error( fin );
		return 0;
	}

	c=tmpoff;
	for (secoff=0; secoff<=tmpoff; secoff++) 
	{
		buf[c]=fgetc(fin);
		c--;
		if( feof(fin) )
		{
			show_file_error( fin );
			return 0;
		}
	}
	if( !write_sector(fout) )
		return 0;
	return 1;
}
Ejemplo n.º 2
0
gboolean save_selected_color (gchar *file) {
	FILE *fp;
	size_t filesize1;
	long filesize2;
	gchar old[512] = "";

	colorname = g_strdup (gtk_entry_get_text (GTK_ENTRY (save_entry)));
	if (!strcmp (colorname, ""))
		return FALSE;

	/* if file exists already, get its contents, otherwise just write to it */
	if (g_file_test (file, G_FILE_TEST_EXISTS)) {
		fp = fopen (file, "r");
		filesize2 = ftell (fp);
		if (fp) {
			filesize1 = fread (old, sizeof (old), 1, fp);
			fclose (fp);
			if(filesize1 != filesize2)
				show_file_error(file, _("read"));
		} else {
			show_file_error (file, _("read"));
			return FALSE;
		}
	}

	fp = fopen (file, "w");
	if (!fp) {
		show_file_error (file, _("write"));
		return FALSE;
	} else {
		g_fprintf (fp, "%3d %3d %3d\t\t%s\n%s", colorvalue.red/256, colorvalue.green/256, colorvalue.blue/256, colorname, old);
		fclose (fp);
		return TRUE;
	}
}
Ejemplo n.º 3
0
static int decode_C3(void)
{
	int	secoff,tmpoff,c;
	
	secoff=0;
	do 
	{
		if (secoff)
			tmpoff = read_offset(fin);
		else
			tmpoff = fgetc(fin);

		if( tmpoff == EOF )
		{
			show_file_error( fin );
			return 0;
		}

		for (; secoff<tmpoff; secoff++) 
		{
			buf[secoff] = fgetc(fin);
			if( feof(fin) )
			{
				show_file_error( fin );
				return 0;
			}

		}
		if (secoff == (int)secsize)
			break;

		tmpoff = read_offset(fin);
		c = fgetc(fin);
		if( tmpoff == EOF || c == EOF )
		{
			show_file_error( fin );
			return 0;
		}

		for (; secoff<tmpoff; secoff++) 
		{
			buf[secoff] = c;
		}
	} while(secoff < (int)secsize);

	if( !write_sector(fout) )
		return 0;
	return 1;
}
Ejemplo n.º 4
0
gboolean delete_color (gchar *file, gchar *color_name, gchar *color_value) {
	FILE *fp;
	gchar *p, *file_color_name = NULL, file_color_value[8], buffer[512] = "", newstuff[512] = "";
	gint r, g, b;
	gboolean found = FALSE;

	/* remove from file */
	fp = fopen (file, "r");
	if (!fp) {
		show_file_error (file, _("read"));
		return FALSE;
	} else {
		while ((p = fgets (buffer, sizeof buffer, fp)) != NULL) {
			if (buffer[0] == '!')
				continue;
			r = g_ascii_strtoull (p, &p, 10);
			g = g_ascii_strtoull (p, &p, 10);
			b = g_ascii_strtoull (p, &p, 10);
			p += strspn (p, " \t");
			g_sprintf (file_color_value, "#%.2X%.2X%.2X", r, g, b);
			file_color_name = g_strchomp (g_strdup (p));

			/* make sure to only remove the first matching color; both value and name must match */
			if (found || strcmp (file_color_name, color_name) != 0 || strcmp (g_ascii_strup (file_color_value, -1), color_value) != 0)
				g_sprintf (newstuff+strlen(newstuff), "%3d %3d %3d\t\t%s\n", r, g, b, file_color_name);
			else
				found = TRUE;
		}
		g_free(p);
		g_free(file_color_name);
		fclose (fp);

		/* only rewrite the file if we found a match */
		if (found) {
			fp = fopen (file, "w");
			if (!fp) {
				show_file_error (file, _("write"));
				return FALSE;
			} else {
				g_fprintf (fp, "%s", newstuff);
				fclose (fp);
				return TRUE;
			}
		}
		return FALSE;
	}
}
Ejemplo n.º 5
0
/**
 * Function that returns the number of players in a file "filename"
 * The players are return into a vector of Player object
 *
 * filename - the file we'll be searching for players objects
*/
vector<Player> Files::get_all_players_file(const char *filename)
{
	ifstream fin;
	string line;
	vector<Player> players;

	//Variable that holds the size of a puzzle
	int puzzle_size = get_puzzle_file_size(filename);

	//Puzzle not found
	if(puzzle_size == 0)
	{
		//Display an error message
		show_file_error(filename);
	}

	if(file_exists(filename))
	{
		try{
			//Open the file
			fin.open(filename);

			if(fin.is_open())
			{
				//We can ignore the puzzle_size + 1 lines
				//Example:
				//1 2 3 
				//4 5 6
				//7 8 0
				//
				//Andre .......
				//Here we ignore 3 + 1 lines, so the pointer of the file is now in the players
				int ignore_lines = puzzle_size + 1;

				for(int i = 0; i < ignore_lines; i++)
					fin.ignore(INT_MAX, '\n');

				//Read till the end of the file
				while(!fin.eof())
				{
					getline(fin, line);
					
					//If the line is not empty, add the player to the vector
					//Else ignore that line
					if(!line.empty())
						players.push_back(str_utils.extract_player_data(line));
				}
			}

			//Close the file
			fin.close();
		}catch(ifstream::failure ex) {
			//Should not happen
			cerr << "[!] Ocorreu um erro. Ficheiro " << filename << endl;
		}
	}

	return players;
}
Ejemplo n.º 6
0
imImage* read_file(const char* filename)
{
  int error;
  imImage* image = imFileImageLoadBitmap(filename, 0, &error);
  if (error) 
    show_file_error(error);
  return image;
}
Ejemplo n.º 7
0
gboolean save_selected_color ()
{
	GtkEntry *entry;
	FILE     *fp;
	gchar     old[512] = "";
	
	/* get entry text */
	entry = GTK_ENTRY (lookup_widget (savedialog, "save_entry"));
	colorname = g_strdup (gtk_entry_get_text (entry));
	
	if (!strcmp (colorname, ""))
		return FALSE;

	/* save color in user file - write at top of the file */
	
	/* if file exists already, get its contents, otherwise just write to it */
	if (g_file_test (get_user_file (), G_FILE_TEST_EXISTS))
	{
		fp = fopen (get_user_file (), "r");
		if (fp)
		{
			fread (old, sizeof (old), 1, fp);
			fclose (fp);
		}
		else
		{
			show_file_error (FILE_READ);
			return FALSE;
		}
	}
	
	fp = fopen (get_user_file (), "w");
	if (!fp)
	{
		show_file_error (FILE_WRITE);
		return FALSE;
	}
	
	fprintf (fp, "%3d %3d %3d\t\t%s\n%s", colorvalue.red/256, colorvalue.green/256,
	         colorvalue.blue/256, colorname, old);
	fclose (fp);
	return TRUE;
}
Ejemplo n.º 8
0
int write_file(const char* filename, const imImage* image)
{
  const char* format = imImageGetAttribString(image, "FileFormat");
  int error = imFileImageSave(filename, format, image);
  if (error)
  {
    show_file_error(error);
    return 0;
  }
  return 1;
}
Ejemplo n.º 9
0
static int decode_C4(void)
{
	int	secoff,tmpoff;

	tmpoff = read_offset(fin);
	if( tmpoff == EOF )
	{
		show_file_error( fin );
		return 0;
	}

	for (secoff=tmpoff; secoff<(int)secsize; secoff++) {
		buf[secoff]=fgetc(fin);
		if( feof(fin) )
		{
			show_file_error( fin );
			return 0;
		}
	}
	if( !write_sector(fout) )
		return 0;
	return 1;
}
Ejemplo n.º 10
0
imImage* read_file(const char* filename)
{
  int error;
  imImage* image = imFileImageLoadBitmap(filename, 0, &error);
  if (error) 
    show_file_error(error);
  else
  {
    /* we are going to support only RGB images with no alpha */
    imImageRemoveAlpha(image);
    if (image->color_space != IM_RGB)
    {
      imImage* new_image = imImageCreateBased(image, -1, -1, IM_RGB, -1);
      imConvertColorSpace(image, new_image);
      imImageDestroy(image);

      image = new_image;
    }

    /* create OpenGL compatible data */
    imImageGetOpenGLData(image, NULL);
  }
  return image;
}
Ejemplo n.º 11
0
/**
 * Deletes the first matching line from the rgb user file
 * that matches the color name and color value specified.
 *
 * color_value is given in hex format
 */
gboolean delete_color (gchar* color_name, gchar* color_value)
{
	FILE     *fp;
	gchar    *p;
	gchar    *file_color_name;
	gchar     file_color_value[8];
	gint      r, g, b;
	gchar     buffer[512] = "";
	gchar     newstuff[512] = "";
	gboolean  found = FALSE;

	
	/* remove from file */
	fp = fopen (get_user_file (), "r");
	if (!fp)
	{
		show_file_error (FILE_READ);
		return FALSE;
	}

	while ((p = fgets (buffer, sizeof buffer, fp)) != NULL)
	{
		if (buffer[0] == '!')
			continue;
		r = g_ascii_strtoull (p, &p, 10);
		g = g_ascii_strtoull (p, &p, 10);
		b = g_ascii_strtoull (p, &p, 10);
		p += strspn (p, " \t");
		g_sprintf (file_color_value, "#%2X%2X%2X", r, g, b);
		file_color_name = g_strchomp (g_strdup (p));
		
		/* make sure to only remove the first matching color. both value and
		   name must match */
		if (found || strcmp (file_color_name, color_name) != 0 ||
		    strcmp (g_ascii_strup (file_color_value, -1), color_value) != 0)
		{
			g_sprintf (newstuff, "%s%3d %3d %3d\t\t%s\n", newstuff, r, g, b, file_color_name);
		}
		else
		{
			found = TRUE;
		}
	}
	fclose (fp);
	
	/* only rewrite the file if we found a match */
	if (found)
	{
		fp = fopen (get_user_file (), "w");
		if (!fp)
		{
			show_file_error (FILE_WRITE);
			return FALSE;
		}
		
		fprintf (fp, "%s", newstuff);
		fclose (fp);
		return TRUE;
	}
	return FALSE;
}
Ejemplo n.º 12
0
static int decode_FA(void)
{
	unsigned char c;

	if (working) 
	{
		Aprint("Trying to start section but last section never had an end section block.");
		return 0;
	}
	c=fgetc(fin);
	if( feof(fin) )
	{
		show_file_error( fin );
		return 0;
	}
	density=((c & 0x70) >> 4);
	last=((c & 0x80) >> 7);
	switch(density) 
	{
		case 0:
			maxsec=720;
			secsize=128;
			break;
		case 2:
			maxsec=720;
			secsize=256;
			break;
		case 4:
			maxsec=1040;
			secsize=128;
			break;
		default:
			Aprint( "Density type is unknown, density type=%u",density);
			return 0;
	}

	if (createdisk == 0) {
		createdisk = 1;
		/* write out atr header */
		/* special code, 0x0296 */
		if( write_atari16(fout,0x296) == 0 )
			return 0;
		/* image size (low) */
		if( write_atari16(fout,(short)(((long)maxsec * secsize) >> 4)) == 0 )
			return 0;
		/* sector size */
		if( write_atari16(fout,secsize) == 0 )
			return 0;
		/* image size (high) */
		if( write_atari16(fout,(short)(((long)maxsec * secsize) >> 20)) == 0 )
			return 0;
		/* 8 bytes unused */
		if( write_atari16(fout,0) == 0 )
			return 0;
		if( write_atari16(fout,0) == 0 )
			return 0;
		if( write_atari16(fout,0) == 0 )
			return 0;
		if( write_atari16(fout,0) == 0 )
			return 0;
		memset(buf,0,256);
		for (cursec=0; cursec<maxsec; cursec++) {
			if( fwrite(buf,secsize,1,fout) != 1 )
			{
				Aprint( "Error writing to output file" );
				return 0;
			}
		}
	}
Ejemplo n.º 13
0
int dcmtoatr(FILE *fin, FILE *fout, const char *input, char *output )
{
	int archivetype;	/* Block type for first block */
	int blocktype;		/* Current block type */
	int tmp;			/* Temporary for read without clobber on eof */

	init_globals( fin, fout );
	Aprint( "Converting %s to %s", input, output );
	if( !fin || !fout )
	{
		Aprint( "Programming error - NULL file specified for conversion" );
		return 0;
	}
	archivetype = blocktype = fgetc(fin);

	if( archivetype == EOF )
	{
		show_file_error( fin );
		return 0;
	}

	switch(blocktype) 
	{
		case 0xF9:
		case 0xFA:
			break;
		default:
			Aprint("0x%02X is not a known header block at start of input file",blocktype);
			return 0;
	}
	
	rewind(fin);
	
	while( 1 )
	{
		if (feof(fin)) 
		{
			fflush(stdout); /* Possible buffered I/O confusion fix */
			if ((!last) && (blocktype == 0x45) && (archivetype == 0xF9)) {
				Aprint("Multi-part archive error.");
				Aprint("To process these files, you must first combine the files into a single file.");
				Aprint("COPY /B file1.dcm+file2.dcm+file3.dcm newfile.dcm from the DOS prompt");
			}
			else 
			{
				Aprint("EOF before end block, input file likely corrupt");
			}
			return 0;
		}
		
		if (working) {
			if (soffset() != ftell(fout)) 
			{
				Aprint("Output desyncronized, possibly corrupt dcm file. fin=%lu fout=%lu != %lu cursec=%u secsize=%u", 
					ftell(fin),ftell(fout),soffset(),cursec,secsize);
				return 0;
			}
		}
		
		tmp = fgetc(fin); /* blocktype is needed on EOF error--don't corrupt it */
		if( tmp == EOF )
		{
			show_file_error( fin );
			return 0;
		}

		blocktype = tmp;
		switch(blocktype) 
		{
		      case 0xF9:
			  case 0xFA:
				  /* New block */
				  if( decode_FA() == 0 )
					  return 0;
				  break;
			  case 0x45:
				  /* End block */
				  working=0;
				  if (last)
					  return 1;	/* Normal exit */
				  break;
			  case 0x41:
			  case 0xC1:
				  if( decode_C1() == 0 )
					  return 0;
				  break;
			  case 0x43:
			  case 0xC3:
				  if( decode_C3() == 0 )
					  return 0;
				  break;
			  case 0x44:
			  case 0xC4:
				  if( decode_C4() == 0 )
					  return 0;
				  break;
			  case 0x46:
			  case 0xC6:
				  if( decode_C6() == 0 )
					  return 0;
				  break;
			  case 0x47:
			  case 0xC7:
				  if( decode_C7() == 0 )
					  return 0;
				  break;
			  default:
				  Aprint("0x%02X is not a known block type.  File may be corrupt.",blocktype);
				  return 0;
		} /* end case */
		
		if ((blocktype != 0x45) && (blocktype != 0xFA) && (blocktype != 0xF9)) 
		{
			if (!(blocktype & 0x80)) 
			{
				cursec=read_atari16(fin);
				if( fseek(fout, soffset(), SEEK_SET) != 0 )
				{
					Aprint( "Failed a seek in output file, cannot continue" );
					return 0;
				}
			} 
			else 
			{
				cursec++;
				if(cursec==4 && secsize!=128)
					fseek(fout,(secsize-128)*3,SEEK_CUR);
			}
		}
	} 	
	return 0; /* Should never be executed */
}