item_db.pl 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/perl
  2. $db = "item_db";
  3. $nb_columns = 22;
  4. @str_col = (1,2,7,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. print("\n");
  68. sub printField {
  69. my ($str, $suffix, $idCol) = @_;
  70. # Remove first { and last }
  71. if ($str =~ /{.*}/)
  72. {
  73. $str = substr($&,1,-1);
  74. }
  75. # If nothing, put NULL
  76. if ($str eq "") {
  77. printf("NULL%s", $suffix);
  78. } else {
  79. my $flag = 0;
  80. # Search if it's a string column ?
  81. foreach $col (@str_col)
  82. {
  83. if ($col == $idCol)
  84. {
  85. $flag = 1;
  86. break;
  87. }
  88. }
  89. if ($flag == 1)
  90. {
  91. # String column, so escape and add ''
  92. printf("'%s'%s", escape($str), $suffix);
  93. } else {
  94. # Not a string column
  95. printf("%s%s", $str,$suffix);
  96. }
  97. }
  98. }
  99. sub escape {
  100. my ($str) = @_;
  101. my @str_splitted = split("'", $str);
  102. my $result = "";
  103. for (my $i=0; $i<=$#str_splitted; $i++)
  104. {
  105. if ($i == 0) {
  106. $result = @str_splitted[0];
  107. } else {
  108. $result = $result."\\'".@str_splitted[$i];
  109. }
  110. }
  111. return $result
  112. }