📄 xtools.c
字号:
type = 1; } } else { /* data-file */ isosize = check_iso_file(-1,tmp,volid,0); if (isosize == 0) { /* unknown data */ type = 3; } else { /* iso9660 */ type = 0; } } /* allocate memory and fill structure */ entry = g_new(image_files_t,1); entry->path = g_strdup(tmp); entry->mtime = buf.st_mtime; entry->size = size; entry->type = type; entry->readable = readable; entry->from_track = 0; if (type == 0) { entry->volname = g_strdup(volid); } else { entry->volname = NULL; } entry->title = NULL; entry->artist = NULL; entry->cddb_ttitle = NULL; entry->cd_discid = NULL; entry->isosize = isosize; /* find if there is some information in the inf-file */ get_inf_tracktitle(tmp, entry); /* add to list */ *retlist = g_list_append(*retlist, entry);}/* scans a directory for files matching the known extensions *//* return 0 if ok, 1 on problem */gint get_img_files(gchar *path, GList **retlist) {gchar *img_ext[] = IMG_EXTENSIONS;struct dirent *ent;DIR *dir; gint i,len,len2; dir = opendir(path); /* invalid directory */ if (dir == NULL) return 1; /* scan a directory */ while ( (ent = readdir(dir)) ) { /* does the extension match? */ for(i = 0; img_ext[i] != NULL; i++) { len = strlen(img_ext[i]); len2 = strlen(ent->d_name); /* skip to short filenames */ if (len2 < len) continue; if (strncmp((ent->d_name)+len2-len,img_ext[i],len) == 0) { /* we found a match */ analyze_imgfile(path,ent->d_name,retlist); } } } closedir(dir); return 0;}/* print imagelist-memory-structure (debug purpose) */void print_imagelist() {GList *loop;image_files_t *entry; dodebug(2,"--------- imagelist glist ---------\n"); loop = g_list_first(imagelist); while (loop) { entry = loop->data; dodebug(2,"path: %s, %ld, %d, %d, %d\n",entry->path, entry->size, entry->type, entry->readable, entry->isosize); if (entry->volname != NULL) { dodebug(2, "\tvolname: %s\n", entry->volname); } loop = loop->next; } }/* search all image-directories and create a list of matching files *//* return number of matching files */gint scan_imagedirs() {GList *loop;gchar tmp[MAXLINE];image_files_t *entry; /* free the old image-list first */ loop = g_list_first(imagelist); while (loop) { entry = loop->data; g_free(entry->path); g_free(entry->volname); g_free(entry->title); g_free(entry->artist); g_free(entry->cddb_ttitle); g_free(entry); loop = loop->next; } g_list_free(imagelist); imagelist = NULL; loop = g_list_first(setupdata.image_dirs); while (loop) { /* image-dir extracted */ strncpy(tmp,(gchar *)loop->data, MAXLINE); get_img_files(tmp,&imagelist); loop = loop->next; } /* now we have a complete image-list */ if (debug) print_imagelist(); return (g_list_length(imagelist));}/* return a string saying which type of CD we are currently handling *//* mode = 0: used check cd in drive, mode = 1: check trackreadset for writing */gint determine_cd_type(gchar *ret, gint mode) {gint i;gint audio,data;gint type;gchar tmp[MAXLINE];GList *loop;track_read_param_t *trackparam; /* unknown type */ type = -1; /* count tracks */ audio = 0; data = 0; if (mode == 0) { /* check cdinfo-structure */ for (i = 0; i < cdinfo.nr_tracks; i++) { if (trackinfo[i]->type == 0) { data++; } else { audio++; } } } else { /* check trackreadset-structure */ loop = g_list_first(trackreadset.trackparams); while(loop) { trackparam = loop->data; if (trackparam->tracktype == 0) { data++; } else { audio++; } loop = loop->next; } /* now point trackparam back to first track for later use */ loop = g_list_first(trackreadset.trackparams); if (loop) trackparam = loop->data; else trackparam = NULL; } /* pure data-cd */ if (data == 1 && audio == 0) { type = 0; } else /* pure audio-cd */ if (data == 0 && audio > 0) { type = 1; } else /* mixed-mode */ if (mode == 0 && data == 1 && audio > 0 && trackinfo[0]->type == 0) { type = 2; } else if (mode == 1 && data == 1 && audio > 0 && trackparam->tracktype == 0) { type = 2; } else /* cd-extra */ if (mode == 0 && data == 1 && audio > 0 && cdinfo.have_cdextra) { type = 3; } else if (mode == 1 && data == 1 && audio > 0 && trackparam->tracktype == 1) { /* one data, at least one audio and first track audio */ type = 3; } else /* multisession */ if (data > 1 && audio == 0) { type = 4; } /* enough for now */ switch (type) { case 0: strncpy(tmp,text(128),MAXLINE); break; case 1: strncpy(tmp,text(129),MAXLINE); break; case 2: strncpy(tmp,text(130),MAXLINE); break; case 3: strncpy(tmp,text(131),MAXLINE); break; case 4: strncpy(tmp,text(132),MAXLINE); break; default: strncpy(tmp,text(127),MAXLINE); break; } /* return value */ strncpy(ret,tmp,MAXLINE); return (type);}/* calculate free space dependent of current image-dir setting *//* return free kbytes and kbytes free in biggest imagedir */gint determine_free_space(gint *biggestfree) {gchar tmp[MAXLINE];gchar path[MAXLINE];GList *loop;gint free, getfree;gint maxfree; /* get image-path */ if (curset.image_index == -1) { strncpy(path,"",MAXLINE); } else { strncpy(path,(gchar *)g_list_nth_data(setupdata.image_dirs, curset.image_index), MAXLINE); } free = 0; maxfree = 0; if (strcmp(path,"") != 0) { free = get_free_space(path,NULL); maxfree = free; } else { /* automatic setting - add all available space */ loop = g_list_first(setupdata.image_dirs); while(loop) { strcpy(tmp,(gchar *)loop->data); getfree = get_free_space(tmp,NULL); free += getfree; /* get biggest block */ if (getfree > maxfree) maxfree = getfree; loop = loop->next; } } if (free < 0) { g_warning("Invalid image-path setting?\n"); free = 0; maxfree = 0; } *biggestfree = maxfree; return free;}/* does look where to save the tracks before reading them. Checks available diskspace and the image-directory-settings. return 0 if ok, 1 on error/disk full *//* return via call by reference the size (in kbytes) that will be free due overwriting old files. Also return the freed size on the directory with the most space available */gint allocate_track_filenames(gint *overwrite, gint *overwritebiggest) {gchar tmp[MAXLINE];gchar biggestpath[MAXLINE];gchar path[MAXLINE];gchar ext[MAXLINE];track_read_param_t *trackparam;GList *loop, *loop2;gint free;gint size, tmpkbyte;gint ret;image_dir_free_t *freedir;GList *freedirs;struct stat buf;gint overwritefree, overwritefreebiggest;gint maxfree; overwritefree = 0; overwritefreebiggest = 0; ret = 0; freedirs = NULL; maxfree = 0; strcpy(biggestpath,""); /* build image-path/free structure */ if (curset.image_index == -1) { /* automatic setting */ loop = g_list_first(setupdata.image_dirs); while(loop) { strcpy(path,(gchar *)loop->data); free = get_free_space(path,NULL); freedir = g_new(image_dir_free_t,1); freedir->path = g_strdup(path); freedir->free = free; freedirs = g_list_append(freedirs,freedir); /* path with biggest available block? */ if (free > maxfree) { maxfree = free; strcpy(biggestpath,path); } loop = loop->next; } } else { /* single path */ strncpy(path,(gchar *)g_list_nth_data(setupdata.image_dirs, curset.image_index), MAXLINE); free = get_free_space(path,NULL); freedir = g_new(image_dir_free_t,1); freedir->path = g_strdup(path); freedir->free = free; freedirs = g_list_append(freedirs,freedir); maxfree = free; strcpy(biggestpath,path); } /* now we have a structure with all path we are allowed to save data in and how much space is available there */ /* loop through all available tracks */ loop = g_list_first(trackreadset.trackparams); while (loop) { trackparam = loop->data; if (trackparam->tracktype == 0) strcpy(ext,"img"); else strcpy(ext,"wav"); /* how much space needs this track? */ size = trackparam->kbyte; strcpy(path,""); /* where is enough space for it? */ loop2 = g_list_first(freedirs); while (loop2) { freedir = loop2->data; /* build temporary filename */ g_snprintf(tmp,MAXLINE, "%s/%s-%02d.%s", freedir->path, curset.file_prefix, trackparam->starttrack, ext); /* already a file with this name on hd? */ if (stat(tmp,&buf) == 0) { /* file exists */ tmpkbyte = buf.st_size/1024; overwritefree += tmpkbyte; /* file in directory with most space? */ if (strcmp(freedir->path,biggestpath) == 0) { overwritefreebiggest += tmpkbyte; } } else { tmpkbyte = 0; } /* enough free? consider space that is freed when we overwrite a file (tmpkbyte) */ if (size < (freedir->free + tmpkbyte)) { /* found freespace */ strcpy(path,freedir->path); freedir->free-=size - tmpkbyte; break; } loop2 = loop2->next; } /* no free space found? */ if (strcmp(path,"") == 0) { ret = 1; break; } /* tmp does contain now our valid filename */ g_free(trackparam->trackfile); trackparam->trackfile = g_strdup(tmp); loop = loop->next; } /* free image-path/free structure */ loop2 = g_list_first(freedirs); while (loop2) { freedir = loop2->data; g_free(freedir->path); g_free(freedir); loop2 = loop2->next; } g_list_free(freedirs); *overwrite = overwritefree; *overwritebiggest = overwritefreebiggest; if (debug > 1) { print_trackreadset(); } return ret;}/* does scan the image-structure for toc-files. Takes current image-dir-setting into account. Return number of found toc files or 0. Newest file is on top */ gint scan_for_toc_files() {GList *loop;image_files_t *entry;gchar basename[MAXLINE];gchar ipath[MAXLINE];gchar *p;time_t fdate; /* clear old list */ g_list_free(tocfiles); tocfiles = NULL; fdate = 0; loop = g_list_first(imagelist); while (loop) { entry = loop->data; /* toc-file */ if (entry->type == 4) { /* get the basedir */ strncpy(basename,entry->path,MAXLINE); p = rindex(basename,'/'); *p = '\0'; if (strcmp(basename,"") == 0) { strcpy(basename,"/"); } /* now check if the basedir fits in the currently set image-path */ if (curset.image_index != -1) { strncpy(ipath, (gchar *)g_list_nth_data( setupdata.image_dirs, curset.image_index), MAXLINE); /* does not fit - skip */ if (strcmp(ipath, basename) != 0) { loop = loop->next; continue; } } /* if new file newer than the old one */ if (entry->mtime < fdate) { /* append at back */ tocfiles = g_list_append(tocfiles,entry->path); } else { /* prepend at front */ tocfiles = g_list_prepend(tocfiles,entry->path); fdate = entry->mtime; } } loop = loop->next; } return g_list_length(tocfiles);}/* this function is called whenever a dialog window idles on the screen and we want that events are processed and if there are no events no CPU-time is wasted */void wait_and_process_events() { while (gtk_events_pending()) gtk_main_iteration(); usleep(100);}/* check if all files scheduled for writing does exist and have the right size. Return 0 if all ok, 1 if all files there but with wrong size, 2 if files missing and 3 if no permission to read/invalid */gint check_write_files(gint nosizecheck) {GList *loop;track_read_param_t *trackparam;struct stat buf;glong size;gint sumframes;gint fd;gint errsize, diff; sumframes = 0; errsize = 0; loop = g_list_first(trackreadset.trackparams); while(loop) { trackparam = loop->data; if (stat(trackparam->trackfile, &buf) != 0) { /* no such file */ return 2; } /* check if regular file or link */ if (S_ISLNK(buf.st_mode) != 1 && S_ISREG(buf.st_mode) != 1) { /* its not */ return 3; } /* readable for us? */ fd = open(trackparam->trackfile, O_RDONLY,0); if (fd == -1) { return 3; } else { close(fd); } if (trackparam->tracktype == 0) { /* datatrack */ size = trackparam->frames * DATASECTORSIZE; sumframes += trackparam->frames; } else { /* audiotrack */ size = trackparam->frames * CDDAFRAME; sumframes += trackparam->frames; } /* check size of file - allow a offset of 4096 bytes */ /* and offset of 152*2048 (leadout+runout sectors) */ /* (and allow offset of 44 bytes (wavheader)) */ diff = abs(size - (glong) buf.st_size); if (diff != 0 && diff != 4096 && diff != 152*2048 && diff != 44) { /* a file with wrong size found? */ errsize++; } loop = loop->next; } /* g_print("sumframes: %d\n", sumframes); */ if (errsize == 0 || nosizecheck) { /* all ok */ return 0; } else { /* files with wrong sizes */ return 1; }}/* get the size of a track given by filename from imagelist (in bytes) */ /* or -1 when not found */glong get_size_from_imagelist(gchar *tname) {GList *loop;image_files_t *entry; loop = g_list_first(imagelist); while (loop) { entry = loop->data; if (strcmp(tname,entry->path) == 0) { return (entry->size); } loop = loop->next; } return -1;}/* get the type of a track given by filename from imagelist */ /* or -1 when not found */gint get_type_from_imagelist(gchar *tname) {GList *loop;image_files_t *entry; loop = g_list_first(imagelist); while (loop) { entry = loop->data; if (strcmp(tname,entry->path) == 0) { return (entry->type); } loop = loop->next; } return -1;}/* get the number of a track given by filename from imagelist */ /* or -1 when not found */gint get_tracknr_from_imagelist(gchar *tname) {GList *loop;image_files_t *entry; loop = g_list_first(imagelist); while (loop) { entry = loop->data; if (strcmp(tname,entry->path) == 0) { return (entry->from_track); } loop = loop->next; } return -1;}image_files_t *get_entry_from_imagelist(gchar *tname) {GList *loop;image_files_t *entry; loop = g_list_first(imagelist); while (loop) { entry = loop->data; if (tname && strcmp(tname,entry->path) == 0) { return entry; } loop = loop->next; } return NULL;}/* get the discid of a track given by filename from imagelist */ /* or 1 when not found */gint get_discid_from_imagelist(gchar *tname, gchar *ret) {GList *loop;image_files_t *entry; loop = g_list_first(imagelist); while (loop) { entry = loop->data; if (strcmp(tname,entry->path) == 0) { if (entry->cd_discid == NULL) return 1; strcpy(ret, entry->cd_discid); return 0; } loop = loop->next; } return 1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -