int GeUser::load(char* filename) { std::ifstream fp; int type; int end_found = 0; char dummy[40]; strcpy(fname, filename); if (!check_file(filename)) return USER__FILEOPEN; fp.open(filename); if (!fp) return USER__FILEOPEN; for (;;) { fp >> type; switch (type) { case user_eData_GeUser: break; case user_eData_GeUserVersion: fp.get(); fp.getline(version, sizeof(version)); if (!streq(version, user_cVersion)) { return USER__DBVERSION; } break; case user_eData_GeNextId: fp >> next_id; break; case user_eData_System: load_system(fp); break; case user_eData_End: end_found = 1; break; default: std::cout << "GeUser:open syntax error\n"; fp.getline(dummy, sizeof(dummy)); } if (end_found) break; } fp.close(); return 1; }
int SystemList::load(std::ifstream& fp) { int type; int end_found = 0; char dummy[40]; for (;;) { fp >> type; switch (type) { case user_eData_SystemName: fp.get(); fp.getline(name, sizeof(name)); break; case user_eData_SystemLevel: fp >> level; break; case user_eData_SystemAttributes: fp >> attributes; break; case user_eData_SystemId: fp >> id; break; case user_eData_SystemDescription: fp.get(); fp.getline(description, sizeof(description)); break; case user_eData_User: load_user(fp); break; case user_eData_System: load_system(fp); break; case user_eData_End: end_found = 1; break; default: std::cout << "System:open syntax error\n"; fp.getline(dummy, sizeof(dummy)); } if (end_found) break; } return 1; }
int main(int argc, char** argv) { if(argc < 2 || argc > 3) usage(argv[0]); FILE* outfile; if(argc < 3) outfile = stdout; else { outfile = fopen(argv[2], "w"); if(outfile == NULL) { perror("Error opening output file"); return EXIT_FAILURE; } } FILE* infile = fopen(argv[1], "r"); if(infile == NULL) { fclose(outfile); perror("Error opening input file"); return EXIT_FAILURE; } System* system = load_system(infile); if(system == NULL) { fclose(outfile); fclose(infile); printf("Loading input file failed.\n"); return EXIT_FAILURE; } init_simulation(system); simulate(outfile, system); free(system); fclose(outfile); fclose(infile); }
int /* O - 1 if successful, 0 on error */ serverLoadConfiguration( const char *directory) /* I - Configuration directory */ { cups_dir_t *dir; /* Directory pointer */ cups_dentry_t *dent; /* Directory entry */ char filename[1024], /* Configuration file/directory */ iconname[1024], /* Icon file */ resource[1024], /* Resource path */ *ptr; /* Pointer into filename */ server_printer_t *printer; /* Printer */ ipp_t *attrs; /* Printer attributes */ char *authtype, /* AuthType value, if any */ *command, /* Command value, if any */ *device_uri, /* DeviceURI value, if any */ *make, /* Make value, if any */ *model, /* Model value, if any */ *proxy_user; /* ProxyUser value, if any */ /* * First read the system configuration file, if any... */ snprintf(filename, sizeof(filename), "%s/system.conf", directory); if (!load_system(filename)) return (0); if (!serverFinalizeConfiguration()) return (0); /* * Then see if there are any print queues... */ snprintf(filename, sizeof(filename), "%s/print", directory); if ((dir = cupsDirOpen(filename)) != NULL) { serverLog(SERVER_LOGLEVEL_INFO, "Loading printers from \"%s\".", filename); while ((dent = cupsDirRead(dir)) != NULL) { if ((ptr = dent->filename + strlen(dent->filename) - 5) >= dent->filename && !strcmp(ptr, ".conf")) { /* * Load the conf file, with any associated icon image. */ serverLog(SERVER_LOGLEVEL_INFO, "Loading printer from \"%s\".", dent->filename); snprintf(filename, sizeof(filename), "%s/print/%s", directory, dent->filename); *ptr = '\0'; snprintf(iconname, sizeof(iconname), "%s/print/%s.png", directory, dent->filename); authtype = command = device_uri = make = model = proxy_user = NULL; if ((attrs = serverLoadAttributes(filename, &authtype, &command, &device_uri, &make, &model, &proxy_user)) != NULL) { snprintf(resource, sizeof(resource), "/ipp/print/%s", dent->filename); if ((printer = serverCreatePrinter(resource, dent->filename, NULL, make, model, access(iconname, R_OK) ? NULL : iconname, NULL, 0, 0, 0, 0, attrs, command, device_uri, proxy_user)) == NULL) continue; if (!Printers) Printers = cupsArrayNew((cups_array_func_t)compare_printers, NULL); cupsArrayAdd(Printers, printer); } } else if (!strstr(dent->filename, ".png")) serverLog(SERVER_LOGLEVEL_INFO, "Skipping \"%s\".", dent->filename); } cupsDirClose(dir); } /* * Finally, see if there are any 3D print queues... */ snprintf(filename, sizeof(filename), "%s/print3d", directory); if ((dir = cupsDirOpen(filename)) != NULL) { serverLog(SERVER_LOGLEVEL_INFO, "Loading 3D printers from \"%s\".", filename); while ((dent = cupsDirRead(dir)) != NULL) { if ((ptr = dent->filename + strlen(dent->filename) - 5) >= dent->filename && !strcmp(ptr, ".conf")) { /* * Load the conf file, with any associated icon image. */ serverLog(SERVER_LOGLEVEL_INFO, "Loading 3D printer from \"%s\".", dent->filename); snprintf(filename, sizeof(filename), "%s/print3d/%s", directory, dent->filename); *ptr = '\0'; snprintf(iconname, sizeof(iconname), "%s/print3d/%s.png", directory, dent->filename); authtype = command = device_uri = make = model = proxy_user = NULL; if ((attrs = serverLoadAttributes(filename, &authtype, &command, &device_uri, &make, &model, &proxy_user)) != NULL) { snprintf(resource, sizeof(resource), "/ipp/print3d/%s", dent->filename); if ((printer = serverCreatePrinter(resource, dent->filename, NULL, make, model, access(iconname, R_OK) ? NULL : iconname, NULL, 0, 0, 0, 0, attrs, command, device_uri, proxy_user)) == NULL) continue; if (!Printers) Printers = cupsArrayNew((cups_array_func_t)compare_printers, NULL); cupsArrayAdd(Printers, printer); } } else if (!strstr(dent->filename, ".png")) serverLog(SERVER_LOGLEVEL_INFO, "Skipping \"%s\".", dent->filename); } cupsDirClose(dir); } return (1); }
int main(int argc, char** argv) { if(argc != 2) usage(argv[0]); FILE* infile = fopen(argv[1], "r"); if(infile == NULL) { perror("Error opening input file"); return EXIT_FAILURE; } System* sys = load_system(infile); if(sys == NULL) { printf("Loading input file failed\n"); return EXIT_FAILURE; } init_simulation(sys); state.sys = sys; state.views = malloc(sys->nplanets * sizeof(PlanetView)); for(int i = 0; i < sys->nplanets; i++) state.views[i].radius = pow(sys->planets[i].mass / DENSITY_FACTOR, 1.0f/3.0f); state.scale = 1.0f; Vector* fst_pos = &sys->planets[0].position; vector_copy(state.pos, *fst_pos); state.pos[1] += 1.1f*get_planet_radius(0); state.pos[0] -= get_planet_radius(0); state.rot_x = 90.0f; state.locked_planet = -1; state.hours_per_sec = DEFAULT_SIMULATION_SPEED; state.time_step = sys->time_step; state.paused = true; state.trails_enabled = true; if(SDL_Init(SDL_INIT_VIDEO) < 0) die("SDL initialization failed"); atexit(SDL_Quit); const SDL_VideoInfo* videoInfo = SDL_GetVideoInfo(); if(!videoInfo) die("Could not get video information"); int videoFlags = SDL_OPENGL | SDL_HWPALETTE | SDL_RESIZABLE | SDL_HWSURFACE | SDL_HWACCEL; SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4); surface = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, videoFlags); if(!surface) { printf("Surface creation failed, trying to disable anti-aliasing...\n"); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0); surface = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, videoFlags); } if(!surface) die("Changing video mode failed"); init_gl(); init_viewport(); SDL_ShowCursor(0); SDL_WM_GrabInput(SDL_GRAB_ON); SDL_Event event; while(SDL_PollEvent(&event)) ; /* ignore spurious mouse events at startup */ bool window_is_active = true; int step = 0; while (true) { Uint32 next_update = SDL_GetTicks() + FRAME_INTERVAL; while (SDL_PollEvent(&event)) { switch(event.type) { case SDL_ACTIVEEVENT: window_is_active = event.active.gain; break; case SDL_VIDEORESIZE: surface = SDL_SetVideoMode(event.resize.w, event.resize.h, SCREEN_BPP, videoFlags); if(!surface) die("Lost video surface during resize"); init_viewport(); break; case SDL_KEYDOWN: handle_keypress(&event.key.keysym); break; case SDL_MOUSEMOTION: handle_mouse(&event.motion); break; case SDL_QUIT: goto out; default: break; } } update(); if(window_is_active) { draw_scene(); glFlush(); SDL_GL_SwapBuffers(); } if(!state.paused) { for(int i = 0; i < (state.hours_per_sec * 3600.0f / FRAME_INTERVAL) / state.time_step; i++) { if((step % TRAILS_INTERVAL) == 0) update_trails(); simulate_one_step(sys, step++, state.time_step); } } Sint32 delta = next_update - SDL_GetTicks(); if(delta > 0) SDL_Delay(delta); } out: return 0; }