]> git.somenet.org - irc/pjirc-ng.git/blob - src/main/java/irc/StringParser.java
Pjirc 2.2.1 as available on the net, reformatted and made it compile.
[irc/pjirc-ng.git] / src / main / java / irc / StringParser.java
1 /*****************************************************/\r
2 /*          This java file is a part of the          */\r
3 /*                                                   */\r
4 /*           -  Plouf's Java IRC Client  -           */\r
5 /*                                                   */\r
6 /*   Copyright (C)  2002 - 2004 Philippe Detournay   */\r
7 /*                                                   */\r
8 /*         All contacts : theplouf@yahoo.com         */\r
9 /*                                                   */\r
10 /*  PJIRC is free software; you can redistribute     */\r
11 /*  it and/or modify it under the terms of the GNU   */\r
12 /*  General Public License as published by the       */\r
13 /*  Free Software Foundation; version 2 or later of  */\r
14 /*  the License.                                     */\r
15 /*                                                   */\r
16 /*  PJIRC is distributed in the hope that it will    */\r
17 /*  be useful, but WITHOUT ANY WARRANTY; without     */\r
18 /*  even the implied warranty of MERCHANTABILITY or  */\r
19 /*  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU   */\r
20 /*  General Public License for more details.         */\r
21 /*                                                   */\r
22 /*  You should have received a copy of the GNU       */\r
23 /*  General Public License along with PJIRC; if      */\r
24 /*  not, write to the Free Software Foundation,      */\r
25 /*  Inc., 59 Temple Place, Suite 330, Boston,        */\r
26 /*  MA  02111-1307  USA                              */\r
27 /*                                                   */\r
28 /*****************************************************/\r
29 \r
30 package irc;\r
31 \r
32 import java.util.Vector;\r
33 \r
34 /**\r
35  * String parser.\r
36  */\r
37 public class StringParser {\r
38         /**\r
39          * Trim the given string, without removing non-printable characters.\r
40          * \r
41          * @param t\r
42          *          string to trim.\r
43          * @return trimmed string.\r
44          */\r
45         public static String trim(String t) {\r
46                 int a = 0;\r
47                 while ((a < t.length()) && (t.charAt(a) == ' '))\r
48                         a++;\r
49                 if (a == t.length())\r
50                         return "";\r
51                 int b = t.length() - 1;\r
52                 while ((b >= 0) && (t.charAt(b) == ' '))\r
53                         b--;\r
54                 if (b < 0)\r
55                         return "";\r
56                 return t.substring(a, b + 1);\r
57         }\r
58 \r
59         private int indexOf(String s, char toFind) {\r
60                 int deep = 0;\r
61                 for (int i = 0; i < s.length(); i++) {\r
62                         char c = s.charAt(i);\r
63                         if ((deep == 0) && (c == toFind))\r
64                                 return i;\r
65                         if (c == '"')\r
66                                 deep = 1 - deep;\r
67                         if (c == '\'')\r
68                                 deep = 1 - deep;\r
69                 }\r
70                 return -1;\r
71         }\r
72 \r
73         /**\r
74          * Parse the string.\r
75          * \r
76          * @param line\r
77          *          string to parse.\r
78          * @return arrays of strings.\r
79          */\r
80         public String[] parseString(String line) {\r
81                 Vector res = new Vector();\r
82                 while (line.length() != 0) {\r
83                         int pos = indexOf(line, ' ');\r
84                         if (pos == -1) {\r
85                                 res.insertElementAt(line, res.size());\r
86                                 line = "";\r
87                         } else {\r
88                                 String part = trim(line.substring(0, pos));\r
89                                 line = trim(line.substring(pos));\r
90                                 res.insertElementAt(part, res.size());\r
91                         }\r
92                 }\r
93 \r
94                 String[] param = new String[res.size()];\r
95                 for (int i = 0; i < res.size(); i++)\r
96                         param[i] = (String) res.elementAt(i);\r
97                 return param;\r
98 \r
99         }\r
100 \r
101 }\r