char pgmread(const char *path, matrix_t *dest) { char result[MAX_TERM]; coord_t x, y, w, h; FILE *file = fopen(path, "r"); if (!file) return 0; readuntil(file, result, MAX_TERM); // identifier readuntil(file, result, MAX_TERM); // width w = atoi(result); readuntil(file, result, MAX_TERM); // height h = atoi(result); readuntil(file, result, MAX_TERM); // max gray pixel_t maxgray = atoi(result); *dest = new_matrix(w, h); dest->width = w; dest->height = h; for (y = 0; y < dest->height; ++y) for (x = 0; x < dest->width; ++x) { readuntil(file, result, MAX_TERM); dest->bytes[y][x] = 255 * atoi(result) / maxgray; } fclose(file); return 1; }
int login() { int admin = 0; char buf[64]; char username[] = "admin"; printf("username: "******"[ERROR] Permission Denied: Wrong credentials\n"); return admin; } printf("password: "******"[ERROR] Permission Denied: Wrong credentials\n"); return admin; } printf("\nWelcome, admin!\n"); admin = 1; return admin; }
static TypeDesc string_to_type (const char *s) { TypeDesc t; std::string tname = readuntil (&s, ' '); if (tname == "int") t = TypeDesc::TypeInt; if (tname == "float") t = TypeDesc::TypeFloat; if (tname == "color") t = TypeDesc::TypeColor; if (tname == "point") t = TypeDesc::TypePoint; if (tname == "vector") t = TypeDesc::TypeVector; if (tname == "normal") t = TypeDesc::TypeNormal; if (tname == "matrix") t = TypeDesc::TypeMatrix; if (tname == "string") t = TypeDesc::TypeString; if (*s == '[') { ++s; if (*s == ']') t.arraylen = -1; else t.arraylen = atoi (s); } return t; }
void OSOReaderQuery::hint (const char *hintstring) { if (m_reading_param && ! strncmp (hintstring, "%meta{", 6)) { hintstring += 6; // std::cerr << " Metadata '" << hintstring << "'\n"; std::string type = readuntil (&hintstring, ',', '}'); std::string name = readuntil (&hintstring, ',', '}'); // std::cerr << " " << name << " : " << type << "\n"; OSLQuery::Parameter p; p.name = name; p.type = string_to_type (type.c_str()); if (p.type.basetype == TypeDesc::STRING) { while (*hintstring == ' ') ++hintstring; while (hintstring[0] == '\"') { ++hintstring; p.sdefault.push_back (readuntil (&hintstring, '\"')); } } else if (p.type.basetype == TypeDesc::INT) { while (*hintstring == ' ') ++hintstring; while (*hintstring && *hintstring != '}') { p.idefault.push_back (atoi (hintstring)); readuntil (&hintstring, ',', '}'); } } else if (p.type.basetype == TypeDesc::FLOAT) { while (*hintstring == ' ') ++hintstring; while (*hintstring && *hintstring != '}') { p.fdefault.push_back (atof (hintstring)); readuntil (&hintstring, ',', '}'); } } m_query.m_params[m_query.nparams()-1].metadata.push_back (p); return; } if (m_reading_param && ! strncmp (hintstring, "%structfields{", 14)) { hintstring += 14; OSLQuery::Parameter ¶m (m_query.m_params[m_query.nparams()-1]); while (*hintstring) { std::string afield = readuntil (&hintstring, ',', '}'); param.fields.push_back (afield); } return; } if (m_reading_param && ! strncmp (hintstring, "%struct{", 8)) { hintstring += 8; if (*hintstring == '\"') // skip quote ++hintstring; OSLQuery::Parameter ¶m (m_query.m_params[m_query.nparams()-1]); param.structname = readuntil (&hintstring, '\"', '}'); return; } // std::cerr << "Hint '" << hintstring << "'\n"; }
void return_movie() { char buf[256]; unsigned int movie_id, num_movies = 0; movie_node_t *node; /* Present the list of rented movies with index */ printf("\nMovies (Rented)\n--------------\n"); for (node = movies_rented; node != NULL; node = node->next) { num_movies++; node->movie->print_info(num_movies, node->movie); } printf("--------------\n%d movie(s)\n", num_movies); if (num_movies == 0) { printf("[ERROR] All the movies are in our inventory.\n"); return; } /* Get and validate the index */ while (1) { printf("Enter movie id: "); if (readuntil(STDIN, buf, sizeof(buf), '\n') < 0) return; movie_id = strtoul(buf, NULL, 10); //if (movie_id >= 1 && movie_id <= g_num_movies) // break; if (movie_id >= 1 && movie_id <= num_movies) break; printf("[ERROR] Invalid movie id. Try again.\n"); } /* Remove the movie entry in rental list */ movie_node_t *tmp = movie_find_by_id(movies_rented, movie_id); if (tmp == NULL) { /* Shouldn't happen */ printf("Sorry, we have some issues here. Please try again later.\n"); return; } if (movie_delete(&movies_rented, movie_id) != 0) { /* Shouldn't happen */ printf("[ERROR] Failed to return the movie. Please try again.\n"); return; } printf("Successfully returned [%s]! Thank you!\n", tmp->movie->title); }
void rent_movie() { movie_node_t *node; unsigned int movie_id, num_movies = 0; char buf[256]; /* Present the full movie list with index */ printf("\nMovies (Full)\n--------------\n"); for (node = movies_full; node != NULL; node = node->next) { num_movies++; node->movie->print_info(num_movies, node->movie); } printf("--------------\n%d movie(s)\n", num_movies); if (num_movies == 0) { /* Shouldn't really happen with successful init */ printf("[ERROR] Movie list is empty. Please try again later.\n"); return; } /* Get and validate the index */ while (1) { printf("Enter movie id: "); if (readuntil(STDIN, buf, sizeof(buf), '\n') < 0) return; movie_id = strtoul(buf, NULL, 10); if (movie_id >=1 && movie_id <= num_movies) break; printf("[ERROR] Invalid movie id. Try again.\n"); } /* Check if the movie isn't rented already */ movie_node_t *tmp = movie_find_by_id(movies_full, movie_id); movie_t *movie = tmp->movie; tmp = movie_find(movies_rented, movie->title); if (tmp) { printf("Sorry, [%s] is already rented at this time. Please try again later.\n", tmp->movie->title); return; } /* If not in rental list, insert it into rental list */ tmp = movie_find_by_id(movies_full, movie_id); if (movie_add(&movies_rented, tmp->movie) != 0) printf("[ERROR] Failed to rent. Please try again later.\n"); else printf("Successfully rented [%s]! Enjoy!\n", tmp->movie->title); }
void remove_movie() { char buf[256]; unsigned int num_movies = 0, movie_id; movie_node_t *node; /* Movie list (full) */ printf("\nMovies (Full)\n--------------\n"); for (node = movies_full; node != NULL; node = node->next) { num_movies++; node->movie->print_info(num_movies, node->movie); } printf("--------------\n%d movie(s)\n", num_movies); if (num_movies == 0) { printf("[ERROR] Movie list is empty.\n"); return; } /* Get and validate the index */ while (1) { printf("Enter movie id: "); if (readuntil(STDIN, buf, sizeof(buf), '\n') < 0) return; movie_id = strtoul(buf, NULL, 10); if (movie_id >= 1 && movie_id <= num_movies) break; printf("[ERROR] Invalid movie id. Try again.\n"); } node = movie_find_by_id(movies_full, movie_id); movie_t *movie = node->movie; #if PATCHED if (movie_delete(&movies_full, movie_id) != 0 || movie_delete(&movies_rented, movie_id) != 0) #else if (movie_delete(&movies_full, movie_id) != 0) #endif printf("[ERROR] Failed to remove.\n"); else { g_num_movies--; free_movie(movie); printf("Successfully removed the movie!\n"); } }
int main() { char buf[80]; printf("Enter the password.\n"); readuntil(STDIN_FILENO, buf, 80, '\n'); // meh. if (strlen(buf) != 30) { puts("Wrong! :("); return; } thunk *t = make_thunk(verify); thunk *a = make_thunk(id); a->value = buf; a->computed = 1; t->arguments = make_args(a); if (t->compute(t)) { puts("Congratulations! :)"); } else { puts("Wrong! :("); } }
void readuntil(FILE *file, char *dest, int max) { if (feof(file)) { dest[0] = 0; return; } char ch = 0; int i = 0; while (i < max && ch != ' ' && ch != '\t' && ch != '\r' && ch != '\n') { if (ch > 0) { dest[i++] = ch; } ch = fgetc(file); } dest[i] = 0; if (i == 0) readuntil(file, dest, max); }
int main() { int admin_mode = 0; char buf[4096]; char welcome[] = "========= Movie Rental Service v0.1 ========="; char menu[] = "\n1. List movies\n2. Rent movie\n3. Return movie\n4. Admin mode\n5. Exit\n\nChoice: "; char admin_menu[] = "\n1. Add movie\n2. Remove movie\n3. Update movie\n4. Quit admin mode\n\nChoice: "; printf(welcome); initialize(); printf("============================================="); while (1) { if (admin_mode) { printf(admin_menu); if (readuntil(STDIN, buf, sizeof(buf), '\n') < 0) quit(); switch (buf[0]) { case '1': add_movie(); break; case '2': remove_movie(); break; case '3': update_movie(); break; case '4': admin_mode = 0; break; default: printf("[ERROR] Invalid menu. Please select again.\n"); break; } } else { printf(menu); if (readuntil(STDIN, buf, sizeof(buf), '\n') < 0) quit(); switch (buf[0]) { case '1': list_movies(); break; case '2': rent_movie(); break; case '3': return_movie(); break; case '4': admin_mode = login(); break; case '5': quit(); break; default: printf("[ERROR] Invalid menu. Please select again.\n"); break; } } } return 0; }
void update_movie() { unsigned int movie_id, num_movies = 0; char *new_title, *new_desc; char buf[1024]; movie_node_t *node; movie_t *movie; /* Present the full list with index */ printf("\nMovies (Full)\n--------------\n"); for (node = movies_full; node != NULL; node = node->next) { num_movies++; node->movie->print_info(num_movies, node->movie); } printf("--------------\n%d movie(s)\n", num_movies); if (num_movies == 0) { printf("[ERROR] Nothing to update.\n"); return; } /* Get and validate the index */ while (1) { printf("Enter movie id: "); if (readuntil(STDIN, buf, sizeof(buf), '\n') < 0) return; movie_id = strtoul(buf, NULL, 10); if (movie_id >= 1 && movie_id <= num_movies) break; printf("[ERROR] Invalid movie id. Try again.\n"); } node = movie_find_by_id(movies_full, movie_id); movie = node->movie; printf("\nUpdate a movie\n--------------\nJust leave it empty to keep the old value.\n"); printf("Enter new title (current: [%s]): ", movie->title); if (readuntil(STDIN, buf, sizeof(buf), '\n') < 0) return; if (cgc_strlen(buf) != 0) { new_title = (char *) malloc(cgc_strlen(buf) + 1); if (new_title == NULL) goto fail; strcpy(new_title, buf); free(movie->title); movie->title = new_title; } printf("Enter new description (current: [%s]): ", movie->desc); if (readuntil(STDIN, buf, sizeof(buf), '\n') < 0) return; if (cgc_strlen(buf) != 0) { new_desc = (char *) malloc(cgc_strlen(buf) + 1); if (new_desc == NULL) goto fail; strcpy(new_desc, buf); free(movie->desc); movie->desc = new_desc; } while (1) { printf("Enter new year (1800-2015) (current: [%d]): ", movie->year); if (readuntil(STDIN, buf, sizeof(buf), '\n') < 0) goto fail; if (cgc_strlen(buf) != 0) { int new_year = strtoul(buf, NULL, 10); if (new_year >= 1800 && new_year <= 2015) { movie->year = new_year; break; } printf("[ERROR] Invalid year. Try again.\n"); } else break; } while (1) { printf("Enter new review score (0-100) (current: [%d/100]: ", movie->score); if (readuntil(STDIN, buf, sizeof(buf), '\n') < 0) goto fail; if (cgc_strlen(buf) != 0) { int new_score = strtoul(buf, NULL, 10); if (new_score >= 0 && new_score <= 100) { movie->score = new_score; break; } printf("[ERROR] Invalid rating. Try again.\n"); } else break; } int finish = 0; while (!finish) { printf("Select a genre (current: [%s])\n 1. Action\n 2. Romance\n 3. Comeda\n 4. Horror\n 5. Other\nChoice: ", movie_g2s(movie->genre)); if (readuntil(STDIN, buf, sizeof(buf), '\n') < 0) goto fail; if (cgc_strlen(buf) != 0) { unsigned int genre = strtoul(buf, NULL, 10); finish = 1; switch (genre) { case 1: movie->genre = G_ACTION; break; case 2: movie->genre = G_ROMANCE; break; case 3: movie->genre = G_COMEDY; break; case 4: movie->genre = G_HORROR; break; case 5: movie->genre = G_OTHER; break; default: finish = 0; printf("[ERROR] Invalid genre. Try again.\n"); break; } } else break; } finish = 0; while (!finish) { printf("Select a film rating (current: [%s]\n 1. G\n 2. PG\n 3. PG-13\n 4. R\n 5. Unknown\nChoice: ", movie_r2s(movie->rating)); if (readuntil(STDIN, buf, sizeof(buf), '\n') < 0) goto fail; if (cgc_strlen(buf) != 0) { unsigned int rating = strtoul(buf, NULL, 10); finish = 1; switch (rating) { case 1: movie->rating = R_G; break; case 2: movie->rating = R_PG; break; case 3: movie->rating = R_PG13; break; case 4: movie->rating = R_R; break; case 5: movie->rating = R_UNKNOWN; break; default: finish = 0; printf("[ERROR] Invalid film rating. Try again.\n"); break; } } else break; } printf("Successfully updated the movie information!\n"); return; fail: if (new_title) free(new_title); if (new_desc) free(new_desc); if (movie) free(movie); }
void add_movie() { char buf[1024]; movie_t *movie; movie = (movie_t *) malloc(sizeof(movie_t)); if (!movie) return; printf("Add a movie\n--------------\n"); printf("Enter Title: "); if (readuntil(STDIN, buf, sizeof(buf), '\n') < 0) goto fail; movie->title = (char *) malloc(cgc_strlen(buf) + 1); if (movie->title == NULL) goto fail; strcpy(movie->title, buf); printf("Enter Description: "); if (readuntil(STDIN, buf, sizeof(buf), '\n') < 0) goto fail; movie->desc = (char *) malloc(cgc_strlen(buf) + 1); if (movie->desc == NULL) goto fail; strcpy(movie->desc, buf); while (1) { printf("Enter Year (1800-2015): "); if (readuntil(STDIN, buf, sizeof(buf), '\n') < 0) goto fail; movie->year = strtoul(buf, NULL, 10); if (movie->year >= 1800 && movie->year <= 2015) break; printf("[ERROR] Invalid year. Try again.\n"); } while (1) { printf("Enter Review score (0-100): "); if (readuntil(STDIN, buf, sizeof(buf), '\n') < 0) goto fail; movie->score = strtoul(buf, NULL, 10); if (movie->score >= 0 && movie->score <= 100) break; printf("[ERROR] Invalid rating. Try again.\n"); } int finish = 0; while (!finish) { printf("Select a genre\n 1. Action\n 2. Romance\n 3. Comeda\n 4. Horror\n 5. Other\nChoice: "); if (readuntil(STDIN, buf, sizeof(buf), '\n') < 0) goto fail; unsigned int genre = strtoul(buf, NULL, 10); finish = 1; switch (genre) { case 1: movie->genre = G_ACTION; break; case 2: movie->genre = G_ROMANCE; break; case 3: movie->genre = G_COMEDY; break; case 4: movie->genre = G_HORROR; break; case 5: movie->genre = G_OTHER; break; default: finish = 0; printf("[ERROR] Invalid genre. Try again.\n"); break; } } finish = 0; while (!finish) { printf("Select a film rating\n 1. G\n 2. PG\n 3. PG-13\n 4. R\n 5. Unknown\nChoice: "); if (readuntil(STDIN, buf, sizeof(buf), '\n') < 0) goto fail; unsigned int rating = strtoul(buf, NULL, 10); finish = 1; switch (rating) { case 1: movie->rating = R_G; break; case 2: movie->rating = R_PG; break; case 3: movie->rating = R_PG13; break; case 4: movie->rating = R_R; break; case 5: movie->rating = R_UNKNOWN; break; default: finish = 0; printf("[ERROR] Invalid film rating. Try again.\n"); break; } } //movie->id = ++g_num_movies; movie->print_info = &print_movie_detail; if (movie_add(&movies_full, movie) != 0) { printf("[ERROR] Failed to add a movie.\n"); goto fail; } return; fail: if (movie) { if (movie->desc) free(movie->desc); if (movie->title) free(movie->title); free(movie); } return; }
void OSOReaderToMaster::hint (string_view hintstring) { std::string h (hintstring); // FIXME -- use string_view ops here if (extract_prefix (h, "%filename{\"")) { m_sourcefile = readuntil (h, "\""); return; } if (extract_prefix (h, "%line{")) { m_sourceline = atoi (h.c_str()); return; } if (extract_prefix (h, "%structfields{")) { ASSERT (m_master->m_symbols.size() && "structfields hint but no sym"); Symbol &sym (m_master->m_symbols.back()); StructSpec *structspec = sym.typespec().structspec(); if (structspec->numfields() == 0) { while (1) { std::string afield = readuntil (h, ",}", true); if (! afield.length()) break; // std::cerr << " struct field " << afield << "\n"; structspec->add_field (TypeSpec(), ustring(afield)); } } return; } if (extract_prefix (h, "%mystructfield{")) { ASSERT (m_master->m_symbols.size() && "mystructfield hint but no sym"); Symbol &sym (m_master->m_symbols.back()); sym.fieldid (atoi(h.c_str()+15)); return; } if (extract_prefix (h, "%read{")) { ASSERT (m_master->m_symbols.size() && "read hint but no sym"); Symbol &sym (m_master->m_symbols.back()); int first, last; sscanf (h.c_str(), "%d,%d", &first, &last); sym.set_read (first, last); return; } if (extract_prefix (h, "%write{")) { ASSERT (m_master->m_symbols.size() && "write hint but no sym"); Symbol &sym (m_master->m_symbols.back()); int first, last; sscanf (h.c_str(), "%d,%d", &first, &last); sym.set_write (first, last); return; } if (extract_prefix(h, "%argrw{")) { const char* str = h.c_str(); ASSERT(*str == '\"'); str++; // skip open quote size_t i = 0; for (; *str != '\"'; i++, str++) { ASSERT(*str == 'r' || *str == 'w' || *str == 'W' || *str == '-'); m_master->m_ops.back().argwrite(i, *str == 'w' || *str =='W'); m_master->m_ops.back().argread(i, *str == 'r' || *str =='W'); } ASSERT(m_nargs == i); // Fix old bug where oslc forgot to mark getmatrix last arg as write static ustring getmatrix("getmatrix"); if (m_master->m_ops.back().opname() == getmatrix) m_master->m_ops.back().argwrite(m_nargs-1, true); } if (extract_prefix(h, "%argderivs{")) { while (1) { std::string afield = readuntil (h, ",}", true); if (! afield.length()) break; int arg = atoi (afield.c_str()); if (arg >= 0) m_master->m_ops.back().argtakesderivs (arg, true); } } if (extract_prefix (h, "%meta{") && m_master->m_symbols.size()) { Symbol &sym (m_master->m_symbols.back()); int lockval = -1; int ok = sscanf (h.c_str(), " int , lockgeom , %d", &lockval); if (ok) sym.lockgeom (lockval); } }