void read_server_book (const char *data, int len) { char buffer[8192]; book *b; page *p; int l = SDL_SwapLE16(*((Uint16*)(data+4))); int idx; if ( l >= sizeof (buffer) ) // Safer l = sizeof (buffer) - 1; memcpy (buffer, data+6, l); buffer[l] = '\0'; b = get_book (SDL_SwapLE16 (*((Uint16*)(data+1)))); if (b == NULL) b = create_book (buffer, data[0], SDL_SwapLE16 (*((Uint16*)(data+1)))); b->server_pages = data[3]; b->have_server_pages++; p=add_page(b);//Will create a page if pages is not found. idx = l + 6; while (idx <= len) { l = SDL_SwapLE16 (*((Uint16*)(&data[idx+1]))); if ( l >= sizeof (buffer) ) // Safer. l = sizeof (buffer) - 1; memcpy (buffer, &data[idx+3], l); buffer[l]=0; switch (data[idx]) { case _TEXT: p=add_str_to_page(buffer,_TEXT,b,p); break; case _AUTHOR: p=add_str_to_page(buffer,_AUTHOR,b,p); break; case _TITLE: p=add_str_to_page(buffer,_TITLE,b,p); break; case _IMAGE: p=add_image_from_server(buffer, b, p); break; case _PAGE: //p=add_page(b); break; } idx += l + 3; } b->active_page += b->pages_to_scroll; b->pages_to_scroll = 0; if (b) display_book_window (b); // Otherwise there's no point... }
/* resize the db and add a book struct with user input * this lives here since it uses static db functions */ void user_add_book(void) { Book* B; long id; int year; char title[257]; char name[112]; char pub[41]; /* get user input */ fprintf(stdout, "Title of the Book? "); clear_stream(); if (! fgets(title, sizeof(title) - 1, stdin)) /* checking for error */ goto error; chomp(title); printf("Author? "); if (! fgets(name, sizeof(name) - 1, stdin)) goto error; chomp(name); printf("ID (10 numbers)? "); if (scanf("%ld", &id) == 0) goto error; /* clearing stream in another way due to scanf */ while(getchar() != '\n'); printf("When was it published? "); if (scanf("%d", &year) == 0) goto error; while(getchar() != '\n'); printf("Publisher company? "); if (! fgets(pub, sizeof(pub) - 1, stdin)) goto error; chomp(pub); /* create and call function to add it */ B = create_book(id, title, year, pub); split_create_authors(B, name); if (!add_book(B)) { printf("Book node added to database\n"); return; } error: printf("Book creation aborted\n"); return; }
book * parse_book(xmlNode *in, char * title, int type, int id) { xmlNode * cur; book * b=create_book(title, type, id); for(cur=in;cur;cur=cur->next){ if(cur->type == XML_ELEMENT_NODE){ if(!xmlStrcasecmp(cur->name,(xmlChar*)"page")){ add_xml_page(cur->children,b); } } } return b; }
/* load and initialize db from file first menu option function */ void init_db(const char *file) { /* temp buffers and descriptors */ FILE *fd; char given_id[11]; char line[256+56+56+4+40]; char book_name[256]; char writer[56*2]; char the_year[4]; char pub_name[40]; fd = fopen(file, "r"); if (!fd) fatal("Is there a datafile?"); /* get the total number of books */ if (!fgets(line, 8, fd)) fatal("while reading datafile"); long n = atol(line); if (!n) fatal("no sum of books at the first line"); /* allocate the main database */ db.arr = smalloc(sizeof(Book)*n); db.numberOfBooks = 0; /* start parsing the file, delimiter is ';' */ memset(line, 0, sizeof(line)); printf("Loading file...\n"); while (fgets(line, sizeof(Book), fd) != NULL) { /* erase the previous data */ memset(given_id, 0, sizeof(given_id)); memset(book_name, 0, sizeof(book_name)); memset(writer, 0, sizeof(writer)); memset(the_year, 0, sizeof(the_year)); memset(pub_name, 0, sizeof(pub_name)); /* scanf is like magic */ sscanf(line, "\"%[0-9]\";\"%[0-9a-zA-Z.:!'?,)( ]\";\"%[0-9a-zA-Z.:',!?)( ]\";\"%[0-9]\";\"%[0-9a-zA-Z.:'!,?)( ]\"", given_id, book_name, writer, the_year, pub_name); /* the symbols in the scanf sequence are the only ones we accept to * be in the strings and we don't want any weird stuff because they * break the encoding, so we're dropping any books that apper to have * no authors */ if (strlen(writer) == 0) { memset(line, 0, sizeof(line)); continue; } /* set defaults here */ if (strlen(the_year) == 0) strcpy(the_year, "0000"); /* check for duplicates */ if (btraverse(atol(given_id), 1) != -1) continue; /* Done parsing. Creating book */ Book *B; B = create_book(atol(given_id), book_name, atoi(the_year), pub_name); /* creating authors */ split_create_authors(B, writer); /* put it in the actual database */ db.arr[db.numberOfBooks] = *B; db.numberOfBooks++; /* clean */ memset(line, 0, sizeof(line)); /* we copy the data above, so we don't need that */ free(B); } fclose(fd); }