void remove_todo(char* id) { FILE *fp; char *list_buffer; fp = open_list("r"); fseek(fp, 0L, SEEK_END); list_buffer = malloc(ftell(fp)*sizeof(char)); fseek(fp, 0L, SEEK_SET); char line [4096]; // todo: define line buffer somewhere while(fgets(line, sizeof(line), fp)){ if(!strstr(line, id)) { if(list_buffer[0] == '\0') { sprintf(list_buffer, "%s", line); } else { sprintf(list_buffer, "%s%s", list_buffer, line); } } else { printf("Removed task "); print_line_colored(line); } } fclose(fp); fp = open_list("w"); fprintf(fp, "%s", list_buffer); fclose(fp); free(list_buffer); }
void AppWindow::on_drag_data_received(const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, const Gtk::SelectionData& selection_data, guint info, guint time) { #ifdef DEBUG std::cout << "ON_DRAG_DATA_RECEIVED: type: " << selection_data.get_data_type() << std::endl; std::cout << "ON_DRAG_DATA_RECEIVED: data: " << selection_data.get_data_as_string() << std::endl; #endif // DEBUG if (selection_data.get_data_type() == "text/uri-list") { std::list<Glib::ustring> new_filenames = selection_data.get_uris(); const std::list<Glib::ustring>::iterator begin = new_filenames.begin(); const std::list<Glib::ustring>::iterator end = new_filenames.end(); std::list<Glib::ustring>::iterator iter = new_filenames.begin(); int counter = 0; while( iter != end ) { // we erase the protocol in front of the filename if( (*iter).find(':') != std::string::npos ) (*iter).erase(0, (*iter).find(':')+3); // unescape the URI char * tempfilename = curl_unescape( iter->c_str(), 0); (*iter) = tempfilename; // curl requires this to be freed like this curl_free( tempfilename ); #ifdef DEBUG std::cout << "ON_DRAG_DATA_RECEIVED: URI: " << *iter << std::endl; #endif // DEBUG iter++; counter++; } open_list( new_filenames, counter ); } // if we're given plain text, maybe it's still an uri or a filename, we should better check // ImageManager.OpenFiles is safe anyway else if( selection_data.get_data_type() == "text/plain" ) { std::string data = selection_data.get_data_as_string(); data.erase( data.find('\n') ); // erase any newlines, all we can do is one file // we erase the protocol in front of the filename if( data.find(':') != std::string::npos ) data.erase(0, data.find(':')+3); // unescape the URI char * tempfilename = curl_unescape( data.c_str(), 0); data = tempfilename; // curl requires this to be freed like this curl_free( tempfilename ); open_new_file( data ); } context->drag_finish(true, false, time); }
// show task with given id. too naive now. void list_id(char* id) { FILE *fp; fp = open_list("r"); char line[4096]; // todo: define line buffer somewhere while(fp && fgets(line, sizeof(line), fp)){ if(strstr(line,id)){ print_line_colored(line); } } fclose(fp); }
void add_todo(char string[]) { char* id = generate_id(); FILE *fp; fp = open_list("ar"); fprintf(fp, "%s\t%s\n", id, string); fclose(fp); printf("Added task with id %s%s%s:\t%s\n",colors[COLOR_GREEN], id, colors[COLOR_RESET], string); free(id); }
FILE *open_list(char *mode) { FILE *fp; char todoFilePath[256]; sprintf(todoFilePath,"%s/%s", getenv("HOME"), FILENAME); fp = fopen(todoFilePath, mode); if (fp == NULL) { fp = fopen(todoFilePath, "w"); fclose(fp); return open_list(mode); } // file opened properly; return FILE pointer return fp; }
void list_all() { FILE *fp; fp = open_list("r"); char line[4096]; // todo: define line buffer somewhere int counter = 0; while(fp && fgets(line, sizeof(line), fp)){ print_line_colored(line); counter++; } if(!counter){ printf("No tasks.\n"); help(); } fclose(fp); }
/* search each line in todofile for pattern*/ void search(char string[]) { regex_t reg; int err; char err_msg[80]; FILE *fp; char line[4096]; if((err = regcomp(®, string, REG_NOSUB|REG_EXTENDED)) != 0){ regerror(err, ®, err_msg, 80); printf("Error analyzing regular expression '%s': %s.\n", string, err_msg); } fp = open_list("r"); while(fgets(line, sizeof(line), fp)){ if(regexec(®, line + 4, 0, NULL, 0) == 0) print_line_colored(line); } fclose(fp); }
/* generates an unused id according to the id scheme */ char* generate_id(){ FILE *fp; fp = open_list("r"); char line[4096]; // todo: define line buffer somewhere char* id = malloc(6*sizeof(char)); int done =0; /* we just keep guessing at an unused id until we strike gold. * todo: do this in a way that doesn't potentially run forever. */ while(!done){ sprintf(id, "%s%s\t", mora[rand()%MORA_LENGTH], mora[rand()%MORA_LENGTH]); fseek(fp, 0, SEEK_SET); done = 1; while(fp && fgets(line, sizeof(line), fp)){ if(strstr(line,id)){ done = 0; } } } fclose(fp); id[5]='\0'; return id; }
int main () { char line[40], *str, cmd, last_cmd = 0; if (!(ld = open_list ())) { fprintf (stderr, "Error opening List\n"); exit (1); } printf ("Type \'-?\' or \'-h\' for Help contents\n"); while (1) { printf ("list> "); fgets (line, 40, stdin); for (str = line; *str == ' ' || *str == '\t'; str++) ; str[strchr (str, '\n') - str] = '\0'; switch (*str) { case '\0' : continue; default : append (ld, str); break; case '.' : cmd = last_cmd; case '-' : if (*str != '.') cmd = last_cmd = *++str; switch (cmd) { default : fprintf (stderr, "Invalid Command\n"); continue; case '?' : case 'h' : case 'H' : printf ("Type any name to add to list\n" "-r<name> to remove from list\n" "\'-d\' to display all list items\n" "\'-f\' to print the first item in list\n" "\'-l\' to print the last item in list\n" "\'-n\' to print the next list item\n" "\'-p\' to print the previous list item\n" "\'.\' to repeat last command\n" //"\'-s\' to sort list items\n" "\'-e\' to exit\n"); break; case 'd' : case 'D' : print_list (ld); break; case 'f' : case 'F' : if (read_first (ld, str)) PRINT_MSG case 'l' : case 'L' : if (read_last (ld, str)) PRINT_MSG case 'n' : case 'N' : if (read_next (ld, str)) PRINT_MSG case 'p' : case 'P' : if (read_prev (ld, str)) PRINT_MSG case 'r' : case 'R' : if (delete (ld, ++str)) { fprintf (stderr, "%s not on list\n", str); continue; } else printf ("%s removed\n", str); break; case 'e' : case 'E' : print_list (ld); close_list (ld); printf ("===> End of App <===\n"); exit (0); } } } }