others.c File Reference

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

Go to the source code of this file.

Functions

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 (char *str, char *path)
 Write string to file.
char * trim (char *str)
char * rtrim (char *str)
 End trim.
char * skip_lines (char *contents, int num_lines)
void find_data_for (char *pattern, char *member, const char *contents)
char * parse_batt (char *str, char *contents)
 Parser for battery info.


Function Documentation

void find_data_for ( 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 205 of file others.c.

References rtrim().

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

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 }                               

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

Parser for battery info.

Parse battery information.

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

Definition at line 230 of file others.c.

Referenced by get_acpi_info(), and get_battery_info().

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 }

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 44 of file others.c.

Referenced by get_settings_info().

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 }

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 158 of file others.c.

Referenced by find_data_for().

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 }

char* skip_lines ( char *  contents,
int  num_lines 
)

Delete initial lines form string.

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

Definition at line 181 of file others.c.

Referenced by get_devs_info().

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 }

char* trim ( char *  str  ) 

Deletes blanks from the begining of string

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

Definition at line 146 of file others.c.

00147 {
00148   g_assert(str!=NULL);
00149   return (str + strspn (str, " "));
00150 }                               

int write_string_to_file ( char *  str,
char *  path 
)

Write string to file.

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 122 of file others.c.

Referenced by lkmonitor_write_settings().

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 }

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 84 of file others.c.

Referenced by lkmonitor_write_settings().

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 }


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