Commit b71a6dd52e34b774d2c5570742dadf38748f7b3d

Authored by Imanol-Mikel Barba Sabariego
1 parent d1d33d9d

Added code for new errors codes and cleaned up a bit

Showing 1 changed file with 51 additions and 37 deletions
mcplayerstat.c
... ... @@ -36,53 +36,67 @@ int parseMCPlayerFile(const char* filename) {
36 36 void* data;
37 37  
38 38 ssize_t dblen = loadDB(filename, &data);
39   - if(dblen > 0) {
40   - Tag t;
41   - pos = parseTag((data + pos),&t);
42   - if(pos != dblen) {
43   - fprintf(stderr,"Didn't reach end of NBT file\n");
44   - return 4;
  39 + if(dblen < 0) {
  40 + switch(dblen) {
  41 + case ACCESS_ERROR:
  42 + fprintf(stderr,"Error calling access() on database %s\n",filename);
  43 + break;
  44 + case OPEN_ERROR:
  45 + fprintf(stderr,"Error opening database %s\n",filename);
  46 + break;
  47 + case READ_ERROR:
  48 + fprintf(stderr,"Error reading database %s\n",filename);
  49 + break;
45 50 }
  51 + return dblen;
  52 + }
  53 + Tag t;
  54 + pos = parseTag((data + pos),&t);
  55 + if(pos < 0) {
  56 + fprintf(stderr,"Error parsing root tag: code %d\n",pos);
  57 + return pos;
  58 + } else if(pos != dblen) {
  59 + fprintf(stderr,"Didn't reach end of NBT file\n");
  60 + return 2;
  61 + }
46 62  
47   - TagCompound root = *(TagCompound*) t.payload;
48   - for(int i = 0; i < root.numTags; ++i) {
49   - Tag node = root.list[i];
50   - if(node.type == TAG_INT) {
51   - if(!strncmp(node.name,"playerGameType",node.nameLength)) {
52   - int playerGameType = *((uint32_t*)node.payload);
53   - printf("playerGameType: %s\n",(playerGameType == 0 ? "Survival" : (playerGameType == 1 ? "Creative" : (playerGameType == 2 ? "Adventure" : (playerGameType == 3 ? "Spectator" : "UNKNOWN")))));
54   - } else if (!strncmp(node.name,"XpLevel",node.nameLength)) {
55   - int xpLevel = *((uint32_t*)node.payload);
56   - printf("XpLevel: %u\n",xpLevel);
57   - }
58   - } else if(node.type == TAG_LIST) {
59   - TagList* l = (TagList*)node.payload;
60   - if(!strncmp(node.name,"Inventory",node.nameLength)) {
61   - printf("Inventory:\n");
62   - printInventory(l);
63   - } else if (!strncmp(node.name,"EnderItems",node.nameLength)) {
64   - printf("Ender Inventory:\n");
65   - printInventory(l);
66   - } else if (!strncmp(node.name,"Pos",node.nameLength)) {
67   - double x = (*(double*)l->list[0].payload);
68   - double y = (*(double*)l->list[1].payload);
69   - double z = (*(double*)l->list[2].payload);
70   - printf("Pos: (x: %f,y: %f,z: %f)\n",x,y,z);
71   - }
  63 + TagCompound root = *(TagCompound*) t.payload;
  64 + for(int i = 0; i < root.numTags; ++i) {
  65 + Tag node = root.list[i];
  66 + if(node.type == TAG_INT) {
  67 + if(!strncmp(node.name,"playerGameType",node.nameLength)) {
  68 + int playerGameType = *((uint32_t*)node.payload);
  69 + printf("playerGameType: %s\n",(playerGameType == 0 ? "Survival" : (playerGameType == 1 ? "Creative" : (playerGameType == 2 ? "Adventure" : (playerGameType == 3 ? "Spectator" : "UNKNOWN")))));
  70 + } else if (!strncmp(node.name,"XpLevel",node.nameLength)) {
  71 + int xpLevel = *((uint32_t*)node.payload);
  72 + printf("XpLevel: %u\n",xpLevel);
  73 + }
  74 + } else if(node.type == TAG_LIST) {
  75 + TagList* l = (TagList*)node.payload;
  76 + if(!strncmp(node.name,"Inventory",node.nameLength)) {
  77 + printf("Inventory:\n");
  78 + printInventory(l);
  79 + } else if (!strncmp(node.name,"EnderItems",node.nameLength)) {
  80 + printf("Ender Inventory:\n");
  81 + printInventory(l);
  82 + } else if (!strncmp(node.name,"Pos",node.nameLength)) {
  83 + double x = (*(double*)l->list[0].payload);
  84 + double y = (*(double*)l->list[1].payload);
  85 + double z = (*(double*)l->list[2].payload);
  86 + printf("Pos: (x: %f,y: %f,z: %f)\n",x,y,z);
72 87 }
73 88 }
74   -
75   - destroyTag(&t);
76   - free(data);
77   - return 0;
78 89 }
79   - return -1;
  90 +
  91 + destroyTag(&t);
  92 + free(data);
  93 + return 0;
80 94 }
81 95  
82 96 int main(int argc, char** argv) {
83 97 if(argc != 2) {
84 98 fprintf(stderr,"No file specified\n");
85   - return 1;
  99 + return 2;
86 100 }
87 101  
88 102 return parseMCPlayerFile(argv[1]);
... ...