23 #define FILE_HEADER_DESC_STRING "# WD directory list file"
24 #define FILE_HEADER_VER_STRING "# File format: version 1"
26 #define USE_FAVOURITES_FILE_STR "USE_FAVOURITES"
28 #define TIME_FORMAT_STRING "%Y/%m/%d %H:%M:%S"
29 #define TIME_SSCAN_STRING "%04u/%02u/%02u %02u:%02u:%02u"
30 #define TIME_STRING_BUFFER_SIZE (30U)
32 #define ANSI_COLOUR_RED "\x1b[31m"
33 #define ANSI_COLOUR_GREEN "\x1b[32m"
34 #define ANSI_COLOUR_GREY "\x1b[37;2m"
35 #define ANSI_COLOUR_RESET "\x1b[0m"
42 #include <sys/param.h>
46 #define MIN_DIR_SIZE 100
62 #define DLI_SIZE (sizeof( struct dir_list_item ))
75 static wd_entity_t get_type(
const char*
const p_path );
77 const char*
const p_fn );
79 const char*
const p_fn );
82 static void increase_dir_alloc(
dir_list_t p_list )
97 const char*
const p_dir,
98 const char*
const p_name,
99 const time_t p_t_added,
100 const time_t p_t_accessed,
110 DEBUG_OUT(
"list does not have space, increasing allocation");
111 increase_dir_alloc( p_list );
117 if( idx < p_list->dir_size )
119 dest = (
char*)malloc( strlen( p_dir ) + 1);
121 strcpy( dest, p_dir );
126 dest = (
char*)malloc( strlen( p_name ) + 1);
128 strcpy( dest, p_name );
160 if( ret_val != NULL ) {
167 increase_dir_alloc( ret_val );
173 static time_t sscan_time(
const char*
const p_str )
201 ret_val = tm.tm_sec +
205 (tm.tm_year-70)*31536000 +
206 ((tm.tm_year-69)/4)*86400 -
207 ((tm.tm_year-1)/100)*86400 +
208 ((tm.tm_year+299)/400)*86400;
218 ret_val = load_dir_list_from_favourites( p_config, p_fn );
221 ret_val = load_dir_list_from_file( p_config, p_fn );
234 TCHAR path[MAX_PATH];
235 HRESULT hr = SHGetFolderPath(0, CSIDL_FAVORITES, 0, 0, path);
239 DEBUG_OUT(
"got favourites drectory: %s", path);
241 if (NULL != (FD = opendir (path)))
243 struct dirent* in_file;
244 DEBUG_OUT(
"opened favourites drectory: %s", path);
246 ret_val->
cfg = p_config;
247 while ((in_file = readdir(FD)))
249 if((0 != strcmp (in_file->d_name,
".")) &&
250 (0 != strcmp (in_file->d_name,
"..")))
252 size_t len = strlen( in_file->d_name );
254 ( 0 == stricmp( &(in_file->d_name[len-4]),
".ini" ))) {
255 char path[ MAXPATHLEN ];
256 char name[ MAXPATHLEN ];
261 DEBUG_OUT(
"found file: %s",in_file->d_name);
263 strncpy( name, in_file->d_name, len-4 );
273 add_dir( ret_val, path, name, added, accessed,
288 FILE* file = fopen( p_fn,
"rt" );
294 ret_val->
cfg = p_config;
296 if( ret_val != NULL ) {
297 char path[ MAXPATHLEN ];
298 char read[ MAXPATHLEN ];
299 char name[ MAXPATHLEN ];
311 DEBUG_OUT(
"generated empty bookmark list");
313 while(( fstr = fgets( read, MAXPATHLEN, file ) ) ||
327 if( read[0] !=
'#' ) {
328 size_t len = strlen( read );
332 for( trimmer = len - 1;
335 if(( read[ trimmer ] ==
'\r' ) ||
336 ( read[ trimmer ] ==
'\n' )) {
344 if( read[0] ==
':' ) {
346 if ( path[0] !=
'\0' ) {
348 DEBUG_OUT(
"creating new bookmark: %s",path);
351 add_dir( ret_val, path, name, added, accessed,
362 strcpy( path, &(read[1]) );
363 }
else if(( read[0] ==
'N' ) &&
364 ( read[1] ==
':' )) {
365 strcpy( name, &(read[2]) );
366 }
else if(( read[0] ==
'A' ) &&
367 ( read[1] ==
':' )) {
368 added = sscan_time(&(read[2]));
369 }
else if(( read[0] ==
'C' ) &&
370 ( read[1] ==
':' )) {
371 accessed = sscan_time(&(read[2]));
372 }
else if(( read[0] ==
'T' ) &&
373 ( read[1] ==
':' )) {
387 "Unrecognised content in bookmarks file: %s\n",
405 for( dir_loop = 0; dir_loop < p_list->
dir_count; dir_loop++, current_item++ )
417 static int find_dir_location(
dir_list_t p_list,
const char*
const p_dir,
size_t* p_loc )
423 for( dir_loop = 0; dir_loop < p_list->
dir_count; dir_loop++, current_item++ )
425 if( 0 == strcmp( p_dir, current_item->
dir_name )) {
427 if( p_loc != NULL ) {
441 if( p_dir < p_list->dir_count ) {
460 if( find_dir_location( p_list, p_dir, &location )) {
469 return( find_dir_location( p_list, p_dir, NULL ) );
472 #define CYGDRIVE_PREFIX "/cygdrive/"
475 char* ret_val = NULL;
484 ret_val = (
char*)malloc( strlen( p_dir ) + 14 );
486 if(((( src[0] >=
'a' ) && (src[0] <=
'z' )) ||
487 (( src[0] >=
'A' ) && (src[0] <=
'Z' ))) &&
489 (( src[2] ==
'\\') ||
513 ret_val = (
char*)malloc( strlen( p_dir ) + 1 );
539 fprintf(stderr,
"Unhandled directory format\n");
553 for( dir_loop = 0, current_item = p_list->
dir_list;
554 dir_loop < p_list->dir_count;
555 dir_loop++, current_item++ )
559 ( 0 == strcmp( p_dir, current_item->
dir_name ))) {
563 fprintf( stdout,
"%s", dir_formatted );
565 if( current_item->
dir_name != dir_formatted ) {
566 free( dir_formatted );
586 for( dir_loop = 0, current_item = p_list->
dir_list;
587 dir_loop < p_list->dir_count;
588 dir_loop++, current_item++ )
595 fprintf( stdout,
"%s", dir_formatted );
597 if( current_item->
dir_name != dir_formatted ) {
598 free( dir_formatted );
613 static wd_entity_t get_type(
const char*
const p_path )
617 int err = stat( p_path , &s);
621 if(ENOENT == errno) {
625 if( S_ISDIR(s.st_mode) ) {
627 }
else if( S_ISREG( s.st_mode )) {
639 fprintf( stdout,
"Empty dirlist structure\n" );
645 for( dir_loop = 0, current_item = p_list->
dir_list;
646 dir_loop < p_list->dir_count;
647 dir_loop++, current_item++ )
677 fprintf( stdout,
"%s\n", dir_formatted );
681 if( dir != dir_formatted ) {
682 free( dir_formatted );
692 char* term = getenv(
"TERM");
695 if(( strcmp( term,
"vt100" ) == 0 ) ||
696 ( strcmp( term,
"xterm" ) == 0 )) {
705 WORD TextColour(WORD fontcolor)
707 HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
708 CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
711 GetConsoleScreenBufferInfo(h, &csbiInfo);
712 wOldColorAttrs = csbiInfo.wAttributes;
716 SetConsoleTextAttribute(h,fontcolor);
718 return wOldColorAttrs;
722 static void dump_time(
const char*
const p_header,
const time_t*
const p_time )
726 strftime( buffer,
sizeof( buffer ),
"%c %Z",
729 fprintf( stdout,
"\n - %s: %s",
738 fprintf( stdout,
"Empty dirlist structure\n" );
744 fprintf( stdout,
"Dirlist has %d entries of %d used\n",
747 for( dir_loop = 0, current_item = p_list->
dir_list;
748 dir_loop < p_list->dir_count;
749 dir_loop++, current_item++ )
759 current_item->
type = get_type( dir );
763 wcol = FOREGROUND_INTENSITY;
769 wcol = FOREGROUND_RED;
774 wcol = FOREGROUND_GREEN;
779 fprintf( stdout,
"[%3d] ", dir_loop);
782 wOldColorAttrs = TextColour(wcol);
786 fprintf( stdout,
"%s", col );
790 fprintf( stdout,
"%s", dir_formatted );
792 if( current_item->
dir_name != dir_formatted ) {
793 fprintf( stdout,
"\n - Unconverted: %s",
795 free( dir_formatted );
800 fprintf( stdout,
"\n - Shorthand: %s",
804 dump_time(
"Added", &( current_item->
time_added ) );
811 TextColour(wOldColorAttrs);
826 assert( p_fn != NULL );
827 assert( p_list != NULL );
829 file = fopen( p_fn,
"wt" );
837 for( dir_loop = 0; dir_loop < p_list->
dir_count; dir_loop++ )
841 fprintf( file,
":%s\n",
845 fprintf( file,
"N:%s\n",
853 fprintf( file,
"A:%s\n",buff);
861 fprintf( file,
"C:%s\n",buff);
869 switch( this_item->
type )
881 fprintf( file,
"T:%s\n",type_string);
int dir_in_list(dir_list_t p_list, const char *const p_dir)
char * format_dir(wd_dir_format_t p_fmt, char *p_dir)
#define FILE_HEADER_VER_STRING
void list_dirs(const dir_list_t p_list)
int wd_output_all
Control whether or not all items should be output regardless of whether or not they seem to point to ...
int dump_dir_with_name(const dir_list_t p_list, const char *const p_name)
time_t wd_now_time
Time to use as the current time when manipulating datestamps.
#define TIME_SSCAN_STRING
struct dir_list_item * dir_list
#define TIME_STRING_BUFFER_SIZE
struct dir_list_s * dir_list_t
Structure to represent a list of directory bookmarks.
#define FILE_HEADER_DESC_STRING
The dir_list module provides functions to manipulate and search the list of bookmarks.
int dump_dir_if_exists(const dir_list_t p_list, const char *const p_dir)
dir_list_t load_dir_list(const config_container_t *const p_config, const char *const p_fn)
Load a set of bookmarks from the specified file.
int bookmark_in_list(dir_list_t p_list, const char *const p_name)
int determine_if_term_is_ansi()
#define USE_FAVOURITES_FILE_STR
const config_container_t * cfg
#define TIME_FORMAT_STRING
int save_dir_list(const dir_list_t p_list, const char *p_fn)
#define ANSI_COLOUR_GREEN
int remove_dir_by_index(dir_list_t p_list, const size_t p_dir)
int add_dir(dir_list_t p_list, const char *const p_dir, const char *const p_name, const time_t p_t_added, const time_t p_t_accessed, const wd_entity_t p_type)
int wd_store_access
Indicate whether or not access times should be stored in the bookmarks.
void dump_dir_list(const dir_list_t p_list)
#define ANSI_COLOUR_RESET
wd_dir_format_t wd_dir_form
Format in which file paths should be output.
wd_entity_t wd_entity_type
Control which types of entity should be included in the output.
int remove_dir(dir_list_t p_list, const char *const p_dir)
dir_list_t new_dir_list(void)
Create a new directory list structure.