/** * Throws: * std::bad_alloc if no memory. * NSPRException if NSPR error. * ParseException upon parse error. */ Http::Response::Response(Log::ModuleId logModule, Connection& conn) : httpStatus(INVALID), cookieList(), extraHdrs(), bodyPtr(NULL), bodyLen(0) { am_status_t status; status = readAndParse(logModule, conn); if (AM_SUCCESS != status) { if (AM_NO_MEMORY == status) { throw std::bad_alloc(); } else if (AM_NSPR_ERROR == status) { throw NSPRException("Http::Response()", "Connection::receiveData"); } else { throw ParseException(status); } } }
int Read_SSD_Scene(char *fname, SCENE *ascene, char *saved_fname) { char keyword[MAXLINELENGTH], arg0[MAXLINELENGTH], arg1[MAXLINELENGTH], arg2[MAXLINELENGTH], arg3[MAXLINELENGTH]; char arg4[MAXLINELENGTH]; FILE *fp; /* We first set all the default values */ RGB_COLOR fcolor, vcolor; int ind, ii, num_ver, key_id, key_id1, npara; ascene->screen_w = 600; ascene->screen_h = 400; /* The default color is white */ ascene->bcolor.rgba[0] = 1.0; ascene->bcolor.rgba[1] = 1.0; ascene->bcolor.rgba[2] = 1.0; ascene->bcolor.rgba[3] = 1.0; fcolor.rgba[0] = 0.0; fcolor.rgba[1] = 0.0; fcolor.rgba[2] = 0.0; fcolor.rgba[3] = 1.0; ascene->nlines = 0; ascene->npolylines = 0; ascene->ntriangles = 0; fp = fopen(fname,"rb"); if (fp == NULL) { fprintf(stderr,"%s:%d: Can not open SSD file <%s>.\n", __FILE__, __LINE__, fname); return -1; } while (readAndParse(fp, keyword, arg0, arg1, arg2, arg3, arg4) > 0) { if (keyword[0] == '\0') { /* We simply all blank lines */ continue; } key_id = match_Keyword(keyword, &npara); switch(key_id) { case LINE_KEY: ascene->nlines++; break; case POLYLINE_KEY: ascene->npolylines++; break; case TRIANGLE_KEY: ascene->ntriangles++; break; } } printf("There are %d lines, %d polylines, and %d triangles in %s.\n", ascene->nlines, ascene->npolylines, ascene->ntriangles, fname); /* We rewind the file to the very beginning to read the file again */ rewind(fp); ascene->lines = (LINE *)malloc(sizeof(LINE) * ascene->nlines); ascene->polylines = (POLYLINE *)malloc(sizeof(POLYLINE) * ascene->npolylines); ascene->triangles = (TRIANGLE *)malloc(sizeof(TRIANGLE) * ascene->ntriangles); ascene->nlines = 0; ascene->npolylines = 0; ascene->ntriangles = 0; while (readAndParse(fp, keyword, arg0, arg1, arg2, arg3, arg4) > 0) { if (keyword[0] == '\0') { /* We simply all blank lines */ continue; } key_id = match_Keyword(keyword, &npara); switch(key_id) { case SCREEN_KEY: ascene->screen_w = atoi(arg0); ascene->screen_h = atoi(arg1); ascene->bcolor.rgba[0] = atoi(arg2)/255.0; ascene->bcolor.rgba[1] = atoi(arg3)/255.0; ascene->bcolor.rgba[2] = atoi(arg4)/255.0; ascene->bcolor.rgba[3] = 1.0; break; case COLOR_KEY: /* We read the color */ fcolor.rgba[0] = atoi(arg0)/255.0; fcolor.rgba[1] = atoi(arg1)/255.0; fcolor.rgba[2] = atoi(arg2)/255.0; break; case LINE_KEY: ind = ascene->nlines; ascene->lines[ind].width = atof(arg0); /* We set the default colors */ vcolor = fcolor; num_ver = 0; while (readAndParse(fp, keyword, arg0, arg1, arg2, arg3, arg4) > 0) { key_id1 = match_Keyword(keyword, &npara); switch(key_id1) { case VERTEX_KEY: ascene->lines[ind].vertices[num_ver].xyzw[0] = atof(arg0); ascene->lines[ind].vertices[num_ver].xyzw[1] = atof(arg1); ascene->lines[ind].vertices[num_ver].xyzw[2] = atof(arg2); memcpy(ascene->lines[ind].vertices[num_ver].rgba, vcolor.rgba, sizeof(vcolor.rgba)); #if defined(DEBUG_FLAG) printf("Point %d %d with color %6.4f %6.4f %6.4f\n", (int)ascene->lines[ind].vertices[num_ver].xyzw[0], (int)ascene->lines[ind].vertices[num_ver].xyzw[1], ascene->lines[ind].vertices[num_ver].rgba[0], ascene->lines[ind].vertices[num_ver].rgba[1], ascene->lines[ind].vertices[num_ver].rgba[2]); #endif num_ver ++; break; case COLOR_KEY: vcolor.rgba[0] = atoi(arg0)/255.0; vcolor.rgba[1] = atoi(arg1)/255.0; vcolor.rgba[2] = atoi(arg2)/255.0; break; default: printf("%s:%d Line (%s %s %s %s %s %s) ignored.\n", __FILE__, __LINE__, keyword, arg0, arg1, arg2, arg3, arg4); } if (num_ver == 2) { break; } } ascene->nlines++; break; case POLYLINE_KEY: ind = ascene->npolylines; ascene->polylines[ind].nvertices = atoi(arg0); ascene->polylines[ind].width = atof(arg1); ascene->polylines[ind].vertices = (COLOR_VERTEX *)malloc(sizeof(COLOR_VERTEX) * ascene->polylines[ind].nvertices); vcolor = fcolor; num_ver = 0; while (readAndParse(fp, keyword, arg0, arg1, arg2, arg3, arg4) > 0) { key_id1 = match_Keyword(keyword, &npara); switch(key_id1) { case VERTEX_KEY: ascene->polylines[ind].vertices[num_ver].xyzw[0] = atof(arg0); ascene->polylines[ind].vertices[num_ver].xyzw[1] = atof(arg1); ascene->polylines[ind].vertices[num_ver].xyzw[2] = atof(arg2); memcpy(ascene->polylines[ind].vertices[num_ver].rgba, vcolor.rgba, sizeof(vcolor.rgba)); #if defined(DEBUG_FLAG) printf("Point %d %d with color %6.4f %6.4f %6.4f\n", (int)ascene->polylines[ind].vertices[num_ver].xyzw[0], (int)ascene->polylines[ind].vertices[num_ver].xyzw[1], ascene->polylines[ind].vertices[num_ver].rgba[0], ascene->polylines[ind].vertices[num_ver].rgba[1], ascene->polylines[ind].vertices[num_ver].rgba[2]); #endif num_ver ++; break; case COLOR_KEY: vcolor.rgba[0] = atoi(arg0)/255.0; vcolor.rgba[1] = atoi(arg1)/255.0; vcolor.rgba[2] = atoi(arg2)/255.0; break; default: printf("%s:%d Line (%s %s %s %s %s %s) ignored.\n", __FILE__, __LINE__, keyword, arg0, arg1, arg2, arg3, arg4); } if (num_ver >= ascene->polylines[ind].nvertices) { break; } } ascene->npolylines++; break; case TRIANGLE_KEY: ind = ascene->ntriangles; vcolor = fcolor; num_ver = 0; while (readAndParse(fp, keyword, arg0, arg1, arg2, arg3, arg4) > 0) { key_id1 = match_Keyword(keyword, &npara); switch(key_id1) { case VERTEX_KEY: ascene->triangles[ind].vertices[num_ver].xyzw[0] = atof(arg0); ascene->triangles[ind].vertices[num_ver].xyzw[1] = atof(arg1); ascene->triangles[ind].vertices[num_ver].xyzw[2] = atof(arg2); memcpy(ascene->triangles[ind].vertices[num_ver].rgba, vcolor.rgba, sizeof(vcolor.rgba)); #if defined(DEBUG_FLAG) printf("Point %d %d with color %6.4f %6.4f %6.4f\n", (int)ascene->triangles[ind].vertices[num_ver].xyzw[0], (int)ascene->triangles[ind].vertices[num_ver].xyzw[1], ascene->triangles[ind].vertices[num_ver].rgba[0], ascene->triangles[ind].vertices[num_ver].rgba[1], ascene->triangles[ind].vertices[num_ver].rgba[2]); #endif num_ver ++; break; case COLOR_KEY: vcolor.rgba[0] = atoi(arg0)/255.0; vcolor.rgba[1] = atoi(arg1)/255.0; vcolor.rgba[2] = atoi(arg2)/255.0; break; default: printf("%s:%d Line (%s %s %s %s %s %s) ignored.\n", __FILE__, __LINE__, keyword, arg0, arg1, arg2, arg3, arg4); } if (num_ver >= 3) { break; } } ascene->ntriangles++; break; case SAVE_KEY: strcpy(saved_fname, arg0); break; default: printf("%s:%d Keyword (%s) and the line (%s %s %s %s %s) ignored.\n", __FILE__, __LINE__, keyword, arg0, arg1, arg2, arg3, arg4); } } fclose(fp); return 0; }
int Read_SSD_Scene(char *fname, SCENE *ascene, CAMERA *acamera, char *saved_fname) { char keyword[MAXLINELENGTH], arg0[MAXLINELENGTH], arg1[MAXLINELENGTH], arg2[MAXLINELENGTH], arg3[MAXLINELENGTH]; char arg4[MAXLINELENGTH]; int ident = 0; FILE *fp; /* We first set all the default values */ RGB_COLOR fcolor, vcolor; int ind, ii, num_ver, key_id, key_id1, npara; int shading = 0; double diffuse[3] = {0,0,0}; double specular[3] = {0,0,0}; ascene->screen_w = 600; ascene->screen_h = 400; /* The default color is white */ ascene->bcolor.rgba[0] = 1.0; ascene->bcolor.rgba[1] = 1.0; ascene->bcolor.rgba[2] = 1.0; ascene->bcolor.rgba[3] = 1.0; fcolor.rgba[0] = 0.0; fcolor.rgba[1] = 0.0; fcolor.rgba[2] = 0.0; fcolor.rgba[3] = 1.0; ascene->nlines = 0; ascene->npolylines = 0; ascene->ntriangles = 0; // ascene->nidentities = 0; ascene->pjType = 0; //0:orthographic 1:perspective ascene->isAxis = 0; //0 represents noAxis ascene->nlights =0; int ntranslate = 0; int nrotate = 0; int nscale = 0; int nmesh = 0; fp = fopen(fname,"rb"); if (fp == NULL) { fprintf(stderr,"%s:%d: Can not open SSD file <%s>.\n", __FILE__, __LINE__, fname); return -1; } while (readAndParse(fp, keyword, arg0, arg1, arg2, arg3, arg4) > 0) { if (keyword[0] == '\0') { /* We simply all blank lines */ continue; } key_id = match_Keyword(keyword, &npara); switch(key_id) { case LINE_KEY: ascene->nlines++; break; case POLYLINE_KEY: ascene->npolylines++; break; case TRIANGLE_KEY: ascene->ntriangles++; break; case IDENTITY_KEY: ascene->nidentities++; break; case TRANSLATE_KEY: ntranslate++; break; case SCALE_KEY: nscale++; break; case ROTATE_KEY: nrotate++; break; case MESH_KEY: nmesh++; break; case LIGHT_KEY: ascene->nlights++; break; } } printf("There are %d lines, %d polylines, and %d triangles in %s.\n", ascene->nlines, ascene->npolylines, ascene->ntriangles, fname); /* We rewind the file to the very beginning to read the file again */ rewind(fp); ascene->lines = (LINE *)malloc(sizeof(LINE) * ascene->nlines); ascene->polylines = (POLYLINE *)malloc(sizeof(POLYLINE) * ascene->npolylines); ascene->triangles = (TRIANGLE *)malloc(sizeof(TRIANGLE) * ascene->ntriangles); ascene->identities = (IDENTITY *)malloc(sizeof(IDENTITY) * ascene->nidentities); ascene->translate = (TRANSLATE *)malloc(sizeof(TRANSLATE) * ntranslate); ascene->scale = (SCALE *)malloc(sizeof(SCALE) * nscale); ascene->rotate = (ROTATE *)malloc(sizeof(ROTATE) * nrotate); ascene->mesh = (MESH *)malloc(sizeof(MESH) * nmesh); ascene->lights = (LIGHT *)malloc(sizeof(LIGHT) * ascene->nlights); ascene->nlines = 0; ascene->npolylines = 0; ascene->ntriangles = 0; ascene->nlights = 0; ascene->nidentities = 0; ntranslate = 0; nscale = 0; nrotate = 0; nmesh = 0; int instr_ind = 0; int brk = 0; int readAndParseResult = 0; while (readAndParse(fp, keyword, arg0, arg1, arg2, arg3, arg4) > 0) { if (keyword[0] == '\0') { /* We simply all blank lines */ continue; } key_id = match_Keyword(keyword, &npara); switch(key_id) { case SCREEN_KEY: ascene->screen_w = atoi(arg0); ascene->screen_h = atoi(arg1); ascene->bcolor.rgba[0] = atoi(arg2)/255.0; ascene->bcolor.rgba[1] = atoi(arg3)/255.0; ascene->bcolor.rgba[2] = atoi(arg4)/255.0; ascene->bcolor.rgba[3] = 1.0; break; case COLOR_KEY: /* We read the color */ fcolor.rgba[0] = atoi(arg0)/255.0; fcolor.rgba[1] = atoi(arg1)/255.0; fcolor.rgba[2] = atoi(arg2)/255.0; break; case LINE_KEY: ind = ascene->nlines; ascene->lines[ind].width = atof(arg0); /* We set the default colors */ vcolor = fcolor; num_ver = 0; while (readAndParse(fp, keyword, arg0, arg1, arg2, arg3, arg4) > 0) { key_id1 = match_Keyword(keyword, &npara); switch(key_id1) { case VERTEX_KEY: ascene->lines[ind].vertices[num_ver].xyzw[0] = atof(arg0); ascene->lines[ind].vertices[num_ver].xyzw[1] = atof(arg1); ascene->lines[ind].vertices[num_ver].xyzw[2] = atof(arg2); memcpy(ascene->lines[ind].vertices[num_ver].rgba, vcolor.rgba, sizeof(vcolor.rgba)); #if defined(DEBUG_FLAG) printf("Point %d %d with color %6.4f %6.4f %6.4f\n", (int)ascene->lines[ind].vertices[num_ver].xyzw[0], (int)ascene->lines[ind].vertices[num_ver].xyzw[1], ascene->lines[ind].vertices[num_ver].rgba[0], ascene->lines[ind].vertices[num_ver].rgba[1], ascene->lines[ind].vertices[num_ver].rgba[2]); #endif num_ver ++; break; case COLOR_KEY: vcolor.rgba[0] = atoi(arg0)/255.0; vcolor.rgba[1] = atoi(arg1)/255.0; vcolor.rgba[2] = atoi(arg2)/255.0; break; default: printf("%s:%d Line (%s %s %s %s %s %s) ignored.\n", __FILE__, __LINE__, keyword, arg0, arg1, arg2, arg3, arg4); } if (num_ver == 2) { break; } } ascene->nlines++; break; case POLYLINE_KEY: ind = ascene->npolylines; ascene->polylines[ind].nvertices = atoi(arg0); ascene->polylines[ind].width = atof(arg1); ascene->polylines[ind].vertices = (COLOR_VERTEX *)malloc(sizeof(COLOR_VERTEX) * ascene->polylines[ind].nvertices); vcolor = fcolor; num_ver = 0; while (readAndParse(fp, keyword, arg0, arg1, arg2, arg3, arg4) > 0) { key_id1 = match_Keyword(keyword, &npara); switch(key_id1) { case VERTEX_KEY: ascene->polylines[ind].vertices[num_ver].xyzw[0] = atof(arg0); ascene->polylines[ind].vertices[num_ver].xyzw[1] = atof(arg1); ascene->polylines[ind].vertices[num_ver].xyzw[2] = atof(arg2); memcpy(ascene->polylines[ind].vertices[num_ver].rgba, vcolor.rgba, sizeof(vcolor.rgba)); #if defined(DEBUG_FLAG) printf("Point %d %d with color %6.4f %6.4f %6.4f\n", (int)ascene->polylines[ind].vertices[num_ver].xyzw[0], (int)ascene->polylines[ind].vertices[num_ver].xyzw[1], ascene->polylines[ind].vertices[num_ver].rgba[0], ascene->polylines[ind].vertices[num_ver].rgba[1], ascene->polylines[ind].vertices[num_ver].rgba[2]); #endif num_ver ++; break; case COLOR_KEY: vcolor.rgba[0] = atoi(arg0)/255.0; vcolor.rgba[1] = atoi(arg1)/255.0; vcolor.rgba[2] = atoi(arg2)/255.0; break; default: printf("%s:%d Line (%s %s %s %s %s %s) ignored.\n", __FILE__, __LINE__, keyword, arg0, arg1, arg2, arg3, arg4); } if (num_ver >= ascene->polylines[ind].nvertices) { break; } } ascene->npolylines++; break; case TRIANGLE_KEY: ind = ascene->ntriangles; vcolor = fcolor; num_ver = 0; while (readAndParse(fp, keyword, arg0, arg1, arg2, arg3, arg4) > 0) { key_id1 = match_Keyword(keyword, &npara); switch(key_id1) { case VERTEX_KEY: ascene->triangles[ind].vertices[num_ver].xyzw[0] = atof(arg0); ascene->triangles[ind].vertices[num_ver].xyzw[1] = atof(arg1); ascene->triangles[ind].vertices[num_ver].xyzw[2] = atof(arg2); memcpy(ascene->triangles[ind].vertices[num_ver].rgba, vcolor.rgba, sizeof(vcolor.rgba)); #if defined(DEBUG_FLAG) printf("Point %d %d with color %6.4f %6.4f %6.4f\n", (int)ascene->triangles[ind].vertices[num_ver].xyzw[0], (int)ascene->triangles[ind].vertices[num_ver].xyzw[1], ascene->triangles[ind].vertices[num_ver].rgba[0], ascene->triangles[ind].vertices[num_ver].rgba[1], ascene->triangles[ind].vertices[num_ver].rgba[2]); #endif num_ver ++; break; case COLOR_KEY: vcolor.rgba[0] = atoi(arg0)/255.0; vcolor.rgba[1] = atoi(arg1)/255.0; vcolor.rgba[2] = atoi(arg2)/255.0; break; default: printf("%s:%d Line (%s %s %s %s %s %s) ignored.\n", __FILE__, __LINE__, keyword, arg0, arg1, arg2, arg3, arg4); } if (num_ver >= 3) { break; } } ascene->ntriangles++; break; case EYE_KEY: acamera->eye.xyzw[0] = atof(arg0); acamera->eye.xyzw[1] = atof(arg1); acamera->eye.xyzw[2] = atof(arg2); break; case GAZE_KEY: acamera->gaze.xyzw[0] = atof(arg0); acamera->gaze.xyzw[1] = atof(arg1); acamera->gaze.xyzw[2] = atof(arg2); break; case UPVECTOR_KEY: acamera->upVector.xyzw[0] = atof(arg0); acamera->upVector.xyzw[1] = atof(arg1); acamera->upVector.xyzw[2] = atof(arg2); break; case ORTHO_KEY: ascene->ortho.right = atof(arg0); ascene->ortho.top = atof(arg1); ascene->ortho.near = atof(arg2); ascene->ortho.far = atof(arg3); break; case PERSP_KEY: ascene->pjType = 1; ascene->persp.near = atof(arg1); ascene->persp.far = atof(arg2); ascene->persp.angle = atof(arg0); break; case FLOOR_KEY: ascene->floor.size = atof(arg0); ascene->floor.xmin = atof(arg1); ascene->floor.xmax = atof(arg2); ascene->floor.ymin = atof(arg3); ascene->floor.ymax = atof(arg4); ascene->floor.color = fcolor; break; case AXIS_KEY: ascene->isAxis = 1; ascene->axis.width = atof(arg0); ascene->axis.length = atof(arg1); break; case AMBIENT_KEY: ascene->ambient[0] = atof(arg0); ascene->ambient[1] = atof(arg1); ascene->ambient[2] = atof(arg2); break; case SHADING_KEY: shading = atof(arg0); break; case DIFFUSE_KEY: diffuse[0] = atof(arg0); diffuse[1] = atof(arg1); diffuse[2] = atof(arg2); break; case SPECULAR_KEY: specular[0] = atof(arg0); specular[1] = atof(arg1); specular[2] = atof(arg2); specular[3] = atof(arg3); break; case LIGHT_KEY: { ascene->lights[ascene->nlights].light[0] = atof(arg0); ascene->lights[ascene->nlights].light[1] = atof(arg1); ascene->lights[ascene->nlights].light[2] = atof(arg2); int result_readAndParse = readAndParse(fp, keyword, arg0, arg1, arg2, arg3, arg4); key_id = match_Keyword(keyword, &npara); ascene->lights[ascene->nlights].ndirections = 0; while(key_id == VERTEX_KEY) { ascene->lights[ascene->nlights].directions[ascene->lights[ascene->nlights].ndirections][0] = atof(arg0); ascene->lights[ascene->nlights].directions[ascene->lights[ascene->nlights].ndirections][1] = atof(arg1); ascene->lights[ascene->nlights].directions[ascene->lights[ascene->nlights].ndirections++][2] = atof(arg2); result_readAndParse = readAndParse(fp, keyword, arg0, arg1, arg2, arg3, arg4); key_id = match_Keyword(keyword, &npara); } fseek(fp,-result_readAndParse,SEEK_CUR); ascene->nlights++; } break; case IDENTITY_KEY: readAndParseResult = readAndParse(fp, keyword, arg0, arg1, arg2, arg3, arg4); while (readAndParseResult > 0) { key_id1 = match_Keyword(keyword, &npara); switch(key_id1) { case TRANSLATE_KEY: ascene->translate[ntranslate].xyz[0] = atof(arg0); ascene->translate[ntranslate].xyz[1] = atof(arg1); ascene->translate[ntranslate++].xyz[2] = atof(arg2); ascene->identities[ascene->nidentities].instr[instr_ind++] = TRANSLATE_KEY; break; case ROTATE_KEY: ascene->rotate[nrotate].angle = atof(arg0); ascene->rotate[nrotate].xyz[0] = atof(arg1); ascene->rotate[nrotate].xyz[1] = atof(arg2); ascene->rotate[nrotate++].xyz[2] = atof(arg3); ascene->identities[ascene->nidentities].instr[instr_ind++] = ROTATE_KEY; break; case SCALE_KEY: ascene->scale[nscale].xyz[0] = atof(arg0); ascene->scale[nscale].xyz[1] = atof(arg1); ascene->scale[nscale++].xyz[2] = atof(arg2); ascene->identities[ascene->nidentities].instr[instr_ind++] = SCALE_KEY; break; case LIGHT_KEY: { ascene->lights[ascene->nlights].light[0] = atof(arg0); ascene->lights[ascene->nlights].light[1] = atof(arg1); ascene->lights[ascene->nlights].light[2] = atof(arg2); int result_readAndParse = readAndParse(fp, keyword, arg0, arg1, arg2, arg3, arg4); key_id = match_Keyword(keyword, &npara); ascene->lights[ascene->nlights].ndirections = 0; while(key_id == VERTEX_KEY){ ascene->lights[ascene->nlights].directions[ascene->lights[ascene->nlights].ndirections][0] = atof(arg0); ascene->lights[ascene->nlights].directions[ascene->lights[ascene->nlights].ndirections][1] = atof(arg1); ascene->lights[ascene->nlights].directions[ascene->lights[ascene->nlights].ndirections++][2] = atof(arg2); result_readAndParse = readAndParse(fp, keyword, arg0, arg1, arg2, arg3, arg4); key_id = match_Keyword(keyword, &npara); } fseek(fp,-result_readAndParse,SEEK_CUR); ascene->nlights++; } break; case AMBIENT_KEY: ascene->ambient[0] = atof(arg0); ascene->ambient[1] = atof(arg1); ascene->ambient[2] = atof(arg2); break; case SHADING_KEY: shading = atof(arg0); break; case DIFFUSE_KEY: diffuse[0] = atof(arg0); diffuse[1] = atof(arg1); diffuse[2] = atof(arg2); break; case SPECULAR_KEY: specular[0] = atof(arg0); specular[1] = atof(arg1); specular[2] = atof(arg2); specular[3] = atof(arg3); break; case MESH_KEY: { FILE *off = fopen(arg0,"rb"); ascene->mesh[nmesh].width = atof(arg1); ascene->mesh[nmesh].shading = shading; ascene->mesh[nmesh].diffuse[0] = diffuse[0]; ascene->mesh[nmesh].diffuse[1] = diffuse[1]; ascene->mesh[nmesh].diffuse[2] = diffuse[2]; ascene->mesh[nmesh].specular[0] = specular[0]; ascene->mesh[nmesh].specular[1] = specular[1]; ascene->mesh[nmesh].specular[2] = specular[2]; ascene->mesh[nmesh].specular[3] = specular[3]; readAndParse(off, keyword, arg0, arg1, arg2, arg3, arg4); readAndParse(off, keyword, arg0, arg1, arg2, arg3, arg4); ascene->mesh[nmesh].nvertices = atoi(keyword); ascene->mesh[nmesh].vertices = (COLOR_VERTEX *)malloc(sizeof(COLOR_VERTEX) * ascene->mesh[nmesh].nvertices); ascene->mesh[nmesh].npolygons = atoi(arg0); ascene->mesh[nmesh].polygons = (POLYGON *)malloc(sizeof(POLYGON) * ascene->mesh[nmesh].npolygons); int mm; for(mm = 0; mm < ascene->mesh[nmesh].nvertices; mm++){ readAndParse(off, keyword, arg0, arg1, arg2, arg3, arg4); ascene->mesh[nmesh].vertices[mm].xyzw[0] = atof(keyword); ascene->mesh[nmesh].vertices[mm].xyzw[1] = atof(arg0); ascene->mesh[nmesh].vertices[mm].xyzw[2] = atof(arg1); ascene->mesh[nmesh].vertices[mm].xyzw[3] = 1; if(arg4[0] == '\0'){ ascene->mesh[nmesh].vertices[mm].rgba[0] = fcolor.rgba[0]; ascene->mesh[nmesh].vertices[mm].rgba[1] = fcolor.rgba[1]; ascene->mesh[nmesh].vertices[mm].rgba[2] = fcolor.rgba[2]; } else{ ascene->mesh[nmesh].vertices[mm].rgba[0] = atof(arg2); ascene->mesh[nmesh].vertices[mm].rgba[1] = atof(arg3); ascene->mesh[nmesh].vertices[mm].rgba[2] = atof(arg4); } } for(mm = 0; mm < ascene->mesh[nmesh].npolygons; mm++){ readAndParse(off, keyword, arg0, arg1, arg2, arg3, arg4); int index[5] = {atoi(arg0),atoi(arg1),atoi(arg2),atoi(arg3),atoi(arg4)}; ascene->mesh[nmesh].polygons[mm].nvertices = atoi(keyword); int nn; for(nn = 0; nn < ascene->mesh[nmesh].polygons[mm].nvertices;nn++){ ascene->mesh[nmesh].polygons[mm].num[nn] = index[nn]; } } nmesh++; ascene->identities[ascene->nidentities].instr[instr_ind++] = MESH_KEY; } break; case COLOR_KEY: fcolor.rgba[0] = atoi(arg0)/255.0; fcolor.rgba[1] = atoi(arg1)/255.0; fcolor.rgba[2] = atoi(arg2)/255.0; break; case IDENTITY_KEY: case SAVE_KEY: case LINE_KEY: brk = 1; fseek(fp,-readAndParseResult,SEEK_CUR); break; default: printf("%s:%d Line (%s %s %s %s %s %s) ignored.\n", __FILE__, __LINE__, keyword, arg0, arg1, arg2, arg3, arg4); } if(brk == 1) break; readAndParseResult = readAndParse(fp, keyword, arg0, arg1, arg2, arg3, arg4); } ascene->identities[ascene->nidentities].inStr_num = instr_ind; ascene->nidentities++; instr_ind = 0; brk = 0; break; case SAVE_KEY: strcpy(saved_fname, arg0); break; default: printf("%s:%d Keyword (%s) and the line (%s %s %s %s %s) ignored.\n", __FILE__, __LINE__, keyword, arg0, arg1, arg2, arg3, arg4); } } fclose(fp); return 0; }
int main(int argc, char* argv[]) { char *prgName = NULL; char *iFileName = NULL; char *oFileName = NULL; /*check for missing inputs*/ if(argc < 3) { printf("%s\n","Missing arguments"); exit(4); } prgName = argv[0]; iFileName = argv[1]; oFileName = argv[2]; char lLine[MAX_LINE_LENGTH+1], *lLabel, *lOpcode, *lArg1, *lArg2, *lArg3, *lArg4; opCodeLine symboles[MAX_LINE_LENGTH+1]; init_opCodeLine(symboles,MAX_LINE_LENGTH+1); int lRet; int address; int k = 0,c = 0; FILE * lInFile; lInFile = fopen(iFileName,"r"); address = 0; /*Preform Pass 1 to gain symbol table*/ do { lRet = readAndParse(lInFile,lLine, &lLabel, &lOpcode, &lArg1, &lArg2, &lArg3, &lArg4); if(lRet != DONE && lRet != EMPTY_LINE) { DEBUG_PRINT("Working on %s\n",lLine); DEBUG_PRINT("label: %s, opcode: %s, arg1: %s, arg2: %s, arg3: %s, arg4: %s\n",lLabel,lOpcode,lArg1,lArg2,lArg3,lArg4); /*Check for .ORIG assimblier instruction*/ if(strcmp(lOpcode,".end") == 0) { DEBUG_PRINT("Found .end Command, starting phase 2 scan\n"); lRet = DONE; } else if(strcmp(lOpcode,".orig") == 0) { address = toNum(lArg1); DEBUG_PRINT("Found .ORIG Statment\n"); } else { /*check for Address depended instrctions*/ if(find_opCodeLine(symboles,lLabel)!= -1) { DEBUG_PRINT("Found a duplicate label\n"); printf("Duplicate Label: '%s'\n",lLabel); exit(4); } set_opCodeLine(&symboles[k],address,lLabel,lOpcode,lArg1,lArg2,lArg3,lArg4); address+=2; k+=1; } } }while(lRet != DONE); debug_printOpCodeLines(symboles,MAX_LINE_LENGTH+1); /*Phase 2 Scan, Convert to Object Code*/ printf("OBJ: 0x%x\n",symboles[0].address); for(c = 0; c < k; c++) { printf("OBJ: 0x%x\n",get_object_code(symboles,c)); } }
/* Note: MAX_LINE_LENGTH, OK, EMPTY_LINE, and DONE are defined values */ int main (){ char lLine[MAX_LINE_LENGTH + 1], *lLabel, *lOpcode, *lArg1, *lArg2, *lArg3, *lArg4; int lRet; int i = 0; int ctr = 0; int addrCtr; /*used in 2nd pass of program*/ int mach_code = 0; int arg1_num = 0; int orig; /*start addr of program*/ int offs; /*offset from current addr to label*/ FILE * pOutfile; pOutfile = fopen( "data.out", "w" ); int endDefined = 0; FILE * lInfile; lInfile = fopen("data.in", "r"); /* open the input file */ /*find start address of program*/ do{ lRet = readAndParse( lInfile, lLine, &lLabel, &lOpcode, &lArg1, &lArg2, &lArg3, &lArg4 ); if (strcmp(lOpcode, ".orig") == 0){ orig = toNum(lArg1); printf("%i\n",orig); } else if (strcmp(lOpcode, ".end") == 0){ endDefined = 1; } } while (lRet != DONE); /*If there is no .END, then throw an error*/ if(endDefined == 0){ error(4); } /*1st pass: generate symbol table*/ /* lInfile = fopen("data.in", "r");*/ /* open the input file */ rewind(lInfile); do{ lRet = readAndParse( lInfile, lLine, &lLabel, &lOpcode, &lArg1, &lArg2, &lArg3, &lArg4 ); printf("1st pass"); printf(" lRet: %i\n", lRet); if (label){ symbol_table[i].addr = orig + ctr; printf("address: %i\n", symbol_table[i].addr); symbol_table[i].name = lLabel; printf("label: %s\n", symbol_table[i].name); label = 0; i++; } ctr++; } while( lRet != DONE ); /*writeText(mach_code);2nd pass: generate machine code*/ /*lInfile = fopen("data.in", "r");*/ /* open the input file */ rewind(lInfile); do { addrCtr = orig; lRet = readAndParse( lInfile, lLine, &lLabel, &lOpcode, &lArg1, &lArg2, &lArg3, &lArg4 ); printf("label:%s", lLabel); printf(" opcode:%s", lOpcode); printf(" arg1:%s", lArg1); printf(" arg2:%s ", lArg2); printf(" arg3:%s\n", lArg3); if( lRet != DONE && lRet != EMPTY_LINE ){ /*generate machine code (hex) given the parsed opcodes*/ if (strcmp(lOpcode, ".orig") == 0){ fprintf( pOutfile, "0x%.4X\n", orig); } if (strcmp(lOpcode, "br") || strcmp(lOpcode, "brnzp") == 0){ /*need condition for n, z, p,*/ offs = returnOffset(lArg1, addrCtr); /*check if offset is too big!!!*/ mach_code = offs; } else if (strcmp(lOpcode, "add") == 0){ mach_code = (ADD << 12) + ((lArg1[1] - 0x30)<<9) + ((lArg2[1] - 0x30)<<6); mach_code &= 0xFFC0; /*clearing the last 6 bits*/ /*see if the number being added is a constant number*/ if( (lArg3[0] == 'x') || (lArg3[0] == '#')){ mach_code |= 0x10; /*Changing the 5th bit to 1 since we are adding a constant*/ mach_code += toNum(lArg3); } /*Argument 3 is a register*/ else{ mach_code += (lArg3[1] - 0x30); } fprintf( pOutfile, "0x%.4X\n", mach_code); } else if (strcmp(lOpcode, "ldb") == 0){ /*extract reg number from arguments, then shift to correct place*/ mach_code = (LDB << 12) + ((lArg1[1] - 0x30)<<9) + ((lArg2[1] - 0x30)<<6); /* put constant in bits[5:0]*/ mach_code &= 0xFFC0; mach_code += toNum(lArg3); printf("mach_code: %i\n", mach_code); fprintf( pOutfile, "0x%.4X\n", mach_code); } else if (strcmp(lOpcode, "stb") == 0){ /*extract reg number from arguments, then shift to correct place*/ mach_code = (STB << 12) + ((lArg1[1] - 0x30)<<9) + ((lArg2[1] - 0x30)<<6); /* put constant in bits[5:0]*/ mach_code &= 0xFFC0; mach_code += toNum(lArg3); printf("mach_code: %i\n", mach_code); fprintf( pOutfile, "0x%.4X\n", mach_code); } else if (strcmp(lOpcode, "jsr") == 0 || strcmp(lOpcode, "jsrr") == 0){ if (strcmp(lOpcode, "jsr") == 0) { mach_code = (JSR << 12) + 1<<11; } else { mach_code = (JSR << 12) + ((lArg1[1] - 0x30) << 5); } } /*fprintf( pOutfile, "0x%.4X\n", mach_code);*/ printf("MACH_CODE!: %i\n", mach_code); addrCtr++; } } while( lRet != DONE ); fclose(pOutfile); }
int main(int argc, char *argv[]) { char *inFileString, *outFileString; FILE *inFilePtr, *outFilePtr; int address; char label[MAXLINELENGTH], opcode[MAXLINELENGTH], arg0[MAXLINELENGTH], arg1[MAXLINELENGTH], arg2[MAXLINELENGTH], argTmp[MAXLINELENGTH]; int i; int numLabels=0; int num; int addressField; char labelArray[MAXNUMLABELS][MAXLABELLENGTH]; int labelAddress[MAXNUMLABELS]; if (argc != 3) { printf("error: usage: %s <assembly-code-file> <machine-code-file>\n", argv[0]); exit(1); } inFileString = argv[1]; outFileString = argv[2]; inFilePtr = fopen(inFileString, "r"); if (inFilePtr == NULL) { printf("error in opening %s\n", inFileString); exit(1); } outFilePtr = fopen(outFileString, "w"); if (outFilePtr == NULL) { printf("error in opening %s\n", outFileString); exit(1); } /* map symbols to addresses */ /* assume address start at 0 */ for (address=0; readAndParse(inFilePtr, label, opcode, arg0, arg1, arg2); address++) { /* printf("%d: label=%s, opcode=%s, arg0=%s, arg1=%s, arg2=%s\n", address, label, opcode, arg0, arg1, arg2); */ /* check for illegal opcode */ if (strcmp(opcode, "add") && strcmp(opcode, "nand") && strcmp(opcode, "lw") && strcmp(opcode, "sw") && strcmp(opcode, "beq") && strcmp(opcode, "jalr") && strcmp(opcode, "halt") && strcmp(opcode, "noop") && strcmp(opcode, ".fill") ) { printf("error: unrecognized opcode %s at address %d\n", opcode, address); exit(1); } /* check register fields */ if (!strcmp(opcode, "add") || !strcmp(opcode, "nand") || !strcmp(opcode, "lw") || !strcmp(opcode, "sw") || !strcmp(opcode, "beq") || !strcmp(opcode, "jalr")) { testRegArg(arg0); testRegArg(arg1); } if (!strcmp(opcode, "add") || !strcmp(opcode, "nand")) { testRegArg(arg2); } /* check addressField */ if (!strcmp(opcode, "lw") || !strcmp(opcode, "sw") || !strcmp(opcode, "beq")) { testAddrArg(arg2); } if (!strcmp(opcode, ".fill")) { testAddrArg(arg0); } /* check for enough arguments */ if ( (strcmp(opcode, "halt") && strcmp(opcode, "noop") && strcmp(opcode, ".fill") && strcmp(opcode, "jalr") && arg2[0]=='\0') || (!strcmp(opcode, "jalr") && arg1[0]=='\0') || (!strcmp(opcode, ".fill") && arg0[0]=='\0')) { printf("error at address %d: not enough arguments\n", address); exit(2); } if (label[0] != '\0') { /* check for labels that are too long */ if (strlen(label) >= MAXLABELLENGTH) { printf("label too long\n"); exit(2); } /* make sure label starts with letter */ if (! sscanf(label, "%[a-zA-Z]", argTmp) ) { printf("label doesn't start with letter\n"); exit(2); } /* make sure label consists of only letters and numbers */ sscanf(label, "%[a-zA-Z0-9]", argTmp); if (strcmp(argTmp, label)) { printf("label has character other than letters and numbers\n"); exit(2); } /* look for duplicate label */ for (i=0; i<numLabels; i++) { if (!strcmp(label, labelArray[i])) { printf("error: duplicate label %s at address %d\n", label, address); exit(1); } } /* see if there are too many labels */ if (numLabels >= MAXNUMLABELS) { printf("error: too many labels (label=%s)\n", label); exit(2); } strcpy(labelArray[numLabels], label); labelAddress[numLabels++] = address; } } for (i=0; i<numLabels; i++) { /* printf("%s = %d\n", labelArray[i], labelAddress[i]); */ } /* now do second pass (print machine code, with symbols filled in as addresses) */ rewind(inFilePtr); for (address=0; readAndParse(inFilePtr, label, opcode, arg0, arg1, arg2); address++) { if (!strcmp(opcode, "add")) { num = (ADD << 22) | (atoi(arg0) << 19) | (atoi(arg1) << 16) | atoi(arg2); } else if (!strcmp(opcode, "nand")) { num = (NAND << 22) | (atoi(arg0) << 19) | (atoi(arg1) << 16) | atoi(arg2); } else if (!strcmp(opcode, "jalr")) { num = (JALR << 22) | (atoi(arg0) << 19) | (atoi(arg1) << 16); } else if (!strcmp(opcode, "halt")) { num = (HALT << 22); } else if (!strcmp(opcode, "noop")) { num = (NOOP << 22); } else if (!strcmp(opcode, "lw") || !strcmp(opcode, "sw") || !strcmp(opcode, "beq")) { /* if arg2 is symbolic, then translate into an address */ if (!isNumber(arg2)) { addressField = translateSymbol(labelArray, labelAddress, numLabels, arg2); /* printf("%s being translated into %d\n", arg2, addressField); */ if (!strcmp(opcode, "beq")) { addressField = addressField-address-1; } } else { addressField = atoi(arg2); } if (addressField < -32768 || addressField > 32767) { printf("error: offset %d out of range\n", addressField); exit(1); } /* truncate the offset field, in case it's negative */ addressField = addressField & 0xFFFF; if (!strcmp(opcode, "beq")) { num = (BEQ << 22) | (atoi(arg0) << 19) | (atoi(arg1) << 16) | addressField; } else { /* lw or sw */ if (!strcmp(opcode, "lw")) { num = (LW << 22) | (atoi(arg0) << 19) | (atoi(arg1) << 16) | addressField; } else { num = (SW << 22) | (atoi(arg0) << 19) | (atoi(arg1) << 16) | addressField; } } } else if (!strcmp(opcode, ".fill")) { if (!isNumber(arg0)) { num = translateSymbol(labelArray, labelAddress, numLabels, arg0); } else { num = atoi(arg0); } } /* printf("(address %d): %d (hex 0x%x)\n", address, num, num); */ fprintf(outFilePtr, "%d\n", num); } exit(0); }
int main(int argc, char** argv) { char* line; /*Last line read */ char* label; /*Last label read*/ char* oCode; /*Last opcode read*/ char* a1; /*Last arguments read*/ char* a2; char* a3; char* a4; int steer; int stat; /*Status code of last line*/ int num; char *prgName = NULL; char *iFileName = NULL; char *oFileName = NULL; prgName = argv[0]; iFileName = argv[1]; oFileName = argv[2]; /*For manual testing*/ /*prgName = "OutProgram"; iFileName = "test2.txt"; oFileName = "test2_out.txt";*/ line=(char*)malloc((MAX_LINE_LENGTH+1)*sizeof(char)); printf("program name = '%s'\n", prgName); printf("input file name = '%s'\n", iFileName); printf("output file name = '%s'\n", oFileName); /* open the source file */ infile = fopen(argv[1], "r"); outfile = fopen(argv[2], "w"); /*For manual testing*/ /*infile = fopen(iFileName, "r"); outfile = fopen(oFileName, "w");*/ if (infile==NULL) { printf("Error: Cannot open file %s\n", argv[1]); exit(4); } if (outfile==NULL) { printf("Error: Cannot open file %s\n", argv[2]); exit(4); } stat = EMPTY_LINE; while(stat == EMPTY_LINE) /*Find first line */ stat=readAndParse( infile, line, &label, &oCode, &a1, &a2, &a3, &a4); if(strcmp(oCode, ".orig")!=0) { printf("Error: File does not begin with .orig"); closeFiles(); exit(4); } origin = toNum(a1); /*Make sure origin is in correct range */ if(origin < 0 || origin > 65534||origin%2 != 0) { printf("Error: Invalid Origin, must be between 0x0000 and 0xFFFF and even"); closeFiles(); exit(3); } PC = origin; while(stat != DONE) { stat = readLabel(infile, line, &label); if(*label != '\0') { addLabel(&label); } /*Increment PC */ if(stat == OK) PC+=2; } if(PC>65538) /*Prevents program from exceeding memory location 0xFFFF*/ { printf("Error: Program exceeds memory"); closeFiles(); exit(4); } rewind(infile); PC=origin; stat = EMPTY_LINE; while(stat == EMPTY_LINE) /*Finds .orig and goes to next line */ stat=readAndParse( infile, line, &label, &oCode, &a1, &a2, &a3, &a4); fprintf(outfile, "0x%0.4X\n", origin); /*Prints out formated Hex version of .orig line */ while(stat != DONE&&stat!=END_OP) { stat = readAndParse( infile, line, &label, &oCode, &a1, &a2, &a3, &a4); if(stat == OK) { steer = 0; num = parseOpcode(oCode,&steer); if(num== -1) { printf("Error: %s is not a valid opcode\n",oCode); closeFiles(); exit(2); } if(num == 17) stat=END_OP; else { /*Execute Opcode */ processOpcode( num, &steer, a1, a2, a3, a4); PC+=2; } } } /*Must end with .end */ if(stat != END_OP) { printf("Error: File does not have a .end pseudocode\n"); closeFiles(); exit(4); } closeFiles(); return 0; }