/*
* Created on Sep 30, 2005
* Created by omschaub
*
*/
package omschaub.stuffer.main;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import omschaub.stuffer.containers.ClientBlock;
import omschaub.stuffer.containers.ClientBlockSet;
import omschaub.stuffer.containers.SetUtils;
import omschaub.stuffer.containers.Table1Container;
import omschaub.stuffer.containers.Table2Container;
public class FileUtilities {
//private static String[][] clientList;
/* *//**
* Main code to pull the clientList, while choosing whether to
* re-read it or not
*
* @param boolean readConfigFile
* @return String[][] clientList
*//*
public static String[][] getClientList(boolean readConfigFile){
if(readConfigFile){
return readClientList();
}else{
return clientList;
}
}
*/
/**
* Internal Code to read the client list from the config file
*
* @return String[][] clientList
*/
public static void readClientList(){
//Initiate the clientList String
//clientList = new String[200][7];
//Pull the config file
File configFile = getConfigFile();
//Read the file
BufferedReader input = null;
try {
input = new BufferedReader ( new FileReader(configFile));
String line;
int counter = 0;
while (( line = input.readLine()) != null){
//parse the new line here
//get the correct indeces off of the line
int rule_index = line.indexOf("|rules|") + 7;
int client_index = line.indexOf("|client|");
//the Container
ClientBlock cb = new ClientBlock();
//write the results to the array
//[0] is the number of the rule
// clientList[counter][0] = line.substring(0,client_index);
cb.setIndex(Integer.parseInt(line.substring(0,client_index)));
//[1] is the client's name
//clientList[counter][1] = line.substring(client_index+8,rule_index-7);
cb.setClientName(line.substring(client_index+8,rule_index-7));
//[2] isRegEx
//clientList[counter][2] = line.substring(rule_index,rule_index + 1);
cb.set_isRegEx(line.substring(rule_index,rule_index + 1).equalsIgnoreCase("0") ? false : true);
//[3] isDownload
//clientList[counter][3] = line.substring(rule_index+1, rule_index+2);
cb.set_isDownloading(line.substring(rule_index+1, rule_index+2).equalsIgnoreCase("0") ? false : true);
//[4] isUploading
//clientList[counter][4] = line.substring(rule_index+2, rule_index+3);
cb.set_isUploading(line.substring(rule_index+2, rule_index+3).equalsIgnoreCase("0") ? false : true);
//[5] isSuperSeeding
//clientList[counter][5] = line.substring(rule_index+3, rule_index+4);
cb.set_isSuperseeding(line.substring(rule_index+3, rule_index+4).equalsIgnoreCase("0") ? false : true);
//[6] is the color (not yet in readable format)
//clientList[counter][6] = line.substring(rule_index+4, line.length());
cb.setColor(line.substring(rule_index+4, line.length()));
if(!Plugin.clientBlock_set.contains(cb))
Plugin.clientBlock_set.addToSet(cb);
//increse the counter 1
counter++;
}
try {
if (input !=null){
input.close();
}
}catch (IOException ex){
ex.printStackTrace();
}
}catch (IOException ex){
ex.printStackTrace();
}
//return clientList;
}
/**
* Writes one client rule to the clientfile
* @param name
* @param isRegEx
* @param isDownloading
* @param isUploading
* @param isSuperseeding
* @param color in r000g000b000 format
*/
public static void writeNewClient(final String name,
final String isRegEx,
final String isDownloading,
final String isUploading,
final String isSuperseeding,
final String color){
//Pull the config file
File configFile = getConfigFile();
//set up the writer
try{
int total_num = getNumberofRules(); //will be the right number due to the 0 in the array
//System.out.println("Hers the num: |" + total_num);
String index_to_write = String.valueOf(total_num);
if(index_to_write.startsWith("0") && !index_to_write.equalsIgnoreCase("0"))
index_to_write = index_to_write.substring(1, index_to_write.length());
String toWrite = index_to_write + "|client|" + name + "|rules|" +
isRegEx + isDownloading + isUploading + isSuperseeding + color;
BufferedWriter bufWriter = new BufferedWriter(new FileWriter(configFile,true));
bufWriter.write(toWrite);
bufWriter.newLine();
bufWriter.close();
readClientList();
}catch(Exception e){
e.printStackTrace();
}
}
/**
* gets the number of rules in the rule file
* @return int of total number of rules in the file
*/
public static int getNumberofRules(){
/*int counter = 0;
try{
//pull the file
File configFile = getConfigFile();
//Read all of the lines of the file and replace the necessary one
BufferedReader in = new BufferedReader(new FileReader(configFile));
while((in.readLine()) != null){
counter++;
}
in.close();
return counter;
}catch (Exception e){
e.printStackTrace();
return 0;
}*/
return Plugin.clientBlock_set.getSize();
}
/**
* Finds, removes and rewrites the given client at oldName for the newName
* @param String number
* @param String newName
*
*/
public static void changeClientName(final String number, final String newName){
try{
ClientBlock cb = Plugin.clientBlock_set.getClientBlockByIndex(Integer.parseInt(number));
cb.setClientName(newName);
writeClientBlockSetToFile(Plugin.clientBlock_set);
reNumberFile();
readClientList();
/*//get the old rule set
//pull the file
File configFile = getConfigFile();
//Read all of the lines of the file and replace the necessary one
String inputLine;
String[] tempStor = new String[200];
int counter = 0;
String[] oldRules = getRules(number);
BufferedReader in = new BufferedReader(new FileReader(configFile));
while((inputLine = in.readLine()) != null){
String temp_line = inputLine.substring(0,inputLine.indexOf("|client|"));
if(temp_line.equalsIgnoreCase(number)){
String toWrite = number + "|client|" + newName + "|rules|" +
oldRules[0] + oldRules[1] + oldRules[2] +
oldRules[3] + oldRules[4];
tempStor[counter] = toWrite;
}else{
tempStor[counter] = inputLine;
}
counter++;
}
//close the reader
in.close();
//no need to purge the file as the writer will be set to NOT append
BufferedWriter bufWriter = new BufferedWriter(new FileWriter(configFile,false));
for(int i = 0; i < tempStor.length; i++){
if(tempStor[i] != null){
bufWriter.write(tempStor[i]);
bufWriter.newLine();
}
}
bufWriter.close();
readClientList();*/
}catch(Exception e){
e.printStackTrace();
}
}
/**
* Finds, removes and rewrites the given client's rule set for the new one
* @param String number
* @param String clientName
* @param String isRegEx
* @param String isDownloading
* @param String isUploading
* @param String isSuperSeeding
* @param String color in r000g000b000 format
*/
public static void changeClientRules(final String numberString,
final String clientName,
final String isRegEx,
final String isDownloading,
final String isUploading,
final String isSuperseeding,
final String color){
try{
//get ClientBlock
ClientBlock cb = Plugin.clientBlock_set.getClientBlockByIndex(Integer.valueOf(numberString).intValue());
//Replace Rules
cb.setClientName(clientName);
cb.set_isRegEx(isRegEx.equalsIgnoreCase("0") ? false : true);
cb.set_isDownloading(isDownloading.equalsIgnoreCase("0") ? false : true);
cb.set_isUploading(isUploading.equalsIgnoreCase("0") ? false : true );
cb.set_isSuperseeding(isSuperseeding.equalsIgnoreCase("0") ? false : true);
cb.setColor(color);
writeClientBlockSetToFile(Plugin.clientBlock_set);
readClientList();
//get the old rule set
//String[][] holder = getClientList(true);
//replace the rules at the given number
/*int number = Integer.valueOf(numberString).intValue();
holder[number][1] = clientName;
holder[number][2] = isRegEx;
holder[number][3] = isDownloading;
holder[number][4] = isUploading;
holder[number][5] = isSuperseeding;
holder[number][6] = color;
//pull the file
File configFile = getConfigFile();
//no need to purge the file as the writer will be set to NOT append
BufferedWriter bufWriter = new BufferedWriter(new FileWriter(configFile,false));
for(int i = 0; i < holder.length; i++){
if(holder[i][0] != null){
bufWriter.write(i + "|client|" + holder[i][1] + "|rules|" +
holder[i][2] + holder[i][3] + holder[i][4] + holder[i][5] + holder[i][6]);
bufWriter.newLine();
}
}
bufWriter.close();
readClientList();*/
}catch(Exception e){
e.printStackTrace();
}
}
/**
* getsRule set for given client
* @param String number
* @return String[] ruleSet
*/
public static String[] getRules(final String number){
String[] ruleSet = new String[6];
try{
ClientBlock cb = Plugin.clientBlock_set.getClientBlockByIndex(Integer.parseInt(number));
ruleSet[0] = (cb.get_isRegEx() ? "1" : "0");
ruleSet[1] = (cb.get_isDownloading() ? "1" : "0");
ruleSet[2] = (cb.get_isUploading() ? "1" : "0");
ruleSet[3] = (cb.get_isSuperseeding() ? "1" : "0");
ruleSet[4] = cb.getColor();
/*
String[][] holder = getClientList(true);
for(int i = 0; i < holder.length; i++){
if(holder[i][0] != null){
if(holder[i][0].equalsIgnoreCase(number)){
ruleSet[0] = holder[i][2];
ruleSet[1] = holder[i][3];
ruleSet[2] = holder[i][4];
ruleSet[3] = holder[i][5];
ruleSet[4] = holder[i][6];
}
}
}
*/
}catch(Exception e){
e.printStackTrace();
}
return ruleSet;
}
public static void writeClientBlockSetToFile(ClientBlockSet cbs){
try{
//no need to purge the file as the writer will be set to NOT append
File configFile = getConfigFile();
BufferedWriter bufWriter = new BufferedWriter(new FileWriter(configFile,false));
Iterator it = cbs.getIterator();
while (it.hasNext()){
//Pull the current ClientBlock
ClientBlock cb = (ClientBlock)it.next();
//Write the rule
bufWriter.write(cb.getIndex() +
"|client|" +
cb.getClientName() +
"|rules|" +
(cb.get_isRegEx() ? "1" : "0") +
(cb.get_isDownloading() ? "1" : "0") +
(cb.get_isUploading() ? "1" : "0") +
(cb.get_isSuperseeding() ? "1" : "0") +
cb.getColor());
bufWriter.newLine();
}
bufWriter.close();
}catch(Exception e){
e.printStackTrace();
}
}
/**
* Delete a client and all of its rules
* @param numberToDelete
*/
public static void deleteClient(final String numberToDelete){
Thread delete_filter = new Thread() {
public void run() {
try{
Plugin.clientBlock_set.removeFromSet(Plugin.clientBlock_set.getClientBlockByIndex(Integer.parseInt(numberToDelete)));
writeClientBlockSetToFile(Plugin.clientBlock_set);
reNumberFile();
readClientList();
/*//pull the entire rule set making sure to re-read the file
String[][] holder = getClientList(true);
//no need to purge the file as the writer will be set to NOT append
File configFile = getConfigFile();
BufferedWriter bufWriter = new BufferedWriter(new FileWriter(configFile,false));
for(int i = 0; i < holder.length; i++){
if(holder[i][0] != null){
if(!holder[i][0].equalsIgnoreCase(numberToDelete)){
bufWriter.write(i + "|client|" + holder[i][1] + "|rules|" +
holder[i][2] + holder[i][3] + holder[i][4] + holder[i][5] + holder[i][6]);
bufWriter.newLine();
}
}
}
bufWriter.close();
reNumberFile();
readClientList();*/
}catch(Exception e){
e.printStackTrace();
}
}
};
delete_filter.setDaemon(true);
delete_filter.run();
}
/**
* Delete a client and all of its rules
* @param numberToDelete
*/
public static void deleteMultipleClients(final String[] numbersToDelete){
Thread delete_filter = new Thread() {
public void run() {
try{
for(int i = 0; i < numbersToDelete.length; i++){
Plugin.clientBlock_set.removeFromSet(Plugin.clientBlock_set.getClientBlockByIndex(Integer.parseInt(numbersToDelete[i])));
}
writeClientBlockSetToFile(Plugin.clientBlock_set);
reNumberFile();
readClientList();
/*
//pull the entire rule set making sure to re-read the file
String[][] holder = getClientList(true);
//no need to purge the file as the writer will be set to NOT append
File configFile = getConfigFile();
BufferedWriter bufWriter = new BufferedWriter(new FileWriter(configFile,false));
for(int j = 0; j < holder.length; j++){
for(int i = 0; i < numbersToDelete.length; i++){
if(holder[j][0] != null){
if(holder[j][0].equalsIgnoreCase(numbersToDelete[i])){
holder[j][0] = null;
holder[j][1] = null;
holder[j][2] = null;
holder[j][3] = null;
holder[j][4] = null;
holder[j][5] = null;
holder[j][6] = null;
}
}
}
}
for(int j = 0; j < holder.length; j++){
if(holder[j][0] != null){
bufWriter.write(j + "|client|" + holder[j][1] + "|rules|" +
holder[j][2] + holder[j][3] + holder[j][4] + holder[j][5] + holder[j][6]);
bufWriter.newLine();
}
}
bufWriter.close();
reNumberFile();
readClientList();
*/
}catch(Exception e){
e.printStackTrace();
}
}
};
delete_filter.setDaemon(true);
delete_filter.run();
}
private static void reNumberFile(){
//pull the entire rule set making sure to re-read the file
//String[][] holder = getClientList(true);
try{
int i = 0;
Iterator it = Plugin.clientBlock_set.getIterator();
while(it.hasNext()){
ClientBlock cb = (ClientBlock)it.next();
cb.setIndex(i);
i++;
}
writeClientBlockSetToFile(Plugin.clientBlock_set);
/*
File configFile = getConfigFile();
//no need to purge the file as the writer will be set to NOT append
BufferedWriter bufWriter = new BufferedWriter(new FileWriter(configFile,false));
for(int i = 0; i < holder.length; i++){
if(holder[i][0] != null){
bufWriter.write(i + "|client|" + holder[i][1] + "|rules|" +
holder[i][2] + holder[i][3] + holder[i][4] + holder[i][5] + holder[i][6]);
bufWriter.newLine();
}
}
bufWriter.close();
readClientList();
*/
}catch(Exception e){
e.printStackTrace();
}
}
/**
* Move a rule up or down in the file
* @param ruleNum (String of the number to move)
* @param up (boolean)
*/
public static boolean moveRule(String ruleNum, boolean up, boolean down, boolean top, boolean bottom){
try{
int index = Integer.parseInt(ruleNum) - 1;
ClientBlock cb_to_move = Plugin.clientBlock_set.getClientBlockByIndex(index);
if(top){
if(index == 0) return false; // already at top
Plugin.clientBlock_set.removeFromSet(cb_to_move);
Iterator it = Plugin.clientBlock_set.getIterator();
while(it.hasNext()){
ClientBlock cb = (ClientBlock)it.next();
cb.setIndex(cb.getIndex() + 1);
}
cb_to_move.setIndex(0);
Plugin.clientBlock_set.addToSet(cb_to_move);
writeClientBlockSetToFile(Plugin.clientBlock_set);
reNumberFile();
readClientList();
return true;
}else if(up){
if(index == 0) return false; // already at top
ClientBlock cb_bumped = Plugin.clientBlock_set.getClientBlockByIndex(index -1);
Plugin.clientBlock_set.removeFromSet(cb_bumped);
cb_bumped.setIndex(index);
cb_to_move.setIndex(index-1);
Plugin.clientBlock_set.addToSet(cb_bumped);
writeClientBlockSetToFile(Plugin.clientBlock_set);
reNumberFile();
readClientList();
return true;
}else if(down){
int size = Plugin.clientBlock_set.getSize();
if(index+1 >= size) return false; //already at bottom
ClientBlock cb_bumped = Plugin.clientBlock_set.getClientBlockByIndex(index + 1);
Plugin.clientBlock_set.removeFromSet(cb_bumped);
cb_bumped.setIndex(index);
cb_to_move.setIndex(index+1);
Plugin.clientBlock_set.addToSet(cb_bumped);
writeClientBlockSetToFile(Plugin.clientBlock_set);
reNumberFile();
readClientList();
return true;
}else if(bottom){
int size = Plugin.clientBlock_set.getSize();
if(index+1 >= size) return false; //already at bottom
Plugin.clientBlock_set.removeFromSet(cb_to_move);
cb_to_move.setIndex(size + 1);
Plugin.clientBlock_set.addToSet(cb_to_move);
writeClientBlockSetToFile(Plugin.clientBlock_set);
reNumberFile();
readClientList();
return true;
}
/* //pull the entire rule set making sure to re-read the file
String[][] holder = getClientList(true);
try{
File configFile = getConfigFile();
int ruleNum_Old = Integer.valueOf(ruleNum).intValue();
int ruleNum_New;
if(top){
if(ruleNum_Old == 1){
//rule already at top
return false;
}
String[] rule_ToMove = holder[ruleNum_Old-1];
//no need to purge the file as the writer will be set to NOT append
BufferedWriter bufWriter = new BufferedWriter(new FileWriter(configFile,false));
bufWriter.write(0 + "|client|" + rule_ToMove[1] + "|rules|" +
rule_ToMove[2] + rule_ToMove[3] + rule_ToMove[4] + rule_ToMove[5] + rule_ToMove[6]);
bufWriter.newLine();
for(int i = 0; i < holder.length; i++){
if(holder[i][0] != null && i != (ruleNum_Old-1)){
bufWriter.write(i+1 + "|client|" + holder[i][1] + "|rules|" +
holder[i][2] + holder[i][3] + holder[i][4] + holder[i][5] + holder[i][6]);
if(i != holder.length)
bufWriter.newLine();
}
}
bufWriter.close();
reNumberFile();
readClientList();
return true;
}else if(bottom){
if(ruleNum_Old >= getNumberofRules())
//rule was the bottom rule
return false;
String[] rule_ToMove = holder[ruleNum_Old-1];
//no need to purge the file as the writer will be set to NOT append
BufferedWriter bufWriter = new BufferedWriter(new FileWriter(configFile,false));
for(int i = 0; i < holder.length; i++){
if(holder[i][0] != null && i != (ruleNum_Old-1)){
bufWriter.write(i+1 + "|client|" + holder[i][1] + "|rules|" +
holder[i][2] + holder[i][3] + holder[i][4] + holder[i][5] + holder[i][6]);
bufWriter.newLine();
}
}
bufWriter.write(0 + "|client|" + rule_ToMove[1] + "|rules|" +
rule_ToMove[2] + rule_ToMove[3] + rule_ToMove[4] + rule_ToMove[5] + rule_ToMove[6]);
bufWriter.close();
reNumberFile();
readClientList();
return true;
}else if(up){
ruleNum_New = ruleNum_Old - 1;
}else{
ruleNum_New = ruleNum_Old + 1;
}
if(ruleNum_New <= 0 || ruleNum_New > getNumberofRules() ){
//we are at the top or bottom already..
return false;
}
String[] rule_Temp = holder[ruleNum_New-1];
String[] rule_ToMove = holder[ruleNum_Old-1];
holder[ruleNum_New - 1] = rule_ToMove;
holder[ruleNum_Old - 1] = rule_Temp;
//no need to purge the file as the writer will be set to NOT append
BufferedWriter bufWriter = new BufferedWriter(new FileWriter(configFile,false));
for(int i = 0; i < holder.length; i++){
if(holder[i][0] != null){
bufWriter.write(i + "|client|" + holder[i][1] + "|rules|" +
holder[i][2] + holder[i][3] + holder[i][4] + holder[i][5] + holder[i][6]);
bufWriter.newLine();
}
}
bufWriter.close();
readClientList();*/
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}
}
/**
* Internal routine to get the config file
* @return configFile
*/
private static File getConfigFile(){
File configFile = null;
try{
//Setup the file
String userPath = Plugin.getPluginInterface().getPluginDirectoryName();
configFile = new File(userPath, "clientlist.config" );
//if the file does not exist, make sure to create it before we try to read it
if (!configFile.isFile()) {
try {
configFile.createNewFile();
}catch (IOException e) {
e.printStackTrace();
}
}
}catch(Exception e){
e.printStackTrace();
}
return configFile;
}
/**
* Utility to write the log file
* @param String[] toWrite
* @param File logFile
* @param boolean append (true to append to the file, false to overwrite it)
*/
public static void writeToLog(final SetUtils toWrite, final File logFile, final boolean append){
if(toWrite == null)
return;
//set up the writer
try{
BufferedWriter bufWriter = new BufferedWriter(new FileWriter(logFile, append));
Iterator it = toWrite.getIterator();
while(it.hasNext()){
Table1Container container = (Table1Container)it.next();
String[] containerString = container.getTableItemsAsString();
for(int i = 0; i < containerString.length; i++){
bufWriter.write(containerString[i] + ",");
}
bufWriter.newLine();
}
bufWriter.close();
}catch(Exception e){
//e.printStackTrace();
BufferedWriter bufWriter;
try {
bufWriter = new BufferedWriter(new FileWriter(logFile, append));
Iterator it = toWrite.getIterator();
while(it.hasNext()){
Table2Container container = (Table2Container)it.next();
String[] containerString = container.getTableItemsAsString();
for(int i = 0; i < containerString.length; i++){
bufWriter.write(containerString[i] + ",");
}
bufWriter.newLine();
}
bufWriter.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
/**
* Utility to write the log file
* @param String[] toWrite
* @param File logFile
* @param boolean append (true to append to the file, false to overwrite it)
*/
public static void writeToLog(final String[] toWrite, final File logFile, final boolean append){
//set up the writer
try{
BufferedWriter bufWriter = new BufferedWriter(new FileWriter(logFile, append));
for(int i = 0; i < toWrite.length; i++){
bufWriter.write(toWrite[i]);
bufWriter.newLine();
}
bufWriter.close();
}catch(Exception e){
e.printStackTrace();
}
}
//EOF
}