nesdev.com
http://forums.nesdev.com/

yy-chr: set file size
http://forums.nesdev.com/viewtopic.php?f=21&t=14529
Page 1 of 1

Author:  EnigmaOverdrive [ Wed Jul 13, 2016 11:22 am ]
Post subject:  yy-chr: set file size

I noticed that some chr files are different sizes (some are 8kb, some are 2kb...) and I need mine to be 1kb in size, but whenever I create a new file in YY-CHR it's 8KB large by default. How can I change this?

Author:  thefox [ Wed Jul 13, 2016 11:50 am ]
Post subject:  Re: yy-chr: set file size

AFAIK it's not possible. You can edit smaller files though (you can use a hex editor like HxD to create a file full of zeros). Caution: If you make changes in areas that don't fall within the original size of the file, the changes won't be saved.

Author:  EnigmaOverdrive [ Wed Jul 13, 2016 12:28 pm ]
Post subject:  Re: yy-chr: set file size

That works, thanks!

Author:  Light-Dark [ Fri Aug 12, 2016 11:54 am ]
Post subject:  Re: yy-chr: set file size

Because Tokumaru's tile compressor takes a max of 4kb of tile data I wrote a simple C program that carves up the standard 8kb chr file for myself. Here's an example program for you that fits your needs:

Code:
#include <stdio.h>
#include <stdlib.h>
char* extractKB(char* indata,int size){
   char* cut = malloc(size);
   
   memcpy(cut,indata,size);   //copy first kb
   
   return cut;
}

int getFileSize(FILE* fp){
   int count = 0;
   while(fgetc(fp)!=EOF)
      count++;
   fseek(fp,0,0);
   return count;
}


int main(int argc,char** argv) {
   if(argc < 3)
      return 1;
   FILE* fp = fopen(argv[1],"rb");
   int size = getFileSize(fp);
   char* buffer=  malloc(size);
   fread(buffer,1,size,fp);
   fclose(fp);
   
   fp = fopen(argv[2],"wb");
   char* extracted = extractKB(buffer,1024);
   
   fwrite(extracted,1,1024,fp);
   
   fclose(fp);
   
   free(buffer);
   free(extracted);
   return 0;
}


It takes two command line args, the chr file and then the output file.

Author:  koitsu [ Fri Aug 12, 2016 1:46 pm ]
Post subject:  Re: yy-chr: set file size

You can't be serious with that getFileSize() routine, are you? You don't need the while/fgetc at all. Try:

Code:
long getFileSize(FILE *fp) {
  long count;

  fseek(fp, 0, SEEK_END);
  /* TODO handle when ftell() returns -1 (error; errno is set) */
  count = ftell(fp);
  fseek(fp, 0, SEEK_SET);

  return count;
}

Note that ftell() returns long, not int, so other portions of the program will need to be updated. Also, fread() takes a size_t for its size and member counts, so you might have to force-cast (no guarantee size_t is a long on every arch).

I'm intentionally choosing not to comment on the rest. :-)

Author:  thefox [ Fri Aug 12, 2016 2:56 pm ]
Post subject:  Re: yy-chr: set file size

Python is quite a bit more practical for tasks like that:
Code:
import sys; open( sys.argv[2], "wb" ).write( open( sys.argv[1], "rb" ).read( 1024 ) )
(That's the whole program.)

On *nix you could achieve the same thing with dd.

Author:  Light-Dark [ Sat Aug 13, 2016 12:32 pm ]
Post subject:  Re: yy-chr: set file size

koitsu wrote:
You can't be serious with that getFileSize() routine, are you? You don't need the while/fgetc at all. Try:

Code:
long getFileSize(FILE *fp) {
  long count;

  fseek(fp, 0, SEEK_END);
  /* TODO handle when ftell() returns -1 (error; errno is set) */
  count = ftell(fp);
  fseek(fp, 0, SEEK_SET);

  return count;
}

Note that ftell() returns long, not int, so other portions of the program will need to be updated. Also, fread() takes a size_t for its size and member counts, so you might have to force-cast (no guarantee size_t is a long on every arch).

I'm intentionally choosing not to comment on the rest. :-)


That's okay, critique is welcome. I'm certainly not the Da Vinci of C code :lol: (slapped it together on the way home from work).

Author:  koitsu [ Sat Aug 13, 2016 12:42 pm ]
Post subject:  Re: yy-chr: set file size

thefox wrote:
On *nix you could achieve the same thing with dd.

There is dd for Windows (which also implements /dev/null, /dev/zero, and /dev/random "devices" for convenience). I've used this for a long time with great success.

Page 1 of 1 All times are UTC - 7 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/