]> www.wagner.pp.ru Git - oss/fgis.git/blob - lib/file_utils.c
First checked in version
[oss/fgis.git] / lib / file_utils.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <signal.h>
4 #include <ctype.h>
5 #include <sys/stat.h>
6 #include "epp.h"
7 char* default_ext(const char *name,const char *ext)
8 {static char buf [1024];
9  strcpy(buf,name);
10  if (strcmp(buf+strlen(buf)-strlen(ext),ext)) {
11      struct stat statbuf;
12      strcat(buf,ext);
13      if (stat(buf,&statbuf)&&!stat(name,&statbuf)) {
14        strcpy(buf,name);
15      }
16  }
17  return buf;
18 }
19
20 char *force_ext(const char *name,const char *ext)
21 {static char buf [1024];
22  char *last_ext;
23  strcpy(buf,name);
24  if ((last_ext=strrchr(buf,'.'))&&last_ext>strrchr(buf,'/')) *last_ext='\0';
25  return strcat(buf,ext);
26 }
27 char *last_ext(const char *name)
28 {static char buf[128];
29  char *extptr;
30  extptr=strrchr(name,'.');
31  if(!extptr) return NULL;
32  if (strchr(extptr,'/')) return NULL;
33  return strcpy(buf,extptr);
34 }
35
36 int signal_recieved;
37 int show_progress(int row,int seqno,int total)
38 { fprintf(stderr,"\rProcessing row %d of %d",seqno,total);
39   fflush(stderr);
40   return (signal_recieved);
41 }
42 int show_percent(int row,int seqno,int total)
43 { static int already_done;
44   int percent=seqno*1000/total;
45   if (percent!=already_done)
46   {  fprintf(stderr,"\r%3d.%1d%% complete",percent/10,percent%10);
47      fflush(stderr);
48      already_done=percent;
49   }
50   return signal_recieved;
51 }
52  
53
54 int check_int(int row,int seqno,int total)
55
56   return (signal_recieved);
57 }
58
59 void int_handler(int sig_no)
60 { signal_recieved=sig_no;
61   signal(sig_no,int_handler);
62 }
63
64 void install_progress_indicator(int (*show_progress)(int,int,int))
65 { signal_recieved=0;
66   signal(SIGINT,int_handler);
67   EndLineProc=show_progress;
68 }
69
70 int clear_progress(int success)
71 { if (EndLineProc)
72   { fprintf(stderr,"\r%s                           \n",success?"Aborted":"Done");
73   signal(SIGINT,SIG_DFL);
74   }
75   return success;
76 }
77 void show_version(char *name,char *RCS_ID)
78 { char ver[25],*version=RCS_ID+1;
79      for(;*version!='$' && *version!=':';version++);
80      strcpy(ver,version);
81      version=strrchr(ver,'$');
82      if (version) *version=0;
83      printf("SoftWeyr %c%s. Version %s\n",toupper(name[0]),name+1,ver);
84      exit(0);
85                            
86 }