/[antiright]/antiright/lib/util.c
ViewVC logotype

Diff of /antiright/lib/util.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.6 by jefbed, Wed Dec 22 02:46:55 2004 UTC revision 1.7 by jefbed, Fri Jan 21 22:36:30 2005 UTC
# Line 1  Line 1 
1  /*  /*
2    AntiRight    AntiRight
3    (c) 2002-2004 Jeffrey Bedard    (c) 2002-2005 Jeffrey Bedard
4    jefbed@e-list.net    jefbed@e-list.net
5    
6    This file is part of AntiRight.    This file is part of AntiRight.
# Line 20  Line 20 
20    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */  */
22    
23  #include "util.h"  #include "library.h"
24    
25  void *  void *
26  xmalloc(size_t size)  xmalloc(size_t size)
27  {  {
28    void *pointer;    void *pointer;
29    pointer=malloc(size);    pointer=malloc(size);
30    assert(pointer != NULL);    assert(pointer != NULL); /* Ensure that the allocation was
31                                  successful.  */
32    return(pointer);    return(pointer);
33  }  }
34    
   
 int  
 antiright_system(char *field_string)  
 {  
   char *system_string;  
   int system_return_value=0;  
   extern int errno;  
     
   
   if((field_string != NULL) & (strlen(field_string)>0))  
     {  
       if(field_string[strlen(field_string)-1] != '&')  
         {  
           asprintf(&system_string, "%s &", field_string);  
           assert(system_string != NULL);  
           system(system_string);  
           free(system_string);  
         }  
       else /* The input command already includes an ampersand and will  
               become a background operation.  */  
         {  
           system(field_string);  
         }  
       return(system_return_value);  
     }  
   else if(errno==ENOENT)  
     {  
       perror("No such file or directory.  ");  
       return(-1);  
     }  
   else /*  The user input is empty.  */  
     {  
       perror("There was an error in executing the command.  ");  
       return(-1);  
     }    
 }  
   
35  /* Make sure that you free the returned string.  */  /* Make sure that you free the returned string.  */
36  char*  char*
37  antiright_pipe_read(char *command_string)  antiright_pipe_read(char *command_string)
# Line 104  antiright_fifo_server(char *command_stri Line 68  antiright_fifo_server(char *command_stri
68      }      }
69  }  }
70    
71  /* Free the returned value!  */  char *
72  char*  antiright_get_file_extension(char *filename)
 antiright_beautified_label(char* current_argument_string)  
73  {  {
74        char *label_string=xmalloc(strlen(current_argument_string));    char *extension=(char*)xmalloc(4);
75        int parse_counter;    unsigned int filename_length=strlen(filename);
76        int label_counter;    unsigned int counter=filename_length-3;
77            unsigned int extension_counter=0;
       for(parse_counter=strlen(current_argument_string);  
           current_argument_string[parse_counter]!=' ' && parse_counter>0;  
           parse_counter--);  
       for(label_counter=0;  
           current_argument_string[parse_counter]!='\0';  
           parse_counter++)  
         {  
           if(current_argument_string[parse_counter] == ' ')  
             parse_counter++; /* This gets rid of an extra leading  
                                 space.  */  
           if(current_argument_string[parse_counter] != '_')  
             label_string[label_counter]=current_argument_string[parse_counter];  
           else  
             label_string[label_counter]=' ';  
           label_counter++;  
         }  
       label_string[label_counter]='\0';  
       return(label_string);  
 }  
78    
79  char*    
80  antiright_insert_string(char *text, char *insertion, unsigned int position)    while(counter < filename_length)
 {  
   /* Declare a counter for the output text.  */  
   unsigned int output_counter=0;  
   /* Declare a counter for the initial text.  */  
   unsigned int text_counter=0;  
   /* Declare a counter for the inserted text.  */  
   unsigned int insertion_counter=0;  
   /* Store these lengths to avoid excessive function calls.  */  
   const unsigned int text_length=strlen(text);  
   const unsigned int insertion_length=strlen(insertion);  
   /* Allocate enough memory to hold both strings and a null  
      character.  */  
   char *output=(char*)malloc(text_length+insertion_length+1);  
   /* Put all text before insertion point into output buffer.  */  
   while(text_counter < position)  
     {  
       output[text_counter]=text[text_counter];  
       text_counter++;  
     }  
   /* Start inserting at position.  */  
   output_counter=position;  
   while(insertion_counter <= insertion_length)  
     {  
       output[output_counter]=insertion[insertion_counter];  
       output_counter++;  
       insertion_counter++;  
     }  
   /* Add the rest of the initial text to the output.  */  
   while(text_counter <= text_length)  
81      {      {
82        output[output_counter]=text[text_counter];        extension[extension_counter]=filename[counter];
83        text_counter++;        counter++;
84        output_counter++;        extension_counter++;
85      }      }
86    /* Set the end of the output to be null terminated.  */    extension[extension_counter]='\0';
87    output[output_counter]='\0';    return(extension);
   /* The function user must make sure that the returned string is  
      freed after use.  */  
   return(output);  
88  }  }
89    
90  char*  void
91  antiright_delete_range(char *text, unsigned int start, unsigned int end)  antiright_exit_cb()
92  {  {
93    /* Store the length of TEXT.  */    exit(0);
   const unsigned int text_length=strlen(text);  
   /* Create an output string large enough to hold TEXT.  */  
   char *output=(char*)malloc(text_length+1);  
   /* Declare a counter for the text.  */  
   unsigned int text_counter=0;  
   /* Declare a counter for the output.  */  
   unsigned int output_counter=0;  
   /* Assign OUTPUT until START.  */  
   while(text_counter < start)  
     {  
       /* TEXT_COUNTER could be used but it is a clearer design to use  
          OUTPUT_CONUTER for assignment to OUTPUT.  */  
       output[output_counter]=text[text_counter];  
       output_counter++;  
       text_counter++;  
     }  
   /* This sets the position at which assignment will once again start,  
      removing all text between start and end through such.  */  
   text_counter=end+1;  
   /* Start assignment from the character after END.  */  
   while(text_counter <= text_length)  
     {  
       output[output_counter]=text[text_counter];  
       text_counter++;  
       output_counter++;  
     }  
   /* Set the end of the output to be null terminated.  */  
   output[output_counter]='\0';  
   /* The function user must make sure that the returned string is  
      freed after use.  */  
   return(output);  
94  }  }
95    
96    void
97    antiright_close_cb(Widget widget,
98                       XtPointer client_data,
99                       XtPointer call_data)
100    {
101      XtUnmanageChild(widget);
102      XtDestroyWidget(widget);
103      call_data=NULL;
104      client_data=NULL;
105    }
106    

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.7

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26