others.c

Go to the documentation of this file.
00001 /*
00002 lkmonitor (Linux Kernel Monitor)
00003 
00004 Application for monitoring and tuning a Linux kernel.
00005 
00006 Copyright (C) 2005-2006  Fernando ApesteguĂ­a
00007 
00008 This program is free software; you can redistribute it and/or
00009 modify it under the terms of the GNU General Public License
00010 as published by the Free Software Foundation; either version 2
00011 of the License, or (at your option) any later version.
00012 
00013 This program is distributed in the hope that it will be useful,
00014 but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 GNU General Public License for more details.
00017 
00018 You should have received a copy of the GNU General Public License
00019 along with this program; if not, write to the Free Software
00020 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00021 */
00022 
00023 #ifdef HAVE_CONFIG_H
00024 #  include <config.h>
00025 #endif
00026 
00027 #include <gnome.h>
00028 #include <string.h>
00029 #include <stdio.h>
00030 #include "lininfo.h"
00031 #include <sys/types.h>
00032 #include <dirent.h>
00033 
00034 #include <glib.h>
00035 #include <pthread.h>
00036 #include <gtk/gtk.h>
00037 
00044 int read_values_from_file(int parm,char *path, ...)
00045 {
00046         g_assert(parm!=0 && path!=NULL);
00047         
00048         int i;
00049         int *val;
00050         va_list list;
00051         gchar *contents;
00052         char *p;
00053         
00054         if(g_file_get_contents((gchar *)path,&contents,NULL,NULL)==FALSE){
00055                 va_start(list,path);
00056                 for(i=0; i<parm;i++){
00057                         val=va_arg(list, int *);
00058                         *val=-1;
00059                 }
00060                 va_end(list);
00061                 g_warning("Failed to open %s file",path);
00062                 return -1;
00063         }
00064         
00065         p=contents;
00066         
00067         va_start(list,path);
00068         for(i=0; i<parm;i++){
00069                 sscanf(p,"%d",va_arg(list, int *));
00070                 p=strchr(p,'\t')+1;
00071         }
00072         va_end(list);
00073         
00074         g_free(contents);
00075         return(0);
00076 }
00077 
00084 int write_values_to_file(int parm, char *path,...)
00085 {
00086         g_assert(parm!=0 && path!=NULL);
00087         
00088         int i;
00089         va_list list;
00090         FILE *target;
00091         gchar num_trans[15]; //Large enough
00092         GString *ms;
00093         
00094         ms=g_string_new("");
00095         
00096         if((target=fopen(path,"w"))==NULL){
00097                 g_warning("Can't write to %s\n",path);
00098                 return(-1);
00099         }
00100         
00101         /* File is ready to be written */
00102 
00103         va_start(list,path);
00104         for(i=0;i<parm;i++){
00105                 sprintf(num_trans,"%d",va_arg(list,int));
00106                 g_string_append(ms,num_trans);
00107                 g_string_append(ms," ");
00108         }
00109         va_end(list);
00110 
00111         /* Write */
00112         fprintf(target,"%s",ms->str);
00113         fclose(target);
00114         return(0);
00115 }
00116 
00122 int write_string_to_file(char *str,char *path)
00123 {
00124   g_assert(str!=NULL && path!=NULL);
00125         
00126   FILE *f;
00127   
00128   if((f=fopen(path,"w"))==NULL){
00129           g_warning("Couldn't open file %s",path);
00130           return -1;
00131   }
00132   
00133   fprintf(f,"%s",str);
00134   printf("%s",str);
00135   fclose(f);
00136   
00137   return(0);
00138 }
00139 
00140 
00145 char *
00146 trim (char *str)
00147 {
00148   g_assert(str!=NULL);
00149   return (str + strspn (str, " "));
00150 }                               
00151 
00152 
00157 char *
00158 rtrim(char* str)
00159 {
00160         g_assert(str!=NULL);
00161         
00162         char* p;
00163         if((p=rindex(str,' '))==NULL)
00164                 return(str);
00165         while(p>str){
00166                 if(*p==' '){
00167                         *p='\0';
00168                         p--;
00169                 }
00170                 else return(str);
00171         }
00172         return(str);//For gcc warning   
00173 }
00174 
00180 char *
00181 skip_lines (char* contents, int num_lines)
00182 {
00183   g_assert(contents!=NULL && num_lines>=0);
00184         
00185   int i, c;
00186 
00187   for (i = 0; i < num_lines; c++)
00188     if (*contents=='\n'){
00189       i++;
00190         }else{
00191        contents++;
00192         }
00193 
00194   return (contents+1);
00195 }
00196 
00204 void
00205 find_data_for (char* pattern,char* member, const char* contents)
00206 {       
00207 
00208         g_assert(pattern!=NULL && member!=NULL && contents!=NULL);
00209         
00210         char p[45];
00211         char *line;
00212         
00213         sscanf(pattern,"%[^\t:]",p);
00214         //p has the name of the attribute (model, model name...)
00215         strcpy(p,rtrim(p));
00216         if((line=strstr(contents,p))==NULL){
00217                 strcpy(member,_("Unknown"));
00218                 return;
00219         }//Position in the correct line
00220 
00221         sscanf(line, pattern,member);
00222         
00223 }                               
00224 
00230 char *parse_batt(char *str,char *contents)
00231 {
00232         g_assert(str!=NULL && contents!=NULL);
00233         
00234         char data[10];
00235         char *p;
00236         
00237         p=strstr(contents,str);
00238         if(p==NULL){
00239                 return(_("Unknown"));
00240         }
00241         p=strstr(p,":")+1;
00242         sscanf(p,"%s",data);
00243         p=malloc(sizeof(data));
00244         strcpy(p,data);
00245         return p;
00246 }

Generated on Sat Dec 2 11:27:51 2006 for lkmonitor by  doxygen 1.5.1