[R] A possible way to reduce basic questions

Jim Lemon bitwrit at ozemail.com.au
Fri Dec 3 16:14:31 CET 2004


As there seems to be some interest in this concept, I'll include the C code 
(The attachment seems to have been deleted from my last message).

I compiled this as follows:

gcc -o indxlst indxlst.c

It runs like this:

indxlst -t <path_to_R_library_directory>

producing a file named

INDEX_list.html

that should be viewable with any HTML browser.

Jim

/* indxlst.c
A program to search R INDEX files and build an HTML page of their contents
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include <sys/stat.h>

#define MAX_LINE 128

typedef struct {
 char *top_dir;
 FILE *instream;
 FILE *htmlstream;
} IL_INFO;

void InitializeILInfo(IL_INFO *il_info) {
 il_info->top_dir = NULL;
}

/* Usage
Displays a simple usage message.
*/

void Usage(void) {
 char *helplines[] =
  {"\nUsage: indxlst -t <R library directory> -h\n",
   NULL
  };
 int line = 0;

 while(helplines) fprintf(stdout,"%s",helplines[line++]);
}

/* ProcessArgs
Tries to process all valid arguments supplied to the program
on the command line.
	Return value	0 = all arguments valid AND
				R library path specified
			1 = at least one bad argument OR
				R library path not specified
*/

int ProcessArgs(int nargs,char **arg,IL_INFO *il_info) {
 int optchar;
 int badargs = 0;

 while((optchar = getopt(nargs,arg,"ht:")) != -1) {
  switch(optchar) {
   case 'h' :
    Usage();
    break;
   case 't' :
    il_info->top_dir = optarg;
    break;
   case '?' :
    badargs++;
    fprintf(stderr,"Invalid argument - %c\n",optopt);
  }
 }
 return(badargs || !il_info->top_dir);
}

void StartHTMLfile(FILE *current_stream) {
 fprintf(current_stream,"<html><head><title>R function 
listing</title></head>\n");
 fprintf(current_stream,"<body><center><h1>R function 
listing</h1></center>\n");
}

void EndHTMLfile(FILE *current_stream) {
 fprintf(current_stream,"</body></html>\n");
}

void GetINDEXpaths(IL_INFO *il_info) {
 char find_cmd[MAX_LINE];
 strcpy(find_cmd,"find ");
 strcat(find_cmd,il_info->top_dir);
 strcat(find_cmd," -name INDEX > INDEX_list.txt\n");
 system(find_cmd);
}

int ProcessINDEXFiles(IL_INFO *il_info) {
 FILE *IFstream;
 char current_file[MAX_LINE];
 char *package_name;
 char *desc_ptr;
 char INDEX_line[MAX_LINE];
 int retval = 1;
 int filename_index;

 il_info->htmlstream = fopen("INDEX_list.html","w");
 if(il_info->htmlstream) {
  StartHTMLfile(il_info->htmlstream);
  IFstream = fopen("INDEX_list.txt","r");
  if(IFstream) {
   while(!feof(IFstream)) {
    if(fgets(current_file,MAX_LINE,IFstream)) {
     filename_index = 0;
     while(current_file[filename_index] > 31) filename_index++;
     current_file[filename_index] = 0;
     il_info->instream = fopen(current_file,"r");
     if(il_info->instream) {
      /* find the last slash in the path */
      package_name = strrchr(current_file,'/');
      /* null terminate at the slash */
      *package_name = 0;
      /* go back to the next slash */
      package_name = strrchr(current_file,'/');
      /* move to the package name */
      package_name++;
      fprintf(il_info->htmlstream,"<h2>%s</h2><table>\n",package_name);
      while(!feof(il_info->instream)) {
       fgets(INDEX_line,MAX_LINE,il_info->instream);
       desc_ptr = INDEX_line;
       while(*desc_ptr > 32) desc_ptr++;
       /* if there was more than one word on the line */
       if(desc_ptr) {
        *desc_ptr = 0;
        *desc_ptr++;
        fprintf(il_info->htmlstream,"<tr><td>%s<td>%s</tr>\n",
         INDEX_line,desc_ptr);
       }
       else fprintf(il_info->htmlstream,"<tr><td>%s</tr>\n",INDEX_line);
      }
      fprintf(il_info->htmlstream,"</table>\n");
      fclose(il_info->instream);
     }
     else printf("Can't open INDEX file %s",current_file);
    }
    retval = 0;
   }
   fclose(IFstream);
  }
  else printf("Can't open INDEX_list.txt\n");
  EndHTMLfile(il_info->htmlstream);
  fclose(il_info->htmlstream);
 }
 else printf("Can't open INDEX_list.html\n");
}

int main(int argc,char *argv[]) {
 IL_INFO il_info;
 int retval = 1;

 InitializeILInfo(&il_info);
 if(!ProcessArgs(argc,argv,&il_info)) {
  GetINDEXpaths(&il_info);
  ProcessINDEXFiles(&il_info);
  /* get rid of the list of INDEX files */
  unlink("INDEX_list.txt");
 }
 else Usage();
 return(retval);
}




More information about the R-help mailing list