Ejemplo n.º 1
0
Archivo: XML.c Proyecto: zanderdk/P1
Graph *readXml(FILE *fp) {
    Floor *floors;
    char c = 0;
    char buf[SMALL_BUFFER];
    int edgesCount, verticesCount, floorsCount;


    while ((c = getc(fp)) != EOF) {
        if (c == '<')
            break;
    }
    fscanf(fp, "%6s", buf);
    if (strcmp(buf, "graph")) {
        error();
        return NULL;
    }

    readDefaultAtributes(&edgesCount, &verticesCount, &floorsCount, fp);
    unsigned long amountToAlloc = sizeof(Graph) + floorsCount * sizeof(Floor) +
                                  verticesCount * sizeof(Vertex) +
                                  edgesCount * sizeof(Edge) +
                                  edgesCount * 2 * sizeof(EdgePointer);

    Graph *graph = malloc(amountToAlloc);
    memset(graph, 0, amountToAlloc);

    floors = (void *)(graph + 1);

    Edge *edges = (Edge *)(floors + floorsCount);
    Vertex *vertices = (Vertex *)(edges + edgesCount);

    reachChar('>', fp);

    readEdges(fp, edges, edgesCount);
    readvertices(fp, vertices, verticesCount, edges, edgesCount);

    nextElement(buf, fp);

    if (strcmp(buf, "graph"))
        error();

    crawlEdges(&edges, vertices, edgesCount, verticesCount);
    crawVertices(&vertices, verticesCount, &floors);
    crawlFloors(&floors, vertices, floorsCount, verticesCount);

    graph->numOfVertices = verticesCount;
    graph->numOfFloors = floorsCount;
    graph->floors = floors;

    return graph;
}
Ejemplo n.º 2
0
void PartitionIO<MeshType>::read (mesh_ptrtype& meshPart)
{
    meshPart.reset();
    M_meshPartIn.reset (new mesh_type);

    M_HDF5IO.openFile (M_fileName, M_comm, true);
    readStats();
    readPoints();
    readEdges();
    readFaces();
    readElements();
    M_HDF5IO.closeFile();

    meshPart = M_meshPartIn;
    M_meshPartIn.reset();
}
bool GraphModelXmlReader::read(QIODevice &in)
{
    auto *m_graphModel = dynamic_cast<GraphModel*>(m_model);
    if(!m_graphModel) {
        return false;
    }

    QXmlStreamReader stream(&in);

    stream.readNextStartElement();
    {
        readProperties(stream);
        readNodes(stream);
        readEdges(stream);
    }

    return true;
}
Ejemplo n.º 4
0
int main()
{
  /* use adjacent matrix to store the undirected graph */
  int V;
  char *adj = NULL;
  int *parent = NULL;

  int src, des; /* the vertices to calculate shortest path */
  char command[2];

  /* read the first character as the command.
   * until EOF */
  while (scanf("%s", command) == 1) {
    char c = command[0];
    switch (c) {

      case 'V':
      case 'v':
        /* start a new graph, need to reset adjacent matrix */
        scanf("%d", &V);
        if (V <= 0) {
          printf("Error: number of vertices must be positive.\n");
          break;
        } else {
          /* dynamic create the array for adjacent matrix
           * and parent array */
          if (adj != NULL) { /* free the old one */
            free(adj);
            free(parent);
          }
          adj = (char *)malloc(sizeof(char) * V * V);
          parent = (int *)malloc(sizeof(int) * V);
          if (adj == NULL || parent == NULL) {
            printf("Error: could not allocate memory for arrays.\n");
            exit(-1);
          }
        }
        break;

      case 'E':
      case 'e':
        /* load edges for the current graph */
        memset(adj, 0, V * V);
        readEdges(V, adj);
        break;

      case 's':
      case 'S':
        /* display shortest path between vertices */
        scanf("%d %d", &src, &des);
        if (src < 0 || src >= V || des < 0 || des >= V) {
          printf("Error: either vertex %d or %d is invalid.\n", src, des);
          break;
        }
        memset(parent, -1, sizeof(int) * V);
        BFS(V, adj, parent, src, des);
        if (parent[des] == -1) {
          /* no path found, display an error */
          printf("Error: no path is found between %d and %d.\n", src, des);
        } else {
          recoverPath(parent, des);
          printf("\n");
        }
        break;
      default:
        printf("Error: Not valid input");
    }
  }
  if (adj != NULL) { /* free the dynamic array */
    free(adj);
    free(parent);
  }
return 0;
}
Ejemplo n.º 5
0
//------------------------------------------------------------------------------
void SlpFrame::load(std::istream &istr)
{
  setIStream(istr);
  
  image_pixel_indexes_ = new uint8_t[width_ * height_];
  std::fill_n(image_pixel_indexes_, width_ * height_, transparent_index_);
  
  readEdges();
  
  // Skipping command offsets. They are not needed now but
  // they can be used for checking file integrity.
  for (uint32_t i=0; i < height_; i++)
  {
    read<uint32_t>();
  }
  
  // Each row has it's commands, 0x0F signals the end of a rows commands.
  for (uint32_t row = 0; row < height_; row++)
  {
    //std::cout << row << ": " << std::hex << (int)(tellg() - file_pos_) << std::endl;
    uint8_t data = 0;
    uint32_t pix_pos = left_edges_[row]; //pos where to start putting pixels
    
    while (data != 0x0F)
    {
      data = read<uint8_t>();
     
      //if ( (tellg() - file_pos_) == 0x1630)
      //  std::cout << row << ": " << std::hex << (int)(tellg() - file_pos_) << " cmd: " << (int)data<< std::endl;
      if (data == 0x0F)
        break;
      
      uint8_t cmd = data & 0xF;
      
      uint32_t pix_cnt = 0;
      
      //uint32_t pix_pos = left_edges_[row];
      uint8_t color_index = 0;
      
      switch (cmd) //0x00
      {
        case 0: // Lesser block copy
        case 4:
        case 8:
        case 0xC:
          pix_cnt = data >> 2;
          readPixelsToImage(row, pix_pos, pix_cnt);
          break;
        
        case 1: // lesser skip (making pixels transparent)
        case 5:
        case 9:
        case 0xD:
          pix_cnt = (data & 0xFC) >> 2;
          pix_pos += pix_cnt;
          break;
          
        case 2: // greater block copy
          pix_cnt = ((data & 0xF0) << 4) + read<uint8_t>();
          
          readPixelsToImage(row, pix_pos, pix_cnt);
          break;
          
        case 3: // greater skip
          pix_cnt = ((data & 0xF0) << 4) + read<uint8_t>();
          pix_pos += pix_cnt;
          break;
          
        case 6: // copy and transform (player color)
          pix_cnt = getPixelCountFromData(data);
 
          // TODO: player color
          readPixelsToImage(row, pix_pos, pix_cnt, true);
          
          break;
          
        case 7: // Run of plain color
          pix_cnt = getPixelCountFromData(data);
          
          color_index = read<uint8_t>();
          setPixelsToColor(row, pix_pos, pix_cnt, 
                           color_index);
        break;
        
        case 0xA: // Transform block (player color)
          pix_cnt = getPixelCountFromData(data);
          
          // TODO: file_.readuint8_t() | player_color
          color_index = read<uint8_t>();
          setPixelsToColor(row, pix_pos, pix_cnt, 
                           color_index, true);
        break;
        
        case 0x0B: // Shadow pixels
          //TODO: incomplete
          pix_cnt = getPixelCountFromData(data);
          pix_pos += pix_cnt; //skip until implemented
          
        break;
        
        case 0x0E: // extended commands.. TODO
        
          switch (data)
          {
            /*case 0x0E: //xflip?? skip?? TODO
            case 0x1E:
              //row-= 1;   
            break;
            */
            
            case 0x4E: //Outline pixel TODO player color
            case 0x6E: 
              //setPixelsToColor(outline_, row, pix_pos, 1, sf::Color(255,100,0));
              pix_pos += 1;
            break;
            
            case 0x5E: //Outline run TODO player color
            case 0x7E: 
              pix_cnt = read<uint8_t>();
              pix_pos += pix_cnt;
              
              //setPixelsToColor(outline_, row, pix_pos, pix_cnt, 
              //                 sf::Color(255 ,100,0));
            break;
          }

        break;
        default:
          std::cerr << "SlpFrame: Unknown cmd at " << std::hex << 
                  (int)(tellg() - slp_file_pos_)<< ": " << (int) data << std::endl;
          break;
      }
      
    }
  }
  
}