package Sight.Structures;
import Sight.io.Output;
import java.io.*;
public class Strings implements Serializable {
public String L[];
int step;
public int length;
int Top;
public Strings() { this(10,10); };
public Strings(int initialSize, int growBy)
{
int a = initialSize;
int b = growBy;
L = new String[a+1];
step = b;
Top = a+1;
length = 0;
};
public Strings(String []A)
{ int k;
length = A.length;
L = new String[length+5];
step = 5;
Top = length+5;
System.arraycopy(A,0,L,0,A.length);
};
public Strings(Strings G)
{
this(G.L);
length = G.length;
step = G.step;
};
public boolean has(String N)
{ if (N==null) return false;
int k;
if (length>0)
for (k=0;k<length;k++)
{
if (L[k] == null) break;
if (L[k].compareTo(N)==0) return true;
};
return false;
};
public boolean has(Strings list)
{ int k;
if (length>0)
for (k=0;k<length;k++)
if (list.has(L[k])) return true;
return false;
};
public int indexOf(String N)
{ int k;
if (length>0)
for (k=0;k<length;k++)
if (L[k].compareTo(N)==0) return k;
return -1;
};
public void Clear()
{ length = 0; }
public String at(int x) throws ArrayIndexOutOfBoundsException
{
if (x>=length) return null;
return L[x];
};
public String get(int x) throws ArrayIndexOutOfBoundsException
{
if (x>=length) return null;
return L[x];
};
public void Sort()
{ int k, l; String n;
for (k=0; k<length; k++)
for (l=k+1; l<length; l++)
if (L[k].compareTo(L[l])>0) { n = L[k]; L[k] = L[l]; L[l]=n; };
};
public void add(String N)
{ if (N==null) return; length++;
if (length>Top)
{ String nL[];
nL = new String[Top+step];
System.arraycopy(L,0,nL,0,length-1);
L = nL;
Top = Top+step;
step = (step*3)/2;
};
L[length-1] = N;
};
public void addExc(String N)
{ if (has(N)) return;
if (N==null) return; add(N);
};
public void add(String [] a)
{ if (a==null) return;
ensureCapacity(length+a.length);
for (int i = 0; i < a.length; i++) {
add(a[i]);
}
};
public void addExc(String [] a)
{ if (a==null) return;
ensureCapacity(length+a.length);
for (int i = 0; i < a.length; i++) {
addExc(a[i]);
}
};
public void ensureCapacity(int size)
{
if (size>L.length)
{ String nL[];
nL = new String[size];
if (length>0)
System.arraycopy(L,0,nL,0,length);
L = nL;
Top = L.length;
};
};
public int scan(String N, int p) throws Exception
{ int j = _scan(N, p);
if (j<0) throw new Exception(N+" nf.");
return j;
};
public int _scan(String N, int p)
{
if (length>p)
for (int k=p;k<length;k++)
{ if (L[k].indexOf(N)>=0) return k;
};
return -1;
};
public int _scan(String N)
{
return _scan(N,0);
};
public int _scan_ic(String N, int p)
{ N = N.toLowerCase();
if (length>p)
for (int k=p;k<length;k++)
{ if (L[k].toLowerCase().indexOf(N)>=0) return k;
};
return -1;
};
public void remove(Strings what)
{ for (int k=0;k<what.length;k++)
{ if (has(what.at(k))) remove(what.at(k));
};
};
public void remove(String n)
{ int nn;
int k, l;
nn = -1;
for (k = 0 ; k < length ; k++)
if (L[k]==n)
{ nn=k; break; };
if (nn<0) return; if (nn!=length-1)
for (k = nn+1 ; k < length ; k++)
L[k-1]=L[k];
length--;
};
public void setElementAt(String N, int p)
{ L[p] = N;
};
public void dump()
{
dump(System.out);
};
public void dump(PrintStream f)
{ int k;
for (k=0;k<length;k++)
f.println(Integer.toString(k)+" "+at(k)+"\r");
}
public void dump(PrintStream f, int how)
{ int k;
if (how == Strings.LINENUM) dump(f);
else if (how == Strings.HTML)
{ f.println("<MENU>");
for (k=0;k<length;k++)
f.println("<LI>"+at(k));
f.println("</MENU>");
}
else
for (k=0;k<length;k++)
f.println(at(k)+"\r");
}
public void add(Strings x)
{if (x!=null)
if (x.length>0)
{ ensureCapacity(length+x.length);
for (int k=0;k<x.length;k++)
{ add(x.L[k]);
};
};
};
public void addExc(Strings x)
{ if (x!=null)
if (x.length>0)
{
{ ensureCapacity(length+x.length);
for (int k=0;k<x.length;k++)
{ if (!has(x.L[k]))
add(x.L[k]);
};
};
};
};
public void _x_add(Strings x)
{if (x!=null)
if (x.length>0)
{
{
for (int k=0;k<x.length;k++)
{ add(x.at(k));
};
};
};
};
void _x_addExc(Strings x)
{ if (x!=null)
if (x.length>0)
{
for (int k=0;k<x.length;k++)
{ if (!has(x.at(k)))
add(x.at(k));
};
};
};
public void save(String file)
{ try {
String comment = callStack();
FileOutputStream fs = new FileOutputStream(file);
PrintStream f = new PrintStream(fs);
f.println(comment);
dump(f);
f.close();
fs.close();
} catch (IOException ioex)
{ System.out.println(ioex.getMessage()); };
};
public void save(String file, int how)
{ try {
String comment = callStack();
FileOutputStream fs = new FileOutputStream(file);
PrintStream f = new PrintStream(fs);
f.println(comment);
dump(f, how);
f.close();
fs.close();
} catch (IOException ioex)
{ System.out.println(ioex.getMessage()); };
};
public Strings(String file)
{ this();
File ft = new File(file);
if (!ft.exists())
{ System.err.println(file+" not found. ");
};
try {
FileInputStream f = new FileInputStream(file);
BufferedReader in = new BufferedReader(
new InputStreamReader(f));
String str;
int np;
while ((str = in.readLine()) != null) {
str = str.trim();
if (str.startsWith("//")) continue; np = str.indexOf(" "); if (str.length()>0) { str = str.substring(np+1).trim();
add(str);
};
}
} catch (IOException ioex)
{ System.out.println(ioex.getMessage()); };
};
public static int PLAIN = 1;
public static int LINENUM = 2;
public static int HTML = 3;
public Strings(String file, int how)
{ this();
File ft = new File(file);
if (!ft.exists())
{ System.out.println(file+" not found! ");
};
try {
FileInputStream f = new FileInputStream(file);
BufferedReader in = new BufferedReader(
new InputStreamReader(f));
String str;
int np;
while ((str = in.readLine()) != null) {
str = str.replace('\r',' ');
str = str.replace('\n',' ');
str = str.trim();
if (str.length()==0) continue; if (str.startsWith("//")) continue; if (how==LINENUM)
{
np = str.indexOf(" "); if (str.length()>0) add(str.substring(np+1).trim());
} else add(str);
}
} catch (IOException ioex)
{ System.out.println(ioex.getMessage()); };
};
public String toString()
{ StringBuffer b = new StringBuffer(599);
for (int k=0;k<length;k++)
{ b.append(at(k));
b.append(", ");
};
return b.toString();
};
private String callStack()
{
String Stack = "";
int pp;
try {
StringWriter sw = new StringWriter();
new Throwable().printStackTrace(
new PrintWriter( sw )
);
Stack = sw.toString().replace('\n',' ').replace('\r',' ');
} catch (Exception exc) {}; return Stack;
};
public String[] toStringArray()
{
String [] r = new String[length];
System.arraycopy(L,0,r,0,length);
return r;
};
static public void main(String[] args) {
Strings s = new Strings(2,2);
for (int i = 0; i < 10; i++) {
s.add("item "+i);
s.add("item "+i);
}
s.dump();
Strings single = new Strings();
single.addExc(s);
System.out.println("Single");
single.dump();
}
}