/** * 所有的长度都用四字节编码 */ static int crl_req_time_list_2_file(struct cmp_db* cmdb,int fd){ struct list_head *head; struct crl_req_time* ptr; u32 size,temp; size = 0; pthread_mutex_lock(&cmdb->lock); head = &cmdb->crl_time.list; list_for_each_entry(ptr,head,list){ if( (temp = crl_req_time_2_file(ptr,fd)) < 0){ wave_printf_fl(MSG_ERROR,"crl_req_time_2_file 失败"); pthread_mutex_unlock(&cmdb->lock); return -1; } size +=1; } pthread_mutex_unlock(&cmdb->lock); size = host_to_be32(size); if( file_insert(fd,0,&size,4) ){ wave_error_printf("文件插入出错"); return -1; } if ( lseek(fd,4,SEEK_CUR) == -1){ wave_error_printf("文件移动偏移量出问题"); return -1; } return 1; }
void console_insert(char c) { /* insert a character: */ file_insert(c, cursor_off); console_update(); console_right(); }
int main(int argc, char *argv[]) { kbd_event_t ev; coord_t coord; bool new_file; spt_t pt; con = console_init(stdin, stdout); console_clear(con); console_get_size(con, &scr_columns, &scr_rows); pane.rows = scr_rows - 1; pane.columns = scr_columns; pane.sh_row = 1; pane.sh_column = 1; /* Start with an empty sheet. */ sheet_init(&doc.sh); /* Place caret at the beginning of file. */ coord.row = coord.column = 1; sheet_get_cell_pt(&doc.sh, &coord, dir_before, &pt); sheet_place_tag(&doc.sh, &pt, &pane.caret_pos); pane.ideal_column = coord.column; if (argc == 2) { doc.file_name = str_dup(argv[1]); } else if (argc > 1) { printf("Invalid arguments.\n"); return -2; } else { doc.file_name = NULL; } new_file = false; if (doc.file_name == NULL || file_insert(doc.file_name) != EOK) new_file = true; /* Move to beginning of file. */ caret_move(-ED_INFTY, -ED_INFTY, dir_before); /* Place selection start tag. */ tag_get_pt(&pane.caret_pos, &pt); sheet_place_tag(&doc.sh, &pt, &pane.sel_start); /* Initial display */ cursor_visible = true; cursor_hide(); console_clear(con); pane_text_display(); pane_status_display(); if (new_file && doc.file_name != NULL) status_display("File not found. Starting empty file."); pane_caret_display(); cursor_show(); done = false; while (!done) { console_get_kbd_event(con, &ev); pane.rflags = 0; if (ev.type == KEY_PRESS) { /* Handle key press. */ if (((ev.mods & KM_ALT) == 0) && ((ev.mods & KM_SHIFT) == 0) && (ev.mods & KM_CTRL) != 0) { key_handle_ctrl(&ev); } else if (((ev.mods & KM_ALT) == 0) && ((ev.mods & KM_CTRL) == 0) && (ev.mods & KM_SHIFT) != 0) { key_handle_shift(&ev); } else if ((ev.mods & (KM_CTRL | KM_ALT | KM_SHIFT)) == 0) { key_handle_unmod(&ev); } } /* Redraw as necessary. */ cursor_hide(); if (pane.rflags & REDRAW_TEXT) pane_text_display(); if (pane.rflags & REDRAW_ROW) pane_row_display(); if (pane.rflags & REDRAW_STATUS) pane_status_display(); if (pane.rflags & REDRAW_CARET) pane_caret_display(); cursor_show(); } console_clear(con); return 0; }
int main() { file_t* file; FILE* infile; char buf[256]; int result = 1; file_itr_t* itr; char* data; u32 len; uint line; if ((file = file_open("sorted.file", 1)) == NULL) { fprintf(stderr, "Failed to open sorted.file\n"); return 1; } infile = fopen("unsorted.txt", "r"); if (infile == NULL) { fprintf(stderr, "Failed to open unsorted.txt\n"); file_close(file); return 1; } line = 1; while (fgets(buf, 256, infile) != NULL) { len = strlen(buf) - 1; buf[len] = 0; if (file_insert(file, buf, len + 1) < 0) { fprintf(stderr, "file_insert failed on key %s (line %d)\n", buf, line); goto done; } line++; } fprintf(stderr, "Added %d records\n", line); if (file_sort(file) < 0) { fprintf(stderr, "failed to sort file\n"); goto done; } file_close(file); fclose(infile); if ((file = file_open("sorted.file", 0)) == NULL) { fprintf(stderr, "Failed to open sorted.file\n"); return 1; } if ((itr = file_itr_init(file)) == NULL) { fprintf(stderr, "file_itr_init failed"); goto done; } while ((result = file_itr_next(itr, &data, &len)) == 0) printf("%s\n", data); if (result < 0) { fprintf(stderr, "file_itr_next failed\n"); result = 1; } file_itr_finish(itr); done: fclose(infile); file_close(file); return result; }