static int requestHandler(void* cls, MHD_Connection* conn, const char* url, const char* method, const char* version, const char* upload_data, size_t* upload_data_size, void** con_cls) { printf("requestHandler %s\n", url); // find handler RequestHandler* handler = NULL; const char* urlPart = NULL; PatternMap::const_iterator itr = sRootPatterns.begin(); while(itr != sRootPatterns.end()) { const string& pattern(itr->first); if(strncmp(url, pattern.c_str(), pattern.size()) == 0) { handler = itr->second; urlPart = url + pattern.size(); break; } ++itr; } if(handler == NULL) { itr = sExactPatterns.find(url); if(itr != sExactPatterns.end()) { handler = itr->second; urlPart = url + itr->first.size(); } } if(handler == NULL) { // 404 static char s404[] = "Request handler not found."; MHD_Response* resp = MHD_create_response_from_data(sizeof(s404)-1, s404, 0, 0); int res = MHD_queue_response(conn, 404, resp); assert(res == MHD_YES); MHD_destroy_response(resp); *con_cls = NULL; return MHD_YES; } ResponseData* rd = handler->handleRequest(urlPart, conn); if(rd) rd->handler = handler; *con_cls = rd; return MHD_YES; }
bool readOldElementsFile( const char *filename, ElementMap &table, const PatternMap &patterns) { FILE *tableFile = fopen(filename, "rb"); try { if (tableFile != NULL) { for (;;) { unsigned char numBuf[4]; unsigned int ldrawNum; int flags; char ldrawBase[128]; std::string ldrawFilename; Element element; size_t readSize = fread(numBuf, 1, 4, tableFile); PatternMap::const_iterator it1; if (readSize == 0) { // done break; } else if (readSize != 4) { throw "Error parsing %s.\n"; } ldrawNum = ((unsigned int)numBuf[0] << 24) | ((unsigned int)numBuf[1] << 16) | ((unsigned int)numBuf[2] << 8) | (unsigned int)numBuf[3]; flags = fgetc(tableFile); if (flags == EOF) { throw "Error parsing %s.\n"; } element.flags = (unsigned int)flags; element.lgeoName.reserve(10); if (!readString(tableFile, element.lgeoName)) { throw "Error parsing %s.\n"; } element.lgeoFilename = element.lgeoName + ".inc"; sprintf(ldrawBase, "%d", ldrawNum); ldrawFilename = ldrawBase; ldrawFilename += ".dat"; table[ldrawFilename] = element; it1 = patterns.find(element.lgeoName.substr(3)); if (it1 != patterns.end()) { const StringStringMap &strings = it1->second; for (StringStringMap::const_iterator it2 = strings.begin(); it2 != strings.end(); it2++) { element.lgeoFilename = "patterns/"; element.lgeoFilename += it2->second; element.lgeoFilename += ".inc"; element.lgeoName = "lg_"; element.lgeoName += it1->first; element.lgeoName += 'p'; element.lgeoName += it2->first; ldrawFilename = ldrawBase; ldrawFilename += 'p'; ldrawFilename += it2->first; ldrawFilename += ".dat"; table[ldrawFilename] = element; } } } fclose(tableFile); return true; } else { throw "Error reading %s.\n"; } } catch (const char *error) { printf(error, filename); if (tableFile != NULL) { fclose(tableFile); } return false; } }