utils.c
Go to the documentation of this file.
00001 #include <string.h>
00002 #include <errno.h>
00003 
00004 #include "csm_all.h"
00005 
00007 FILE * open_file(const char *filename, const char*mode);
00008 
00009 
00010 FILE * open_file(const char *filename, const char*mode) {
00011         FILE*file = fopen(filename, mode);
00012         if(file==NULL) {
00013                 sm_error("Could not open file '%s': %s.\n", filename, strerror(errno)); 
00014                 return 0;
00015         }
00016         return file;
00017 }
00018 
00019 FILE * open_file_for_reading(const char*filename) {
00020         if(!strcmp(filename, "-"    )) return stdin;
00021         if(!strcmp(filename, "stdin")) return stdin;
00022         return open_file(filename, "r");
00023 }
00024 
00025 FILE * open_file_for_writing(const char*filename) {
00026         if(!strcmp(filename, "-"     )) return stdout;
00027         if(!strcmp(filename, "stdout")) return stdout;
00028         if(!strcmp(filename, "stderr")) return stderr;
00029         return open_file(filename, "w");
00030 }
00031 
00032 void my_basename(const char *file, char*dest) {
00033   const char *start = strrchr(file, DIR_SEPARATOR);
00034   if(!start)
00035         strcpy(dest, file);
00036   else
00037     strcpy(dest, start+1);
00038 }
00039 
00040 void my_basename_no_suffix(const char *file, char*dest) {
00041   const char *start = strrchr(file, DIR_SEPARATOR);
00042   if(!start) start = file; else start += 1;
00043   const char *end = strrchr(file, '.');
00044   if(!end || end<start) end = start + strlen(start);
00045 
00046   strncpy(dest, start, (size_t) (end-start));
00047   dest[end-start] = '\0';
00048 
00049 /*  printf("start: '%s' end: '%s' base_no_suffix: '%s'\n", start, end, dest);*/
00050 }
00051 
00052 void my_no_suffix(const char *file, char*dest) {
00053   const char *end = strrchr(file, '.');
00054   if(!end) end = file + strlen(file);
00055   strncpy(dest, file, (size_t) (end-file) );
00056   dest[end-file] = '\0';
00057 }
00058 
00059 
00061 char * my_strdup(const char *s) {
00062         size_t len = strlen(s) + 1; /* null byte */
00063         char * t = (char*) malloc(len);
00064         memcpy(t,s,len);
00065         return t;
00066 }
00067 


csm
Author(s): Andrea Censi
autogenerated on Mon Jan 16 2017 03:48:29