root/tools/item_db.pl @ 13

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