Пример #1
0
void SBServerParseKeyVals(SBServer server, char *keyvals)
{
    char *k, *v;

    k = mytok(++keyvals,'\\'); //skip over starting backslash
    while (k != NULL)
    {
        v = mytok(NULL,'\\');
        if (v == NULL)
            v = "";
        if (CheckValidKey(k))
            SBServerAddKeyValue(server, k, v);
        k = mytok(NULL,'\\');
    }
}
Пример #2
0
void save_line(char* buffer , FILE *fout){
    char temp[78];
    char tok_buffer[78];
    char rest_buffer[78];
    mytok(buffer,tok_buffer,rest_buffer);
    mytok(rest_buffer,tok_buffer,rest_buffer);
    strcpy(temp,rest_buffer);
    printf("DATA: %s\n",temp);
    if(fout!=NULL) {
        fprintf(fout,"%s\n",temp);
        printf("syst: line saved:%s\n",temp);
    }
    else{
        printf("error during writing...\n");
        exit(0);
    }
}
Пример #3
0
void procesar(char *result, char *x, int len) {
	char buf[2000];
	memcpy(buf, x, len);
	buf[len]='\0';
	char *a = mytok(buf,' ');
	char *b = mytok(0,' ');
	char *n = mytok(0,' ');
	BigInt *aa=bnNewBigInt(100,0);
    BigInt *bb=bnNewBigInt(100,0);
    BigInt *cc=bnNewBigInt(100,0);
    BigInt *ans=bnNewBigInt(100,0);
    
    bnStrToInt(aa, a);
    bnStrToInt(bb, b);
    bnStrToInt(cc, n);
    bnPowModInt(ans, aa, bb, cc);
    bnIntToStr(result,ans);
    
    bnDelBigInt(aa);
    bnDelBigInt(bb);
    bnDelBigInt(cc);
    bnDelBigInt(ans);
}
Пример #4
0
int main(int argc, char **argv)
{

	int listenfd, connfd, n, headerSize, fileSize=0;
	char buffer[BUFFSZ];
	char *response;
	char *filepath;
	char *filename;
	char *filetype;
	
	char *status200= "HTTP/1.0 200 OK\r\n";
	char *status400= "HTTP/1.0 400 Bad Request\r\n";
	char *status404= "HTTP/1.0 404 Not Found\r\n";
	char *status403= "HTTP/1.0 403 Forbidden\r\n";
	char *server	= "Server: SERVERNAME\r\nConnection: CONNECTION\r\nContent-Length: ";
	char *html		= "\r\nContent-Type: text/html\r\n\r\n";
	char *jpeg		= "\r\nContent-Type: image/jpeg\r\n\r\n";
	char *gif 		= "\r\nContent-Type: image/gif\r\n\r\n";
	char *ico 		= "\r\nContent-Type: image/x-icon\r\n\r\n";
	char *js 		= "\r\nContent-Type: text/javascript\r\n\r\n";
	char *css 		= "\r\nContent-Type: text/css\r\n\r\n";
	char *txt 		= "\r\nContent-Type: text/plain\r\n\r\n";
	/*Add more mimetypes here, but also add filetype in Match file with mimetypes section*/


	FILE *file;
	int fileErr;
	char *mode = "r";
			
	pid_t childpid;

	socklen_t clilen;

	struct sockaddr_in cliaddr, servaddr;

	listenfd = Socket (AF_INET, SOCK_STREAM, 0);
	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_addr.s_addr = htonl (INADDR_ANY);
	servaddr.sin_port = htons (MYPORT);
	Bind(listenfd, (SA *) &servaddr, sizeof(servaddr));
	Listen(listenfd, LISTENQ);
	
	for ( ; ; ) {
		clilen = sizeof(cliaddr);
		connfd = Accept(listenfd, (SA *) &cliaddr, &clilen);
		if ( (childpid = Fork()) == 0) { /* child process */
			Close(listenfd);				/* close listening socket */			
			n = recv(connfd, buffer, sizeof(buffer), 0);
			  						
			if(n < 0) printf("can't receive from client"); 
			else buffer[n] = '\0';

			/*printf("-----------------------------------\n");
  			printf("SERVER GOT MESSAGE:\n%s", buffer);  */
			
			
			/*check for "GET " and "HTTP/1." in the first line of REQUEST*/
			if(strstr(buffer,"GET ")==NULL || strstr(buffer," HTTP/1.")==NULL){
				fprintf(stderr,"400! %s Bad Request\n");
				strcpy(buffer,status400);

				send(connfd, buffer, strlen(buffer)+fileSize, 0);
				exit(1);			
			}
			
			
			filename=mytok(buffer, ' ', 1);
			
			/*-----------------------Do address redirects here--------------------------*/
			if(strcmp(filename,"/")==0) filename="/index.html";
			
			
			/*--------------------------------------------------------------------------*/
			
			/*-------------------Attach the WWWFOLDER to the path-----------------------*/
			filepath=(char *)malloc(sizeof(WWWFOLDER)+sizeof(char)*strlen(filename));
			strcpy(filepath,WWWFOLDER);
			strcat(filepath,filename);
			/*printf("filename:\t'%s'\n",filename);*/
			
			/*--------------------------------------------------------------------------*/
			
			memset(buffer,0,sizeof(buffer));
			/*printf("buffer:\t'%s'\n",buffer);*/
			
			file = fopen(filepath, mode);			

			/*----------------------No object or permission issues----------------------*/
			if (file == NULL) {
				fileErr = errno;
				
				if(fileErr == EACCES){/*Check if it was due to permission*/
					fprintf(stderr,"403! '%s' is Forbidden\n", filename);
					strcpy(buffer,status404);

					free(filepath);
					free(filename);
					filename="/403.html";
					filepath=(char *)malloc(sizeof(WWWFOLDER)+sizeof(char)*strlen(filename));

					strcpy(filepath,WWWFOLDER);
					strcat(filepath,filename);
					file = fopen(filepath, mode);
					if (file == NULL){
						fprintf(stderr,"we cant find our 403 page!\n");
						send(connfd, buffer, strlen(buffer)+fileSize, 0);
						exit(1);
					}	
				}
				else{/*Assume that all other errors mean the file is not there..*/
					fprintf(stderr, "404! '%s' is Not Found\n", filename);
					strcpy(buffer,status404);

					free(filepath);
					free(filename);
					filename="/404.html";
					filepath=(char *)malloc(sizeof(WWWFOLDER)+sizeof(char)*strlen(filename));

					strcpy(filepath,WWWFOLDER);
					strcat(filepath,filename);
					file = fopen(filepath, mode);
					if (file == NULL){
						fprintf(stderr,"we cant find our 404 page!\n");
						send(connfd, buffer, strlen(buffer)+fileSize, 0);
						exit(1);
					}
				}
			}
			/*--------------------------------------------------------------------------*/
			else
				strcpy(buffer,status200);

			
			/*extract filetype*/
			if(index(filename,'.')!=NULL)
				filetype=mytok(filename, '.', 1);
			else
				filetype="";
				
			strcat(buffer,server);
			
			fseek(file, 0, SEEK_END);
			fileSize = ftell(file);
			rewind(file);
			
		
			sprintf(buffer+strlen(buffer), "%d", fileSize);			
				
			/*-----------------------Match file with mimetypes-------------------------*/
			if (strcmp(filetype,"html")==0)
				strcat(buffer,html);
			
			else if (strcmp(filetype,"jpeg")==0)
				strcat(buffer,jpeg);
			
			else if (strcmp(filetype,"gif")==0)
				strcat(buffer,gif);
			
			else if (strcmp(filetype,"ico")==0)
				strcat(buffer,ico);
				
			else if (strcmp(filetype,"js")==0)
				strcat(buffer,js);
				
			else if (strcmp(filetype,"css")==0)
				strcat(buffer,css);			
						
			else if (strcmp(filetype,"txt")==0 || strcmp(filetype,"")==0)
				strcat(buffer,txt);			
			/*If you add more filetypes here also define them at the beginning of main()*/
			/*--------------------------------------------------------------------------*/
				
			/*printf("buff:\t'%s'\n",buffer);*/
			
			
			response=(char*) malloc(sizeof(buffer)+fileSize);
			memset(response,0,sizeof(response));
			
			memcpy(response, buffer, strlen(buffer)+1);
			headerSize=strlen(response);
			fread(response+strlen(response), 1, fileSize, file);
			fclose(file);
			
			
			/*
			printf("-----------------------------------\n");
			if (strcmp(filetype,"js")==0)
			printb(response,headerSize+fileSize);
 			printf("\n-----------------------------------\n");
 			*/
 			
			n = send(connfd, response, headerSize+fileSize, 0);			
			if(n < 0) printf("can't send to client"); 
			
			free(filename);
			free(filetype);
			free(filepath);
			free(response);
			
			exit (0);
		}
		Close(connfd);						/* parent closes connected socket */
	}
}
Пример #5
0
int Observable2D::ParseObservable2D(std::string& type, 
                                     boost::tokenizer<boost::char_separator<char> >* tok, 
                                     boost::tokenizer<boost::char_separator<char> >::iterator& beg, 
                                     std::string& infilename, 
                                     std::ifstream& ifile,
                                     int lineNo,
                                     int rank)
{
    if (infilename.find("\\/") == std::string::npos) filepath = infilename.substr(0, infilename.find_last_of("\\/") + 1);
    if (std::distance(tok->begin(), tok->end()) < 12) {
        setName(*beg);
        ++beg;
        if (std::distance(tok->begin(), tok->end()) < 4) {
            if(rank == 0) throw std::runtime_error("ERROR: lack of information on " + name + " in " + infilename + " at line number" + boost::lexical_cast<std::string>(lineNo));
            else sleep (2);
        }
        std::string toMCMC = *beg;
        if (toMCMC.compare("MCMC") == 0)
            setTMCMC(true);
        else if (toMCMC.compare("noMCMC") == 0)
            setTMCMC(false);
        else {
            if (rank == 0) throw std::runtime_error("ERROR: wrong MCMC flag in Observable2D" + name + " at line number:" + boost::lexical_cast<std::string>(lineNo) + " of file " + infilename + ".\n");
            else sleep(2);
        }
        
        ++beg;
        setDistr(*beg);
        if (distr.compare("file") == 0) {
            if (std::distance(tok->begin(), tok->end()) < 6) {
                if(rank == 0) throw std::runtime_error("ERROR: lack of information on "+ *beg + " in " + infilename);
                else sleep (2);
            }
            setFilename(filepath + *(++beg));
            setHistoname(*(++beg));
        }

        std::vector<double> min(2, 0.);
        std::vector<double> max(2, 0.);
        std::vector<double> ave(2, 0.);
        std::vector<double> errg(2, 0.);
        std::vector<double> errf(2, 0.);
        std::vector<std::string> thname(2, "");
        std::vector<std::string> label(2, "");
        std::vector<std::string> type2D(2, "");
        std::string line;
        size_t pos = 0;
        boost::char_separator<char> sep(" \t");
        for (int i = 0; i < 2; i++) {
            IsEOF = getline(ifile, line).eof();
            if (line.empty() || line.at(0) == '#') {
                if (rank == 0) throw std::runtime_error("ERROR: no comments or empty lines in Observable2D please! In file " + infilename + " at line number:" + boost::lexical_cast<std::string>(lineNo) + ".\n");
                else sleep(2);
            }
            lineNo++;
            boost::tokenizer<boost::char_separator<char> > mytok(line, sep);
            beg = mytok.begin();
            type2D[i] = *beg;
            if (type2D[i].compare("Observable") != 0 && type2D[i].compare("BinnedObservable") != 0 && type2D[i].compare("FunctionObservable") != 0) {
                if (rank == 0) throw std::runtime_error("ERROR: in line no." + boost::lexical_cast<std::string>(lineNo) + " of file " + infilename + ", expecting an Observable or BinnedObservable or FunctionObservable type here...\n");
                else sleep(2);
            }
            ++beg;
            thname[i] = *beg;
            ++beg;
            label[i] = *beg;
            while ((pos = label[i].find("~", pos)) != std::string::npos)
                label[i].replace(pos++, 1, " ");
            ++beg;
            min[i] = atof((*beg).c_str());
            ++beg;
            max[i] = atof((*beg).c_str());
            if (distr.compare("weight") == 0) {
                ++beg;
                ave[i] = atof((*beg).c_str());
                ++beg;
                errg[i] = atof((*beg).c_str());
                ++beg;
                errf[i] = atof((*beg).c_str());
                if (errg[i] == 0. && errg[i] == 0.) {
                    if (rank == 0) throw std::runtime_error("ERROR: The Gaussian and flat error in weight for " + name + " cannot both be 0. in the " + infilename + " file, line number:" + boost::lexical_cast<std::string>(lineNo) + ".\n");
                    else sleep(2);
                }
            } else if (distr.compare("noweight") == 0 || distr.compare("file") == 0) {
                if (type2D[i].compare("BinnedObservable") == 0 || type2D[i].compare("FunctionObservable") == 0) {
                    ++beg;
                    ++beg;
                    ++beg;
                }
            } else {
                if (rank == 0) throw std::runtime_error("ERROR: wrong distribution flag in " + name + " in file " + infilename + ".\n");
                else sleep(2);
            }
            if (type2D[i].compare("BinnedObservable") == 0) {
                ++beg;
                bin_min[i] = atof((*beg).c_str());
                ++beg;
                bin_max[i] = atof((*beg).c_str());
            } else if (type2D[i].compare("FunctionObservable") == 0) {
                ++beg;
                bin_min[i] = atof((*beg).c_str());
                ++beg;
            }
        }
        setObsType(type2D[0]);
        obsType2 = type2D[1];
        setThname(thname[0]);
        thname2 = thname[1];
        setLabel(label[0]);
        label2 = label[1];
        setMin(min[0]);
        min2 = min[1];
        setMax(max[0]);
        max2= max[1];
        setAve(ave[0]);
        ave2 = ave[1];
        setErrg(errg[0]);
        errg2 = errg[1];
        setErrf(errf[0]);
        errf2 = errf[1];
        if (distr.compare("file") == 0) {
            setLikelihoodFromHisto(filename, histoname);
            if (rank == 0) std::cout << "added input histogram " << filename << "/" << histoname << std::endl;
        }
        return lineNo;
    } else {
        beg = ParseObservable(type, tok, beg, filepath, filename, rank);
        ++beg;
        std::string distr = *beg;
        if (distr.compare("file") == 0) {
            if (std::distance(tok->begin(), tok->end()) < 14) {
                if (rank == 0) throw std::runtime_error("ERROR: lack of information on " + *beg + " in " + infilename + ".\n");
                else sleep(2);
            }
            setFilename(filepath + *(++beg));
            setHistoname(*(++beg));
            setLikelihoodFromHisto(filename, histoname);
            if (rank == 0) std::cout << "added input histogram " << filename << "/" << histoname << std::endl;
        } else if (distr.compare("noweight") == 0) {
        } else { 
            if (rank == 0) throw std::runtime_error("ERROR: wrong distribution flag in " + name);
            else sleep(2);
        }
        setDistr(distr);
        ++beg;
        thname2 = *beg;
        ++beg;
        std::string label = *beg;
        size_t pos = 0;
        while ((pos = label.find("~", pos)) != std::string::npos)
            label.replace(pos, 1, " ");
        label2 = label;
        ++beg;
        min2 = atof((*beg).c_str());
        ++beg;
        max2 = atof((*beg).c_str());
        ++beg;
        if (beg != tok->end())
            if (rank == 0) std::cout << "WARNING: unread information in observable2D " << name << std::endl;
        return lineNo;
    }
}
void CorrelatedGaussianParameters::ParseCGP(std::vector<ModelParameter>& ModPars, 
                                            std::ifstream& ifile, 
                                            boost::tokenizer<boost::char_separator<char> >::iterator & beg,
                                            int rank)
{
    name = *beg;
    ++beg;
    int size = atoi((*beg).c_str());
    int nlines = 0;
    std::string line;
    bool IsEOF;
    boost::char_separator<char>sep(" \t");
    for (int i = 0; i < size; i++) {
        IsEOF = getline(ifile, line).eof();
        if (line.empty() || line.at(0) == '#') {
            if (rank == 0) std::cout << "ERROR: no comments or empty lines in CorrelatedGaussianParameters please!"
                    << std::endl;
            exit(EXIT_FAILURE);
        }
        lineNo++;
        boost::tokenizer<boost::char_separator<char> > tok(line, sep);
        beg = tok.begin();
        std::string type = *beg;
        ++beg;
        if (type.compare("ModelParameter") != 0)
            if (rank == 0) throw std::runtime_error("ERROR: in line no." + boost::lexical_cast<std::string>(lineNo) + " of file " + filename + ", expecting a ModelParameter type here...\n");
        ModelParameter tmpMP;
        beg = tmpMP.ParseModelParameter(beg);
        if (beg != tok.end())
                if (rank == 0) std::cout << "WARNING: unread information in parameter " << tmpMP.getname() << std::endl;
        tmpMP.setCgp_name(name);
        AddPar(tmpMP);
        nlines++;
    }
    if (nlines > 1) {
        gslpp::matrix<double> myCorr(gslpp::matrix<double>::Id(nlines));
        int ni = 0;
        for (int i = 0; i < size; i++) {
            IsEOF = getline(ifile, line).eof();
            if (line.empty() || line.at(0) == '#') {
                if (rank == 0) std::cout << "ERROR: no comments or empty lines in CorrelatedGaussianParameters please!"
                        << std::endl;
                exit(EXIT_FAILURE);
            }
            lineNo++;
            boost::tokenizer<boost::char_separator<char> > mytok(line, sep);
            beg = mytok.begin();
            int nj = 0;
            for (int j = 0; j < size; j++) {
                if ((*beg).compare(0, 1, "0") == 0
                        || (*beg).compare(0, 1, "1") == 0
                        || (*beg).compare(0, 1, "-") == 0) {
                    if (std::distance(mytok.begin(), mytok.end()) < size && rank == 0) throw std::runtime_error(("ERROR: Correlation matrix is of wrong size in Correlated Gaussian Parameters: " + name).c_str());
                    myCorr(ni, nj) = atof((*beg).c_str());
                    nj++;
                    beg++;
                } else {
                    if (rank == 0) std::cout << "ERROR: invalid correlation matrix for "
                            << name << ". Check element (" << ni + 1 << "," << nj + 1 << ") in line number " + boost::lexical_cast<std::string>(lineNo) << std::endl;
                    exit(EXIT_FAILURE);
                }
            }
            ni++;
        }
        DiagonalizePars(myCorr);
        ModPars.insert(ModPars.end(), getDiagPars().begin(), getDiagPars().end());

    } else {
        if (rank == 0) std::cout << "\nWARNING: Correlated Gaussian Parameters " << name.c_str() << " defined with less than two correlated parameters. The set is being marked as normal Parameters." << std::endl;
        if (getPars().size() == 1) ModPars.push_back(ModelParameter(getPar(0)));
        for (int i = 0; i < size; i++) {
            IsEOF = getline(ifile, line).eof();
            lineNo++;
        }
    }
}