PFile Conversion Utility (another) [by Daniel Koepke]
Snippet Posted Wednesday, August 12th @ 11:33:07 PM, by George Greer in the Utils dept.
Added Mar 15, 1997. Click the link below to read it or download it.

From: Daniel Koepke <dkoepke@california.com>
Subject: Player file converter

Well, I had a bit of spare time earlier today and nothing to do, so I
decided to write a quick little player file converter. Note that this
isn't well tested and probably not the most flexible, but from what
I can tell, it works.

  // Convert player files (char_file_u)
  // dkoepke@california.com

  #include "../conf.h"
  #include "../sysdep.h"
  #include "../structs.h"
  #include "../utils.h"

  typedef struct old_char_file_u {
    // char_player_data
    char name[MAX_NAME_LENGTH+1];
    char description[EXDSCR_LENGTH];
    char title[MAX_TITLE_LENGTH+1];
    byte sex;
    byte race;
    byte level;
    sh_int hometown;
    time_t birth;
    int played;
    ubyte weight;
    ubyte height;

    char pwd[MAX_PWD_LENGTH+1];

    struct char_special_data_saved char_specials_saved;
    struct player_special_data_saved player_specials_saved;
    struct char_ability_data abilities;
    struct char_point_data points;
    struct affected_type affected[MAX_AFFECT];

    time_t last_logon;
    char host[HOST_LENGTH+1];
  } OldData;
  typedef struct char_file_u NewData;

  NewData ConvertData(OldData old) {
    NewData new;
    int i;

    memmove((void *) &new, (const void *) &old, sizeof(OldData));
    for (i=0;i<26;i++) new.colors[i]=7; // for testing purposes
    return (new);
  }

  void main(int argc, char **argv) {
    int recs, size = 0;
    FILE *fp, *nfp;
    char fn[128];
    OldData old;
    NewData new;

    if (argc != 2) {
      printf("convplr <playerfile>\n");
      exit(1);
    }

    sprintf(fn, "%s.new", argv[1]);

    if (!(fp = fopen(argv[1], "rb"))) {
      perror(argv[1]);
      exit(1);
    } else if (!(nfp = fopen(fn, "wb"))) {
      perror(fn);
      exit(1);
    }

    // get number of records
    fseek(fp, 0L, SEEK_END); // go to the end of the file
    size = ftell(fp); // tell us where we are (at the end, so file size)
    recs = size / sizeof(OldData);
    rewind(fp);

    if (size % sizeof(OldData)) {
      printf("Bad record count for old player file.\n");
      exit(1);
    }

    for (size=0; size<recs; size++) {
      fread(&old, sizeof(OldData), 1, fp);
      new = ConvertData(old);
      fwrite(&new, sizeof(NewData), 1, nfp);
    }

    fclose(fp);
    fclose(nfp);
    printf("Done.\n");
  }

To compile (under UNIX with gcc) type:

  gcc -g -O -o convplr convplr.c

assuming, of course, that you name it convplr.c. Also, notes on how
and when to use it:

  o Say you want to add a little thing like highly configurable
    user colors. You copy your current char_file_u into convplr.c
    as old_char_file_u (replacing the version I included if you
    need to).

  o Then you add your new data to the ****END**** of char_file_u.
    It's very important (if you couldn't guess) that you put it
    at the end of the char_file_u structure in structs.h. If you
    don't, your data will be corrupted because the conversion
    program uses memmove() to move the entire old structure into
    the new one [to explain better: say you have the numbers 1234
    and you read them in, then you add a 7 between 1 and 2, and
    convert, it'd read 17234 meaning that the 7 is now where the
    2 was, the 2 where the 3 was, the 3 where the 4 was, and the
    4 is empty; if you, however, add the 7 at the end, the memmove
    call copies 1234 into 12347, leaving 7 empty].

  o In ConvertData() after the memmove() call add code (if you
    think it necessary) to set the new values to a default. After
    the memmove() call, the new data should be zeroed out already,
    but in some cases you'll want to give the data a default value.
    For instance, when I added pagelen and pagewidth I set the
    default values to 24 and 79 respectively. In the case of my
    prompt code, where people without a prompt are given a default
    in the actual code, I wouldn't need to set a default.

  o Run 'convplr <playerfile>'; for instance, in the stock Circle
    directory, you'd do 'convplr lib/etc/players'. This will give
    you the file lib/etc/players.new

  o Move your old player file (players) to players.old and then
    move players.new to players.

  o Try to boot-up. If things don't work, you can remove the stuff
    you put in char_file_u and move players.old back to players
    to go back to how things were before.

Note that the conversion program is only really for when you want to
add something to char_file_u; changing existing stuff and deleting
old stuff won't provide the correct results. Sorry, but it was only
a 5 minute hack...:)


<< PFile Conversion Utility [by Gekke Eekhoorn] | Reply | View as text | Flattened | Player Listing Command [by Justin Robinson] >>

 


Related Links
  download
Related Articles
More by greerga
 
 

CircleMUD Snippets
 
Note: Not all of these snippets will work perfectly with your version of code, so be prepared to fix one or two bugs that may arise, and please let me know what you needed to do to fix it. Sending a corrected version is always welcome.
Finally, if you wish to use any of the snippets from this page, you are more than welcome, just mention the authors in your credits. If you wish to release any of these snippets to the public on another site, contact me FIRST.