bool push_file(char *file_path) { FILE *fp; // If the file stack is full, do nothing. if (next_file_index == FILE_STACK_SIZE) return(false); // Add the file to the top of the stack. top_file_ptr = &file_stack[next_file_index]; next_file_index++; strcpy(top_file_ptr->file_path, file_path); top_file_ptr->file_buffer = NULL; top_file_ptr->line_no = 0; // Open the file for reading in binary mode. if ((fp = fopen(file_path, "rb")) == NULL) { pop_file(); return(false); } // Seek to the end of the file to determine it's size, then seek back to // the beginning of the file. fseek(fp, 0, SEEK_END); top_file_ptr->file_size = ftell(fp); fseek(fp, 0, SEEK_SET); // Allocate the file buffer, and read the contents of the file into it. if ((top_file_ptr->file_buffer = new char[top_file_ptr->file_size]) == NULL || fread(top_file_ptr->file_buffer, top_file_ptr->file_size, 1, fp) == 0) { fclose(fp); pop_file(); return(false); } // Close the file. fclose(fp); // Initialise the file position and file buffer pointer. top_file_ptr->file_position = 0; top_file_ptr->file_buffer_ptr = top_file_ptr->file_buffer; return(true); }
bool push_ZIP_file(char *file_path) { unz_file_info info; // If the file stack is full, do nothing. if (next_file_index == FILE_STACK_SIZE) return(false); // Add the file to the top of the stack. top_file_ptr = &file_stack[next_file_index]; next_file_index++; strcpy(top_file_ptr->file_path, file_path); top_file_ptr->file_buffer = NULL; top_file_ptr->line_no = 0; // Attempt to locate the file in the ZIP archive. if (unzLocateFile(unzip_handle, file_path, 0) != UNZ_OK) { pop_file(); return(false); } // Get the uncompressed size of the file, and allocate the file buffer. unzGetCurrentFileInfo(unzip_handle, &info, NULL, 0, NULL, 0, NULL, 0); top_file_ptr->file_size = info.uncompressed_size; if ((top_file_ptr->file_buffer = new char[top_file_ptr->file_size]) == NULL) { pop_file(); return(false); } // Open the file, read it into the file buffer, then close the file. unzOpenCurrentFile(unzip_handle); unzReadCurrentFile(unzip_handle, top_file_ptr->file_buffer, top_file_ptr->file_size); unzCloseCurrentFile(unzip_handle); // Initialise the file position and file buffer pointer. top_file_ptr->file_position = 0; top_file_ptr->file_buffer_ptr = top_file_ptr->file_buffer; return(true); }
static void shutting_down(t_server *serv, t_file *file, char *ret, int s) { remove(ret); serv->files = pop_file(serv->files, file->from, file->to); close(s); serv->fd_type[s] = FD_FREE; }
static void get_line(void) { int r; again: anthy_free_line(); g_ps.line_num ++; r = get_line_in(); if (r == -1){ /* EOF等でこれ以上読めん */ if (g_ps.cur_fpp > 0) { pop_file(); goto again; }else{ return ; } } if (g_ps.nr_token < 1) { goto again; } if (!strcmp(g_ps.tokens[0], "\\include")) { proc_include(); goto again; }else if (!strcmp(g_ps.tokens[0], "\\eof")) { if (g_ps.cur_fpp > 0) { pop_file(); goto again; }else{ anthy_free_line(); return ; } } if (g_ps.tokens[0][0] == '#'){ goto again; } }
void pop_all_files(void) { while (top_file_ptr) pop_file(); }