From b71a6dd52e34b774d2c5570742dadf38748f7b3d Mon Sep 17 00:00:00 2001 From: Imanol-Mikel Barba Sabariego Date: Mon, 1 Jun 2020 00:23:49 +0100 Subject: [PATCH] Added code for new errors codes and cleaned up a bit --- mcplayerstat.c | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------- 1 file changed, 51 insertions(+), 37 deletions(-) diff --git a/mcplayerstat.c b/mcplayerstat.c index 2f02ff2..ca566e2 100644 --- a/mcplayerstat.c +++ b/mcplayerstat.c @@ -36,53 +36,67 @@ int parseMCPlayerFile(const char* filename) { void* data; ssize_t dblen = loadDB(filename, &data); - if(dblen > 0) { - Tag t; - pos = parseTag((data + pos),&t); - if(pos != dblen) { - fprintf(stderr,"Didn't reach end of NBT file\n"); - return 4; + if(dblen < 0) { + switch(dblen) { + case ACCESS_ERROR: + fprintf(stderr,"Error calling access() on database %s\n",filename); + break; + case OPEN_ERROR: + fprintf(stderr,"Error opening database %s\n",filename); + break; + case READ_ERROR: + fprintf(stderr,"Error reading database %s\n",filename); + break; } + return dblen; + } + Tag t; + pos = parseTag((data + pos),&t); + if(pos < 0) { + fprintf(stderr,"Error parsing root tag: code %d\n",pos); + return pos; + } else if(pos != dblen) { + fprintf(stderr,"Didn't reach end of NBT file\n"); + return 2; + } - TagCompound root = *(TagCompound*) t.payload; - for(int i = 0; i < root.numTags; ++i) { - Tag node = root.list[i]; - if(node.type == TAG_INT) { - if(!strncmp(node.name,"playerGameType",node.nameLength)) { - int playerGameType = *((uint32_t*)node.payload); - printf("playerGameType: %s\n",(playerGameType == 0 ? "Survival" : (playerGameType == 1 ? "Creative" : (playerGameType == 2 ? "Adventure" : (playerGameType == 3 ? "Spectator" : "UNKNOWN"))))); - } else if (!strncmp(node.name,"XpLevel",node.nameLength)) { - int xpLevel = *((uint32_t*)node.payload); - printf("XpLevel: %u\n",xpLevel); - } - } else if(node.type == TAG_LIST) { - TagList* l = (TagList*)node.payload; - if(!strncmp(node.name,"Inventory",node.nameLength)) { - printf("Inventory:\n"); - printInventory(l); - } else if (!strncmp(node.name,"EnderItems",node.nameLength)) { - printf("Ender Inventory:\n"); - printInventory(l); - } else if (!strncmp(node.name,"Pos",node.nameLength)) { - double x = (*(double*)l->list[0].payload); - double y = (*(double*)l->list[1].payload); - double z = (*(double*)l->list[2].payload); - printf("Pos: (x: %f,y: %f,z: %f)\n",x,y,z); - } + TagCompound root = *(TagCompound*) t.payload; + for(int i = 0; i < root.numTags; ++i) { + Tag node = root.list[i]; + if(node.type == TAG_INT) { + if(!strncmp(node.name,"playerGameType",node.nameLength)) { + int playerGameType = *((uint32_t*)node.payload); + printf("playerGameType: %s\n",(playerGameType == 0 ? "Survival" : (playerGameType == 1 ? "Creative" : (playerGameType == 2 ? "Adventure" : (playerGameType == 3 ? "Spectator" : "UNKNOWN"))))); + } else if (!strncmp(node.name,"XpLevel",node.nameLength)) { + int xpLevel = *((uint32_t*)node.payload); + printf("XpLevel: %u\n",xpLevel); + } + } else if(node.type == TAG_LIST) { + TagList* l = (TagList*)node.payload; + if(!strncmp(node.name,"Inventory",node.nameLength)) { + printf("Inventory:\n"); + printInventory(l); + } else if (!strncmp(node.name,"EnderItems",node.nameLength)) { + printf("Ender Inventory:\n"); + printInventory(l); + } else if (!strncmp(node.name,"Pos",node.nameLength)) { + double x = (*(double*)l->list[0].payload); + double y = (*(double*)l->list[1].payload); + double z = (*(double*)l->list[2].payload); + printf("Pos: (x: %f,y: %f,z: %f)\n",x,y,z); } } - - destroyTag(&t); - free(data); - return 0; } - return -1; + + destroyTag(&t); + free(data); + return 0; } int main(int argc, char** argv) { if(argc != 2) { fprintf(stderr,"No file specified\n"); - return 1; + return 2; } return parseMCPlayerFile(argv[1]); -- libgit2 0.22.2