item_db.pl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/perl
  2. $db = "item_db";
  3. $nb_columns = 22;
  4. @str_col = (1,2,19,20,21);
  5. $line_format = "([^\,]*),([^\,]*),([^\,]*),([^\,]*),([^\,]*),([^\,]*),([^\,]*),([^\,]*),([^\,]*),([^\,]*),([^\,]*),([^\,]*),([^\,]*),([^\,]*),([^\,]*),([^\,]*),([^\,]*),([^\,]*),([^\,]*),(\{.*\}),(\{.*\}),(\{.*\})";
  6. #$line_format = ;
  7. $create_table = "#
  8. # Table structure for table `item_db`
  9. #
  10. DROP TABLE IF EXISTS `item_db`;
  11. CREATE TABLE `item_db` (
  12. `id` smallint(5) unsigned NOT NULL default '0',
  13. `name_english` varchar(50) NOT NULL default '',
  14. `name_japanese` varchar(50) NOT NULL default '',
  15. `type` tinyint(2) unsigned NOT NULL default '0',
  16. `price_buy` mediumint(10) unsigned default NULL,
  17. `price_sell` mediumint(10) unsigned default NULL,
  18. `weight` smallint(5) unsigned NOT NULL default '0',
  19. `attack` smallint(3) unsigned default NULL,
  20. `defence` tinyint(3) unsigned default NULL,
  21. `range` tinyint(2) unsigned default NULL,
  22. `slots` tinyint(2) unsigned default NULL,
  23. `equip_jobs` int(12) unsigned default NULL,
  24. `equip_upper` tinyint(8) unsigned default NULL,
  25. `equip_genders` tinyint(2) unsigned default NULL,
  26. `equip_locations` smallint(4) unsigned default NULL,
  27. `weapon_level` tinyint(2) unsigned default NULL,
  28. `equip_level` tinyint(3) unsigned default NULL,
  29. `refineable` tinyint(1) unsigned default NULL,
  30. `view` smallint(3) unsigned default NULL,
  31. `script` text,
  32. `equip_script` text,
  33. `unequip_script` text,
  34. PRIMARY KEY (`id`)
  35. ) ENGINE=MyISAM;
  36. ";
  37. printf("%s\n",$create_table);
  38. while ($ligne=<STDIN>)
  39. {
  40. if ($ligne =~ /[^\r\n]+/)
  41. {
  42. $ligne = $&;
  43. if ($ligne =~ /^\/\//)
  44. {
  45. printf("# ");
  46. $ligne = substr($ligne, 2);
  47. }
  48. if ($ligne =~ $line_format) {
  49. @champ = ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22);
  50. } else {
  51. @champ = ();
  52. }
  53. if ($#champ != $nb_columns - 1)
  54. {
  55. # Can't parse, it's a real comment
  56. printf ("%s\n", $ligne);
  57. } else {
  58. printf("REPLACE INTO `%s` VALUES (", $db);
  59. for ($i=0; $i<$#champ; $i++)
  60. {
  61. printField($champ[$i],",",$i);
  62. }
  63. printField($champ[$#champ],");\n",$#champ);
  64. }
  65. }
  66. }
  67. sub printField {
  68. my ($str, $suffix, $idCol) = @_;
  69. # Remove first { and last }
  70. if ($str =~ /{.*}/)
  71. {
  72. $str = substr($&,1,-1);
  73. }
  74. # If nothing, put NULL
  75. if ($str eq "") {
  76. printf("NULL%s", $suffix);
  77. } else {
  78. my $flag = 0;
  79. # Search if it's a string column ?
  80. foreach $col (@str_col)
  81. {
  82. if ($col == $idCol)
  83. {
  84. $flag = 1;
  85. break;
  86. }
  87. }
  88. if ($flag == 1)
  89. {
  90. # String column, so escape and add ''
  91. printf("'%s'%s", escape($str), $suffix);
  92. } else {
  93. # Not a string column
  94. printf("%s%s", $str,$suffix);
  95. }
  96. }
  97. }
  98. sub escape {
  99. my ($str) = @_;
  100. my @str_splitted = split("'", $str);
  101. my $result = "";
  102. for (my $i=0; $i<=$#str_splitted; $i++)
  103. {
  104. if ($i == 0) {
  105. $result = @str_splitted[0];
  106. } else {
  107. $result = $result."\\'".@str_splitted[$i];
  108. }
  109. }
  110. return $result
  111. }