/home/fernape/Projects/lkmonitor/src/others.c File Reference

#include <gnome.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <glib.h>
#include <pthread.h>
#include <gtk/gtk.h>
#include "lininfo.h"
#include "processes.h"

Go to the source code of this file.

Defines

#define TAM   45

Functions

int numsort (const void *stra, const void *strb)
 Sorting function for scandir.
int read_values_from_file (int parm, char *path,...)
 Read integer values from file.
int write_values_to_file (int parm, char *path,...)
 Write integer values to file.
int write_string_to_file (const char *str, char *path)
char * trim (char *str)
char * rtrim (char *str)
 End trim.
char * skip_lines (char *contents, int num_lines)
void find_data_for (const char *pattern, char *member, const char *contents)
char * parse_batt (const char *str, char *contents, char *dest)
 End find_data_for.
gint list_com (gconstpointer a, gconstpointer b)
 Custom list comparison function.


Define Documentation

#define TAM   45


Function Documentation

void find_data_for ( const char *  pattern,
char *  member,
const char *  contents 
)

Main parse function. This function actually parses the contents of procfs files and tokenize elements to offer clean data.

Parameters:
pattern sscanf pattern format to make parsing
member Field struct to fill with parsed data
contents Contents of the file to be analyzed

Definition at line 223 of file others.c.

References rtrim(), and TAM.

Referenced by get_cpu_info(), get_mem_info(), get_mhz_info(), get_mm_details(), and get_stats_info().

00224 {       
00225    #define TAM 45
00226         g_assert(pattern!=NULL && member!=NULL && contents!=NULL);
00227         
00228         char p[TAM];
00229         char *line;
00230         
00231         sscanf(pattern,"%[^\t:]",p);
00232         //p has the name of the attribute (model, model name...)
00233         //strncpy(p,rtrim(p),TAM);
00234         rtrim(p);
00235         if((line=strstr(contents,p))==NULL){
00236                 strncpy(member,_("Unknown"),strlen(_("Unknown")));
00237                 *(member+strlen(_("Unknown")))='\0';
00238                 return;
00239         }//Position in the correct line
00240 
00241         sscanf(line, pattern,member);
00242         
00243 }                               

gint list_com ( gconstpointer  a,
gconstpointer  b 
)

Custom list comparison function.

Custom list elements comparison

Parameters:
a The first element
b The second element
Returns:
0 if the elements are equal. -1 Otherwise

Definition at line 275 of file others.c.

References lnode::pid.

00276 {
00277         struct lnode *la,*lb;
00278         
00279         la=(struct lnode *)a;
00280         lb=(struct lnode *)b;
00281         
00282         if(la->pid==lb->pid)
00283                 return 0;
00284         
00285         return -1;
00286 }

int numsort ( const void *  stra,
const void *  strb 
)

Sorting function for scandir.

Sorting by value, not by alphasort.

Parameters:
stra First string. ASCII representation of an integer
strb Second string. ASCII representation of an integer
Returns:
-1 if a < b, 0 if a = b, 1 if b > a

Definition at line 45 of file others.c.

Referenced by get_proc_list().

00046 {
00047         int a,b;
00048         a=atoi(stra);
00049         b=atoi(strb);
00050         
00051         
00052         return(a<b?(-1):(a==b?0:1));
00053 
00054 }

char* parse_batt ( const char *  str,
char *  contents,
char *  dest 
)

End find_data_for.

Parse battery information.

Parameters:
str String to search in contents
contents String that contents text
dest Destination string
Returns:
0 on success

Definition at line 251 of file others.c.

Referenced by get_acpi_info(), and get_battery_info().

00252 {
00253 
00254         g_assert(str!=NULL && contents!=NULL);
00255         
00256         char data[10];
00257         char *p;
00258         
00259         p=strstr(contents,str);
00260         if(p==NULL){
00261                 return(_("Unknown"));
00262         }
00263         p=strstr(p,":")+1;
00264         sscanf(p,"%s",data);
00265         strncpy(dest,data,sizeof(dest));
00266 
00267         return dest;
00268 }

int read_values_from_file ( int  parm,
char *  path,
  ... 
)

Read integer values from file.

Read integer values from file.

Parameters:
parm Number of parameters
path File to read
... variable list of parameters to read. Should be (int*)
Returns:
0 on success, -1 on error

Definition at line 62 of file others.c.

References modules_info::contents.

Referenced by get_settings_info().

00063 {
00064         g_assert(parm!=0 && path!=NULL);
00065         
00066         int i;
00067         int *val;
00068         va_list list;
00069         gchar *contents;
00070         char *p;
00071         
00072         if(g_file_get_contents((gchar *)path,&contents,NULL,NULL)==FALSE){
00073                 va_start(list,path);
00074                 for(i=0; i<parm;i++){
00075                         val=va_arg(list, int *);
00076                         *val=-1;
00077                 }
00078                 va_end(list);
00079                 g_warning("Failed to open %s file",path);
00080                 return -1;
00081         }
00082         
00083         p=contents;
00084         
00085         va_start(list,path);
00086         for(i=0; i<parm;i++){
00087                 sscanf(p,"%d",va_arg(list, int *));
00088                 p=strchr(p,'\t')+1;
00089         }
00090         va_end(list);
00091         
00092         g_free(contents);
00093         return(0);
00094 }

char* rtrim ( char *  str  ) 

End trim.

Deletes blanks from the end of string.

Parameters:
str String to delete blanks from
Returns:
Trimed String

Definition at line 175 of file others.c.

Referenced by find_data_for().

00176 {
00177         g_assert(str!=NULL);
00178         
00179         char* p;
00180         if((p=rindex(str,' '))==NULL)
00181                 return(str);
00182         while(p>str){
00183                 if(*p==' '){
00184                         *p='\0';
00185                         p--;
00186                 }
00187                 else return(str);
00188         }
00189         return(str);//For gcc warning   
00190 }

char* skip_lines ( char *  contents,
int  num_lines 
)

Delete initial lines from string.

Parameters:
contents String that contents text
num_lines Number of lines to leave out
Returns:
0 on success

Definition at line 198 of file others.c.

Referenced by get_devs_info(), get_net_info(), and lkmonitor_load_net_dev().

00199 {
00200   g_assert(contents!=NULL && num_lines>=0);
00201         
00202   int i, c;
00203 
00204   for (i = 0; i < num_lines; c++){
00205     if (*contents=='\n')
00206       i++;
00207 
00208     contents++;
00209         
00210   }
00211 
00212   return (contents+1);
00213 }

char* trim ( char *  str  ) 

Deletes blanks from the begining of string

Parameters:
str String to delete blanks from
Returns:
Trimed String

Definition at line 163 of file others.c.

00164 {
00165   g_assert(str!=NULL);
00166   return (str + strspn (str, " "));
00167 }                               

int write_string_to_file ( const char *  str,
char *  path 
)

Write a string to file.

Parameters:
str String to be written
path File to write
Returns:
0 on success, -1 on error

Definition at line 140 of file others.c.

Referenced by lkmonitor_write_settings().

00141 {
00142   g_assert(str!=NULL && path!=NULL);
00143         
00144   FILE *f;
00145   
00146   if((f=fopen(path,"w"))==NULL){
00147           g_warning("Couldn't open file %s",path);
00148           return -1;
00149   }
00150   
00151   fprintf(f,"%s",str);
00152   fclose(f);
00153   
00154   return(0);
00155 }

int write_values_to_file ( int  parm,
char *  path,
  ... 
)

Write integer values to file.

Write integer values from file.

Parameters:
parm Number of parameters
path File to read
... variable list of parameters to write. Should be int
Returns:
0 on success, -1 on error

Definition at line 102 of file others.c.

Referenced by lkmonitor_write_settings().

00103 {
00104         g_assert(parm!=0 && path!=NULL);
00105         
00106         int i;
00107         va_list list;
00108         FILE *target;
00109         gchar num_trans[15]; //Large enough
00110         GString *ms;
00111         
00112         ms=g_string_new("");
00113         
00114         if((target=fopen(path,"w"))==NULL){
00115                 g_warning("Can't write to %s\n",path);
00116                 return(-1);
00117         }
00118         
00119         /* File is ready to be written */
00120 
00121         va_start(list,path);
00122         for(i=0;i<parm;i++){
00123                 sprintf(num_trans,"%d",va_arg(list,int));
00124                 g_string_append(ms,num_trans);
00125                 g_string_append(ms," ");
00126         }
00127         va_end(list);
00128 
00129         /* Write */
00130         fprintf(target,"%s",ms->str);
00131         fclose(target);
00132         return(0);
00133 }


Generated on Tue Apr 1 22:52:56 2008 for lkmonitor by  doxygen 1.5.1