Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
SNBTLib - debug.c

debug.c

Caricato da: ZioCrocifisso
Scarica il programma completo

  1. /*
  2.  * Copyright (c) 2013, ZioCrocifisso
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions are met:
  7.  *
  8.  * 1. Redistributions of source code must retain the above copyright notice, this
  9.  *    list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright notice,
  11.  *    this list of conditions and the following disclaimer in the documentation
  12.  *    and/or other materials provided with the distribution.
  13.  *
  14.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17.  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  18.  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  21.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24.  */
  25.  
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include "nbt.h"
  30.  
  31. static const char *typenames[] = {
  32.         "End",
  33.         "Byte",
  34.         "Short",
  35.         "Int",
  36.         "Long",
  37.         "Float",
  38.         "Double",
  39.         "Byte Array",
  40.         "String",
  41.         "List",
  42.         "Compound",
  43.         "Int Array",
  44.         "Tag",
  45.         "?"
  46. };
  47.  
  48. static int printval(nbt_tag *tag, nbt_byte type, int depth);
  49. static int printmul(nbt_tag *tag, int depth);
  50. static int printtag(nbt_tag *tag, int depth);
  51.  
  52. static int printval(nbt_tag *tag, nbt_byte type, int depth)
  53. {
  54.         if (NBT_IS_INTEGER(type)) {
  55.                 printf("%ld\n", nbt_get_integer(tag));
  56.         } else if (NBT_IS_REAL(type)) {
  57.                 printf("%f\n", nbt_get_real(tag));
  58.         } else if (NBT_IS_STRING(type)) {
  59.                 printf("%s\n", nbt_get_string(tag));
  60.         } else if (NBT_IS_MULTIPLE(type)) {
  61.                 return printmul(tag, depth);
  62.         } else if (NBT_IS_TAG(type)) {
  63.                 return printtag(nbt_get_tag(tag), depth);
  64.         } else {
  65.                 return NBT_ERR_INVALID_TYPE;
  66.         }
  67.  
  68.         return 1;
  69. }
  70.  
  71. static int printmul(nbt_tag *tag, int depth)
  72. {
  73.         uint64_t length = nbt_get_length(tag);
  74.         uint64_t i;
  75.         char *depthchars = (char *) malloc(depth + 2);
  76.         nbt_byte ctype = nbt_get_children_type(tag);
  77.         nbt_tag muls;
  78.  
  79.         memset(depthchars, '-', depth + 1);
  80.         depthchars[depth + 1] = 0;
  81.  
  82.         printf("(%ss)\n", nbt_type_name(nbt_get_children_type(tag)));
  83.  
  84.         for (i = 0; i < length; i++) {
  85.                 printf("%s", depthchars);
  86.                 if (NBT_IS_MULTIPLE(ctype)) {
  87.                         muls = nbt_get_multiple(tag);
  88.                         printmul(&muls, depth + 1);
  89.                 } else {
  90.                         printval(tag, ctype, depth + 1);
  91.                 }
  92.         }
  93.  
  94.         free(depthchars);
  95.  
  96.         return 1;
  97. }
  98.  
  99. static int printtag(nbt_tag *tag, int depth)
  100. {
  101.         printf("[%s] %s: ", nbt_type_name(tag->id), nbt_get_name(tag));
  102.         return printval(tag, tag->id, depth);
  103. }
  104.  
  105. int nbt_print(nbt_tag *tag)
  106. {
  107.         if (!tag) {
  108.                 return NBT_ERR_INVALID_ARG;
  109.         }
  110.  
  111.         return printtag(tag, 0);
  112. }
  113.  
  114. char *nbt_type_name(nbt_byte id) {
  115.         if (id == NBT_TAG_TAG) {
  116.                 id = 12;
  117.         } else if (id < NBT_TAG_END || id > NBT_TAG_INT_ARRAY) {
  118.                 id = 13;
  119.         }
  120.  
  121.         return typenames[id];
  122. }