void testing(string name, string mode, string text, bool del) { char buf[128]; stream str; str = stropen(name, mode); dprintf(0,"%s has strname -> %s\n", name, strname(str)); dprintf(0,"%s has strseek -> %d\n", name, strseek(str)?1:0); if (streq(mode, "a") || streq(mode, "r")) { printf("READING\n"); while (fgets(buf, 127, str) != NULL) printf("%s", buf); } if (streq(mode,"w") || streq(mode,"w!") || streq(mode,"s") || streq(mode,"a")) { printf("WRITING\n"); sprintf(buf, "%s\n", text); fputs(buf, str); } if (streq(mode, "s")) { printf("REWIND AND READING\n"); rewind(str); while (fgets(buf, 127, str) != NULL) printf("%s", buf); } if (del) strdelete(str,TRUE); else strclose(str); }
NetworkConfigEntity *ConfigurationManager::addNetwork(NetworkConfigEntity *, const RTNETADDRIPV4& networkId, const RTNETADDRIPV4& netmask, RTNETADDRIPV4& LowerAddress, RTNETADDRIPV4& UpperAddress) { static int id; char name[64]; RTStrPrintf(name, RT_ELEMENTS(name), "network-%d", id); std::string strname(name); id++; if (!LowerAddress.u) LowerAddress = networkId; if (!UpperAddress.u) UpperAddress.u = networkId.u | (~netmask.u); return new NetworkConfigEntity(strname, g_RootConfig, g_AnyClient, 5, networkId, netmask, LowerAddress, UpperAddress); }
static void exp_EndElementHandler(void * userdata, const XML_Char *name) { ExpatPPHandler * hand = (reinterpret_cast<ExpatPP *>(userdata))->handler; if(!hand) return; std::string strname((char *)name); makeStrLCase(strname); hand->elementEndHandler(strname); }
BaseConsoleCommand *ConsoleCommandPool::lookup(const char *name) { std::string strname(name); if( commands.find(strname) == commands.end() ) return NULL; else return commands[strname]; }
HostConfigEntity *ConfigurationManager::addHost(NetworkConfigEntity* pCfg, const RTNETADDRIPV4& address, ClientMatchCriteria *criteria) { static int id; char name[64]; RTStrPrintf(name, RT_ELEMENTS(name), "host-%d", id); std::string strname(name); id++; return new HostConfigEntity(address, strname, pCfg, criteria); }
static void exp_StartElementHandler(void * userdata, const XML_Char *name, const XML_Char **attrs) { ExpatPPHandler * hand = (reinterpret_cast<ExpatPP *>(userdata))->handler; if(!hand) return; std::string strname((char *)name); std::map<std::string,std::string> attrmap; if(attrs) while(*attrs) { std::string attrnamestr((char *)(*(attrs++))); std::string attrvalstr((char *)(*(attrs++))); makeStrLCase(attrnamestr); attrmap[attrnamestr] = attrvalstr; } makeStrLCase(strname); hand->elementStartHandler(strname, attrmap); }
static char *strname(u_char *buf, int offset) { char i; static char name[64]; static int n = 0; char *ptr = name + n, *p = (char *)(buf + offset); while ((i = *p) != 0) { if (*p & 0xc0) { return strname(buf, ntohs(*((uint16_t *)p)) & 0x03ff); } memcpy(ptr, ++p, i); p += i; ptr += i; *ptr++ = '.'; n += i+1; } name[strlen(name)-1] = '\0'; n = 0; return name; }
UINT CMainFrame::GetNodeID(const ConfigSection& node, LPCWSTR name) { UINT id = node->GetUInt32(name); std::wstring strname(node->GetString(name)); size_t pos = strname.find(L' '); if (id != 0 && pos != strname.npos) { strname = strname.substr(pos + 1); TRACE2("String ID: %d, %s\n", id, strname.c_str()); ASSERT(strname.find(L' ') == strname.npos && !strname.empty()); std::map<std::wstring, UINT>::iterator it = m_idnames.find(strname); ASSERT_MESSAGE(it == m_idnames.end() || it->second == id, "A string name must has only one ID value in the factory xml."); m_idnames[strname] = id; } return id; }
/** Call-back to add variables to parser automatically. We use different storage for constants and variables. Variables are stored in a vector of Variable object pointers. They must have the name x{index} where index is any non-negative integer. Note that the vector is resized to whatever the maximum index is. It is up to the user to make sure that indices are sequential without any gap. In case there is a gap in indices, those entries will remain unused. If the name starts with anything other than `x` or `y`, then it is taken to be a named constant, which must be set before any expression or variables and error is thrown. NOTE: this is called not on setting expression but on first attempt at evaluation of the same, i.e. when you access `value` of the Function object. */ double * _functionAddVar(const char *name, void *data) { Function* function = reinterpret_cast< Function * >(data); double * ret = NULL; string strname(name); // Names starting with x are variables, everything else is constant. if (strname[0] == 'x'){ int index = atoi(strname.substr(1).c_str()); if ((unsigned)index >= function->_varbuf.size()){ function->_varbuf.resize(index+1, 0); for (unsigned ii = 0; ii <= index; ++ii){ if (function->_varbuf[ii] == 0){ function->_varbuf[ii] = new Variable(); } } function->_numVar = function->_varbuf.size(); } ret = &(function->_varbuf[index]->value); } else if (strname[0] == 'y'){ int index = atoi(strname.substr(1).c_str()); if ((unsigned)index >= function->_pullbuf.size()){ function->_pullbuf.resize(index+1, 0 ); for (unsigned ii = 0; ii <= index; ++ii){ if (function->_pullbuf[ii] == 0){ function->_pullbuf[ii] = new double(); } } } ret = function->_pullbuf[index]; } else { cerr << "Got an undefined constant " << name << endl << "You must define the constants beforehand using LookupField c: c[name]" " = value" << endl; throw mu::ParserError("Undefined constant."); } return ret; }
char * captoinfo(char *cap) { char *info, *ip, *token, *val, *p, tok[3]; const char *name; size_t len, lp, nl, vl, rl; int defs[__arraycount(def_infos)], fv; _DIAGASSERT(cap != NULL); len = strlen(cap) * 2; len += __arraycount(def_infos) * (5 + 4 + 3); /* reserve for defs */ info = ip = malloc(len); if (info == NULL) return NULL; memset(defs, 0, sizeof(defs)); lp = 0; tok[2] = '\0'; for (token = _ti_get_token(&cap, ':'); token != NULL; token = _ti_get_token(&cap, ':')) { if (token[0] == '\0') continue; name = token; val = p = NULL; fv = nl = 0; if (token[1] != '\0') { tok[0] = token[0]; tok[1] = token[1]; nl = 1; if (token[2] == '\0') { name = flagname(tok); val = NULL; } else if (token[2] == '#') { name = numname(tok); val = token + 2; } else if (token[2] == '=') { name = strname(tok); val = strval(token + 2); fv = 1; } else nl = 0; } /* If not matched we may need to convert padding still. */ if (nl == 0) { p = strchr(name, '='); if (p != NULL) { val = strval(p); *p = '\0'; fv = 1; } } /* See if this sets a default. */ for (nl = 0; nl < __arraycount(def_infos); nl++) { if (strcmp(name, def_infos[nl].name) == 0) { defs[nl] = 1; break; } } nl = strlen(name); if (val == NULL) vl = 0; else vl = strlen(val); rl = nl + vl + 3; /* , \0 */ if (lp + rl > len) { if (rl < 256) len += 256; else len += rl; p = realloc(info, len); if (p == NULL) { if (fv == 1) free(val); return NULL; } info = p; } if (ip != info) { *ip++ = ','; *ip++ = ' '; } strcpy(ip, name); ip += nl; if (val != NULL) { strcpy(ip, val); ip += vl; if (fv == 1) free(val); } } /* Add any defaults not set above. */ for (nl = 0; nl < __arraycount(def_infos); nl++) { if (defs[nl] == 0) { *ip++ = ','; *ip++ = ' '; strcpy(ip, def_infos[nl].name); ip += strlen(def_infos[nl].name); *ip++ = '='; strcpy(ip, def_infos[nl].cap); ip += strlen(def_infos[nl].cap); } } *ip = '\0'; return info; }
int main(int argc, char *argv[]) { int fd; struct sockaddr_in sa; u_char buf[MAXLINE]; u_char *ptr; if (argc != 3) err_quit("udpdns <qeury_name> <dns_server>"); if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) err_sys("socket error"); bzero(&sa, sizeof(sa)); sa.sin_family = AF_INET; sa.sin_port = htons(DNSPORT); if (inet_aton(argv[2], &sa.sin_addr) != 1) err_quit("inet_aton error"); /* Wrap DNS Query Packet */ ptr = buf; *((uint16_t *)ptr) = htons(1234); ptr += 2; *((uint16_t *)ptr) = htons(0x0100); /* RD=1 (recursion desired) */ ptr += 2; *((uint16_t *)ptr) = htons(1); /* QDCOUNT */ ptr += 2; *((uint16_t *)ptr) = 0; /* ANCOUNT */ ptr += 2; *((uint16_t *)ptr) = 0; /* NSCOUNT */ ptr += 2; *((uint16_t *)ptr) = 0; /* ARCOUNT */ ptr += 2; int i, len; char *str[20], *p; str[0] = strtok(argv[1], "."); p = (str[0] == NULL) ? argv[1] : str[0]; len = strlen(p); snprintf((char *)ptr++, 2+len, "%c%s", (u_char)len, p); ptr += len; if (str[0] == NULL) { *ptr++ = '\0'; } else { for (i = 1; i < 20; i++) { if ((str[i] = strtok(NULL, ".")) == NULL) break; len = strlen(str[i]); snprintf((char *)ptr++, 2+len, "%c%s", (u_char)len, str[i]); ptr += len; } *ptr++ = '\0'; } *((uint16_t *)ptr) = htons(1); /* query type = A */ ptr += 2; *((uint16_t *)ptr) = htons(1); /* query class = 1 */ ptr += 2; len = ptr - buf; if (sendto(fd, buf, len, 0, (struct sockaddr *)&sa, sizeof(sa)) != len) err_sys("sendto error"); if ((len = recvfrom(fd, buf, MAXLINE, 0, NULL, NULL)) < 0) err_sys("recvfrom error"); buf[len] = 0; ptr = buf; ptr += 2; if (*((uint16_t *)ptr) & 0x0200) /* truncated */ err_msg("message truncated"); int ancount; ptr += 4; /* must get the ANCOUNT */ ancount = ntohs(*((uint16_t *)ptr)); printf("ANCOUNT = %d\n", ancount); ptr += 2+4; /* pass other XXCOUNT */ /* Quenstion section */ while (*ptr++ != '\0') ; printf("QType = %d\n", ntohs(*((uint16_t *)ptr))); ptr += 2; printf("QClass = %d\n", ntohs(*((uint16_t *)ptr))); ptr += 2; /* answer section */ char *name; int type; while (ancount-- > 0) { if (*ptr & 0xc0) { /* DNS message compression */ /* The 14 bits that follow in the pointer specify an offset in the DNS message */ name = strname(buf, (ntohs(*((uint16_t *)ptr)) & 0x03ff)); ptr += 2; } else { /* full domain name */ printf("%s\n", strname(ptr, 0)); while (*ptr++ != '\0') ; } type = ntohs(*((uint16_t *)ptr)); ptr += 4; /* pass the type & class */ printf("TTL = %d sec\n", ntohl(*((uint32_t *)ptr))); ptr += 4; len = ntohs(*((uint16_t *)ptr)); ptr += 2; if (type == 1) printf("Name: %s\nAddress: %s\n", name, inet_ntoa(*((struct in_addr *)ptr))); else printf("Name: %s\nType: %d\n", name, type); ptr += len; } return 0; }
bool LoadOBJFromFile(const char * const filename, IndexedModel& result) { std::string strname(filename); if (details::bufferedModels.count(strname)) { IndexedModel exst = details::bufferedModels.at(filename); result.indices = exst.indices; result.maxDepth = exst.maxDepth; result.maxHeight = exst.maxHeight; result.maxWidth = exst.maxWidth; result.vertices = exst.vertices; return true; } std::ifstream file; file.open(filename); if (!file.is_open()) { MessageBox( NULL, ("Could not load model file: " + std::string(filename)).c_str(), FATALERROR, FATALLAYOUT); return false; } // Initialize placeholder vectors and the indices std::vector<disorganizedData::disorganized_Vertex> disVertices; std::vector<disorganizedData::disorganized_Texcoord> disTexcoords; std::vector<disorganizedData::disorganized_Normal> disNormals; std::vector<organizedData::OBJIndex> orgIndices; std::string line; for (; std::getline(file, line); ) { if (line[0] == '#') continue; std::vector<std::string> splitContents = split(line, ' '); std::string prefix = splitContents[0]; if (prefix[0] == 'v') { if (prefix[1] == 't') { disorganizedData::disorganized_Texcoord rawTexcoord = {}; rawTexcoord.u = std::stof(splitContents[1]); rawTexcoord.v = 1 - std::stof(splitContents[2]); disTexcoords.push_back(rawTexcoord); } else if (prefix[1] == 'n') { disorganizedData::disorganized_Normal rawNormal = {}; rawNormal.nx = std::stof(splitContents[1]); rawNormal.ny = std::stof(splitContents[2]); rawNormal.nz = std::stof(splitContents[3]); disNormals.push_back(rawNormal); } else { disorganizedData::disorganized_Vertex rawVertex = {}; rawVertex.x = std::stof(splitContents[1]); rawVertex.y = std::stof(splitContents[2]); rawVertex.z = std::stof(splitContents[3]); disVertices.push_back(rawVertex); } } else if (prefix[0] == 'f') { for (int i = 1; i < 4; i++) { organizedData::OBJIndex objIndex = {}; std::vector<std::string> splitIndex = split(splitContents[i], '/'); if (splitIndex[0].size() != 0) { objIndex.vertexIndex = std::stoi(splitIndex[0]) - 1; } if (splitIndex[1].size() != 0) { objIndex.texcoordIndex = std::stoi(splitIndex[1]) - 1; } if (splitIndex[2].size() != 0) { objIndex.normalIndex = std::stoi(splitIndex[2]) - 1; } orgIndices.push_back(objIndex); } } } file.close(); float minX, maxX, minY, maxY, minZ, maxZ; minX = 0.0f; maxX = 0.0f; minY = 0.0f; maxY = 0.0f; minZ = 0.0f; maxZ = 0.0f; for (size_t i = 0; i < orgIndices.size(); i++) { Vertex finalVertex = {}; if (disVertices.size() != 0) { finalVertex.x = disVertices[orgIndices[i].vertexIndex].x; finalVertex.y = disVertices[orgIndices[i].vertexIndex].y; finalVertex.z = disVertices[orgIndices[i].vertexIndex].z; if (finalVertex.x > maxX) maxX = finalVertex.x; if (finalVertex.x < minX) minX = finalVertex.x; if (finalVertex.y > maxY) maxY = finalVertex.y; if (finalVertex.y < minY) minY = finalVertex.y; if (finalVertex.z > maxZ) maxZ = finalVertex.z; if (finalVertex.z < minZ) minZ = finalVertex.z; } if (disTexcoords.size() != 0) { finalVertex.u = disTexcoords[orgIndices[i].texcoordIndex].u; finalVertex.v = disTexcoords[orgIndices[i].texcoordIndex].v; } if (disNormals.size() != 0) { finalVertex.nx = disNormals[orgIndices[i].normalIndex].nx; finalVertex.ny = disNormals[orgIndices[i].normalIndex].ny; finalVertex.nz = disNormals[orgIndices[i].normalIndex].nz; } result.vertices.push_back(finalVertex); result.indices.push_back(i); } result.maxDepth = std::abs(maxZ - minZ); result.maxHeight = std::abs(maxY - minY); result.maxWidth = std::abs(maxX - minX); details::bufferedModels.insert(std::pair<std::string, IndexedModel>(std::string(filename), result)); return true; }