Project Homepage | Sourceforge Page | CVS Repository | Freshmeat.net Page | Download project | Author's Homepage |
00001 /*!\file comboHandlers.c 00002 \brief Handle comboBox operations 00003 00004 The functions here are somewhat misplaced for now. I'll fix this in 00005 run-free 3.0. Basically, the idea is that we are trying to handle 00006 functionality that is based on the values of the combo box. 00007 */ 00008 /* NOTICE: 00009 Copyright (C) 2004 Karl N. Redman (SleepingStill.com) 00010 00011 This program is free software; you can redistribute it and/or modify 00012 it under the terms of the GNU General Public License as published by 00013 the Free Software Foundation; either version 2 of the License, or 00014 (at your option) any later version. 00015 00016 This program is distributed in the hope that it will be useful, 00017 but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 GNU General Public License for more details. 00020 00021 You should have received a copy of the GNU General Public License 00022 along with this program; if not, write to the Free Software 00023 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00024 00025 For further information contact: parasyte@sleepingstill.com 00026 */ 00027 00028 #include <stdio.h> 00029 #include <stdlib.h> 00030 #include <string.h> 00031 #include <gtk/gtk.h> 00032 00033 #include <config.h> 00034 00035 #include "main.h" 00036 #include "comboHandlers.h" 00037 #include "debug.h" 00038 00039 00040 //############## FUNCTION definition 00041 /** 00042 \brief Read the history file. 00043 00044 \author Karl N. Redman 00045 00046 \param list A list to populate (must be NULL) -probably 00047 superfluious. 00048 \param filename The filename of the file to read from 00049 00050 \return A populated GList. 00051 00052 \par Purpose: 00053 Read a history file and populate a GList with it's data. The list 00054 is populated in reverse of the file contents to accomidate for 00055 (only) a file that is appended to. 00056 */ 00057 GList *readHistory(GList *list, char *filename) 00058 { 00059 FILE *fp; 00060 00061 /** \warning This caused alot of headeache. In the first place, gtk 00062 GList operations are not intuitive at all. In the end it was 00063 discovered that, because the values of the glist are pointers, 00064 that we must use a persistant glist to populate our target 00065 glist. What this means is that we end up passing in and returning 00066 the same pointer (not the same address) in order to properly 00067 populate our GList. 00068 */ 00069 00070 //grab the environment $HOME directory 00071 //TODO 00072 if( (fp = fopen(filename, "a+") ) == NULL) 00073 { 00074 dprint("run-free: bash_history file not found"); 00075 00076 /**\todo 00077 we are not handling file operations properly here -I was 00078 probably tired and lazy at the time. This MUST be fixed in 00079 the near future! 00080 */ 00081 00082 //ok, try a different file 00083 //TODO 00084 //else pop up a dialog 00085 g_print("could not open history file\n"); 00086 } 00087 00088 //max line length is the system max (this could be huge!) 00089 char line[RF_LINE_MAX_LEN+1]; 00090 int c; 00091 int i =0, numItems =0; 00092 //char *string; 00093 00094 memset(line, '\0', sizeof(line)); 00095 00096 do 00097 { 00098 if( (c = fgetc(fp)) == EOF) 00099 { 00100 //dprint("end of file\n"); 00101 break; 00102 } 00103 if(c != '\n') 00104 { 00105 if(i >= RF_LINE_MAX_LEN) 00106 dprint("HISTORY LINE LEN TOO LONG!\n"); 00107 00108 line[i] = c; 00109 i++; 00110 continue; 00111 } 00112 else 00113 { 00114 i=0; 00115 if(strcmp(line, "\n") == 0) 00116 { 00117 memset(line, '\0', sizeof(line)); 00118 continue; 00119 } 00120 } 00121 00122 list = g_list_prepend(list, strdup(line)); 00123 numItems++; 00124 00125 memset(line, '\0', sizeof(line)); 00126 }while(1); 00127 00128 fclose(fp); 00129 00130 //list = g_list_reverse(list); 00131 00132 //eliminate duplicates 00133 GList *newList = NULL; 00134 00135 i=0; 00136 00137 //this is how we set the combo box length 00138 //int loop_num = RF_MAX_HISTLIST_LEN-1; 00139 int loop_num = history_len -1; 00140 00141 //weed out duplicates and only keep the first instance of a history 00142 //line 00143 for(;i < loop_num && (list != NULL); i++, 00144 list = g_list_next(list)) 00145 { 00146 00147 //see if we've seen this one before 00148 if(g_list_find_custom(newList, list->data, 00149 (GCompareFunc)strcmp) == NULL) 00150 { 00151 //put the old command in the combo box 00152 newList = g_list_append(newList, (char *)list->data); 00153 } 00154 else 00155 { 00156 //increase the number of loops we're going to try and 00157 // grab the next one (unless NULL happens on next) 00158 ++loop_num; 00159 } 00160 }//end for 00161 00162 newList = g_list_prepend(newList, ""); 00163 return(newList); 00164 } 00165 00166 /** 00167 \brief Free up a GList and all it's members. 00168 00169 \author Karl N. Redman 00170 00171 \param list A pointer to a GList to be freed 00172 00173 \par Purpose: 00174 Frees the points in the GList and then the GList pointer itself. 00175 */ 00176 void freeList(GList *list) 00177 { 00178 int i =0; 00179 for(; i < (int)g_list_length(list); i++, list = list->next) 00180 g_free(list->data); 00181 00182 g_list_free(list); 00183 } 00184 00185 00186 /** 00187 \brief write a history file. 00188 00189 \author Karl N. Redman 00190 00191 \param command the command to append to the file 00192 \param filename the file to append the command to 00193 00194 \par 00195 Purpose: 00196 Appends the command to the end of the file 00197 */ 00198 int writeHistory(char *command, char *filename) 00199 { 00200 //always writes to the end of the file 00201 00202 FILE *fp; 00203 int retval = 0; 00204 int wLen = 0; 00205 00206 00207 fp = fopen(filename, "a+"); 00208 if(fp == NULL) 00209 { 00210 /**\todo FIX THIS I'm not handling file operations properly 00211 -probably tired at the time... -parasyte. 00212 */ 00213 //display dialog 00214 } 00215 00216 //write command line to file 00217 if((wLen = fputs(command, fp)) > 0) 00218 { 00219 retval = -1; 00220 } 00221 //else write a new line 00222 fputs("\n", fp); 00223 00224 fclose(fp); // close file 00225 00226 return retval; 00227 }