00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <stdio.h>
00029 #include <string.h>
00030 #include <stdlib.h>
00031
00032 #include "uti.h"
00033 #include "xalloc.h"
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 fntmp * fntmp_construct( const char * dest, const char * tmpfile )
00044 {
00045 fntmp * f;
00046 f = (fntmp *)xmalloc(sizeof(fntmp));
00047 f->name = xstrdup(dest);
00048 if (tmpfile!=NULL)
00049 f->tmp = path_src2destp(dest,tmpfile,NULL);
00050 else {
00051 f->tmp = f->name;
00052 frename_bak(f->tmp,f->tmp);
00053 }
00054 return f;
00055 }
00056
00057
00058
00059
00060
00061 const char * fntmp_getname( fntmp * f )
00062 {
00063 return f->tmp;
00064 }
00065
00066
00067
00068
00069
00070
00071 void fntmp_destruct( fntmp * f )
00072 {
00073 if (f->tmp!=f->name) {
00074 xfrename_bak(f->tmp,f->name);
00075 xfree(f->tmp);
00076 }
00077 xfree(f->name);
00078 xfree(f);
00079 }
00080
00081
00082
00083
00084
00085 size_t path_namepos( const char * path )
00086 {
00087 size_t l;
00088 l = strlen(path);
00089 while ((l)&&(path[l]!=PATH_DIRSEP)&&(path[l]!=PATH_DRIVESEP))
00090 l--;
00091 if ((path[l]==PATH_DIRSEP)||(path[l]==PATH_DRIVESEP))
00092 return(l+1);
00093 else
00094 return(l);
00095 }
00096
00097
00098
00099
00100 size_t path_extpos( const char * path )
00101 {
00102 size_t l;
00103 l = strlen(path);
00104 while ((l)&&(path[l]!='.')&&(path[l]!=PATH_DIRSEP))
00105 l--;
00106 if (path[l]=='.')
00107 return(l);
00108 else
00109 return(strlen(path));
00110 }
00111
00112
00113
00114
00115
00116
00117 char * path_dirchange( char * path, const char * new_dir )
00118 {
00119 char * tmp;
00120 tmp=xstrdup(&(path[path_namepos(path)]));
00121 strcpy(path,new_dir);
00122 strcat(path,tmp);
00123 xfree(tmp);
00124 return(path);
00125 }
00126
00127
00128
00129
00130
00131
00132 char * path_namechange( char * path, const char * new_name)
00133 {
00134 char * ex;
00135 ex=xstrdup(&(path[path_extpos(path)]));
00136 path[path_namepos(path)]=0;
00137 strcat(path,new_name);
00138 strcat(path,ex);
00139 xfree(ex);
00140 return(path);
00141 }
00142
00143
00144
00145
00146
00147
00148 char * path_extchange( char * path, const char * new_ext )
00149 {
00150 path[path_extpos(path)]=0;
00151 strcat(path,new_ext);
00152 return(path);
00153 }
00154
00155
00156
00157
00158
00159
00160 int frename_del( const char * name, const char * new_name )
00161 {
00162 remove(new_name);
00163 return rename(name,new_name);
00164 }
00165
00166
00167
00168
00169 void xfrename_del( const char * name, const char * new_name )
00170 {
00171 remove(new_name);
00172 xrename(name,new_name);
00173 }
00174
00175
00176
00177
00178
00179
00180 int frename_ext_del( const char * name, const char * new_ext )
00181 {
00182 int retval;
00183 char * tmp;
00184 tmp=(char *)xmalloc(strlen(name)+strlen(new_ext)+1);
00185 strcpy(tmp,name);
00186 path_extchange(tmp,new_ext);
00187 retval = frename_del(name,tmp);
00188 xfree(tmp);
00189 return retval;
00190 }
00191
00192
00193
00194
00195 void xfrename_ext_del( const char * name, const char * new_ext )
00196 {
00197 char * tmp;
00198 tmp=(char *)xmalloc(strlen(name)+strlen(new_ext)+1);
00199 strcpy(tmp,name);
00200 path_extchange(tmp,new_ext);
00201 xfrename_del(name,tmp);
00202 xfree(tmp);
00203 }
00204
00205
00206
00207
00208
00209
00210
00211 int frename_bak( const char * name, const char * new_name )
00212 {
00213 frename_ext_del(new_name,PATH_EXT_BAK);
00214 return rename(name,new_name);
00215 }
00216
00217
00218
00219
00220 void xfrename_bak( const char * name, const char * new_name )
00221 {
00222 frename_ext_del(new_name,PATH_EXT_BAK);
00223 xrename(name,new_name);
00224 }
00225
00226
00227
00228
00229
00230 int frename_ext_bak( const char * name, const char * new_ext )
00231 {
00232 int retval;
00233 char * tmp;
00234 tmp=(char *)xmalloc(strlen(name)+strlen(new_ext)+1);
00235 strcpy(tmp,name);
00236 path_extchange(tmp,new_ext);
00237 retval = frename_bak(name,tmp);
00238 xfree(tmp);
00239 return retval;
00240 }
00241
00242
00243
00244
00245 void xfrename_ext_bak( const char * name, const char * new_ext )
00246 {
00247 char * tmp;
00248 tmp=(char *)xmalloc(strlen(name)+strlen(new_ext)+1);
00249 strcpy(tmp,name);
00250 path_extchange(tmp,new_ext);
00251 xfrename_bak(name,tmp);
00252 xfree(tmp);
00253 }
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275 char * path_src2destde( const char * source, const char * dest,
00276 const char * ddir, const char * dext )
00277
00278 {
00279 #define LEN(str) ((str)?strlen(str):0)
00280 #define LLENO(str) (LEN(str))
00281 #define STRNCPY(dest,source,n) { \
00282 strncpy(dest,source,n); \
00283 dest[n] = 0; \
00284 }
00285 #define STRNCAT(dest,source,n) { \
00286 dest[strlen(dest)+1]=0; \
00287 strncat(dest,source,n); \
00288 }
00289
00290 size_t sl, sn,se, dl, dn,de;
00291 char * tmp = (char *)xmalloc(LEN(source)+LEN(dest)+LEN(ddir)+LEN(dext)+4);
00292
00293 sn=se=dn=de=0;
00294 if ((dl=LEN(dest))!=0) {
00295 dn=path_namepos(dest);
00296 de=path_extpos(dest);
00297 }
00298 if ((sl=LEN(source))!=0) {
00299 sn=path_namepos(source);
00300 se=path_extpos(source);
00301 }
00302
00303 if (dn) STRNCPY(tmp,dest,dn)
00304 else if LLENO(ddir) strcpy(tmp,ddir);
00305 else if (sn) STRNCPY(tmp,source,sn)
00306 else *tmp=0;
00307
00308 if (de-dn) STRNCAT(tmp,dest+dn,de-dn)
00309 else if (se-sn) STRNCAT(tmp,source+sn,se-sn);
00310
00311 if (dl-de) strcat(tmp,dest+de);
00312 else if LLENO(dext) strcat(tmp,dext);
00313 else if (sl-se) strcat(tmp,source+se);
00314
00315 return tmp;
00316 #undef LEN
00317 #undef LLENO
00318 #undef STRNCPY
00319 #undef STRNCAT
00320 }
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353 char * path_src2destp( const char * source, const char * dest,
00354 const char *dpath )
00355
00356 {
00357 #define LEN(str) ((str)?strlen(str):0)
00358 #define LLENO(str) (LEN(str))
00359 #define STRNCPY(dest,source,n) { \
00360 strncpy(dest,source,n); \
00361 dest[n] = 0; \
00362 }
00363 #define STRNCAT(dest,source,n) { \
00364 dest[strlen(dest)+1]=0; \
00365 strncat(dest,source,n); \
00366 }
00367 size_t sl,sn,se, dl,dn,de, ddl,ddn,dde;
00368 char * tmp = (char *)xmalloc(LEN(source)+LEN(dest)+LEN(dpath)+4);
00369
00370 sn=se=dn=de=ddn=dde=0;
00371 if ((dl=LEN(dest))!=0) {
00372 dn=path_namepos(dest);
00373 de=path_extpos(dest);
00374 }
00375 if ((sl=LEN(source))!=0) {
00376 sn=path_namepos(source);
00377 se=path_extpos(source);
00378 }
00379 if ((ddl=LEN(dpath))!=0) {
00380 ddn=path_namepos(dpath);
00381 dde=path_extpos(dpath);
00382 }
00383
00384 if (dn) STRNCPY(tmp,dest,dn)
00385 else if (ddn) STRNCPY(tmp,dpath,ddn)
00386 else if (sn) STRNCPY(tmp,source,sn)
00387 else *tmp=0;
00388
00389 if (de-dn) STRNCAT(tmp,dest+dn,de-dn)
00390 else if (dde-ddn) STRNCAT(tmp,dpath+ddn,dde-ddn)
00391 else if (se-sn) STRNCAT(tmp,source+sn,se-sn);
00392
00393 if (dl-de) strcat(tmp,dest+de);
00394 else if (ddl-dde) strcat(tmp,dpath+dde);
00395 else if (sl-se) strcat(tmp,source+se);
00396
00397 return tmp;
00398 }
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416 char * path_src2dest( const char * source, const char * dest )
00417
00418 {
00419 return path_src2deste(source,dest,NULL);
00420 }
00421
00422
00423
00424
00425
00426
00427 char * path_src2deste( const char * source, const char * dest, const char *dext )
00428
00429 {
00430 static char dd[]={ '.', PATH_DIRSEP, '\0'};
00431 char * s;
00432
00433 if (!strcmp(dest,"-")) s=xstrdup(dest);
00434 else if (!strcmp(source,"-")) s=path_src2destde("noname",dest,dd,dext);
00435 else s=path_src2destde(source,dest,dd,dext);
00436
00437 if (s==strstr(s,dd)) strcpy(s,s+2);
00438 return s;
00439 }
00440
00441