Exemple #1
0
bool Config::readConfigFile(std::string configPath)
{
	std::ifstream file(configPath.c_str(), std::ios::in);

	if (!file.is_open())
	{
		return false;
	}

	Vita::string line;
	Vita::string key;
	Vita::string value;
	std::vector<Vita::string> parts;

	while (!file.eof())
	{
		getline(file, line);

		size_t pos = line.find_first_of('#');
		if (pos != std::string::npos)
		{
			line.erase(pos);
		}

		if (line.trim().empty())
		{
			continue;
		}

		parts = line.explode("=");
		key = parts.front().trim().toLower();
		value = parts.back().trim("\"").trim();

		// Read in extern file
		if (value.size() > 1 && value.substr(0, 1) == ">")
		{
			std::ifstream file(getRightConfigPath(value.substr(1), basePath_).c_str(),
					std::ios::in);
			if (!file.is_open())
			{
				return false;
			}

			Vita::string line;
			value.clear();

			while (!file.eof())
			{
				getline(file, line);

				size_t pos = line.find_first_of('#');
				if (pos != std::string::npos)
				{
					line.erase(pos);
				}

				if (line.trim().empty())
				{
					continue;
				}

				value += " " + line.trim();
			}

			value = value.trim();

			// remove all multiple spaces
			while (value.find("  ") != std::string::npos)
			{
				value = value.replace("  ", " ");
			}
		}

		if (key == "include")
		{
			readConfigFile(getRightConfigPath(value, basePath_));
		}
		else if (key == "name")
		{
			name_ = value.toLower();
		}
		else if (key == "version")
		{
			version_ = value;
		}
		else if (key == "info")
		{
			info_ = value;
		}
		else if (key == "priority")
		{
			priority_ = value.convert<int>();
		}
		else if (key == "freedriver")
		{
			value = value.toLower();

			if (value == "false")
			{
				freedriver_ = false;
			}
			else if (value == "true")
			{
				freedriver_ = true;
			}
		}
		else if (key == "classids")
		{
			// Add new HardwareIDs group to vector if vector is not empty
			if (!hwdIDs_.back().classIDs.empty())
			{
				Config::HardwareIDs hwdID;
				hwdIDs_.push_back(hwdID);
			}

			hwdIDs_.back().classIDs = splitValue(value);
		}
		else if (key == "vendorids")
		{
			// Add new HardwareIDs group to vector if vector is not empty
			if (!hwdIDs_.back().vendorIDs.empty())
			{
				Config::HardwareIDs hwdID;
				hwdIDs_.push_back(hwdID);
			}

			hwdIDs_.back().vendorIDs = splitValue(value);
		}
		else if (key == "deviceids")
		{
			// Add new HardwareIDs group to vector if vector is not empty
			if (!hwdIDs_.back().deviceIDs.empty())
			{
				Config::HardwareIDs hwdID;
				hwdIDs_.push_back(hwdID);
			}

			hwdIDs_.back().deviceIDs = splitValue(value);
		}
		else if (key == "blacklistedclassids")
		{
			hwdIDs_.back().blacklistedClassIDs = splitValue(value);
		}
		else if (key == "blacklistedvendorids")
		{
			hwdIDs_.back().blacklistedVendorIDs = splitValue(value);
		}
		else if (key == "blacklisteddeviceids")
		{
			hwdIDs_.back().blacklistedDeviceIDs = splitValue(value);
		}
		else if (key == "mhwddepends")
		{
			dependencies_ = splitValue(value);
		}
		else if (key == "mhwdconflicts")
		{
			conflicts_ = splitValue(value);
		}
	}

	// Append * to all empty vectors
	for (std::vector<Config::HardwareIDs>::iterator hwdID = hwdIDs_.begin();
			hwdID != hwdIDs_.end(); hwdID++)
	{
		if ((*hwdID).classIDs.empty())
		{
			(*hwdID).classIDs.push_back("*");
		}

		if ((*hwdID).vendorIDs.empty())
		{
			(*hwdID).vendorIDs.push_back("*");
		}

		if ((*hwdID).deviceIDs.empty())
		{
			(*hwdID).deviceIDs.push_back("*");
		}
	}

	if (name_.empty())
	{
		return false;
	}

	return true;
}
Exemple #2
0
QString QtRPT::sectionField(QString value, bool exp, bool firstPass) {
    QString tmpStr;
    QStringList res;
    bool aggregate = false;

    for (int i = 0; i < value.size(); ++i) {
        if (value.at(i) != '[' && value.at(i) != ']' &&
            value.at(i) != '<' && value.at(i) != '>' && !aggregate)
            tmpStr += value.at(i);
        else if ((value.at(i) == '[' || value.at(i) == ']') && aggregate)
             tmpStr += value.at(i);
        else if (value.at(i) != '<' && value.at(i) != '>' && aggregate)
             tmpStr += value.at(i);
        else {
            if (exp && (value.at(i) == '<' || value.at(i) == '>') )
                tmpStr += value.at(i);
            if (value.at(i) == ']' && !aggregate) {
                tmpStr += value.at(i);
                res << tmpStr;
                tmpStr.clear();
            }
            if (value.at(i) == '[' && !aggregate) {
                if (!tmpStr.isEmpty())
                    res << tmpStr;
                tmpStr.clear();
                tmpStr += value.at(i);
            }
            if (!exp && value.at(i) == '<') {
                aggregate = true;
                if (!tmpStr.isEmpty())
                    res << tmpStr;
                tmpStr.clear();
                tmpStr += value.at(i);
            }
            if (!exp && value.at(i) == '>') {
                aggregate = false;
                tmpStr += value.at(i);
                res << tmpStr;
                tmpStr.clear();
            }
        }
    }
    if (!tmpStr.isEmpty()) res << tmpStr;    

    tmpStr.clear();
    for (int i = 0; i < res.size(); ++i) {
        if (res.at(i).contains("[") && res.at(i).contains("]") && !res.at(i).contains("<") ) {
            QString tmp = sectionValue(res.at(i));
            bool ok;
            if (exp) { //Process highlighting and visibility
                tmp.toDouble(&ok);
                if (!ok) tmp.toFloat(&ok);
                if (!ok) tmp.toInt(&ok);
                if (!ok)
                    tmpStr += "'"+tmp+"'";
                else
                    tmpStr += tmp;
            } else { //Process usuall field
                if (firstPass) { //Process during first pass to calculate aggregate values
                    double v = tmp.toDouble(&ok);
                    if (ok) {
                        AggregateValues av;
                        av.paramName = res.at(i);
                        av.paramValue = v;
                        av.lnNo = m_recNo;
                        bool founded = false;
                        for (int j = 0; j < listOfPair.size(); ++j) {
                            if (listOfPair.at(j).lnNo == av.lnNo && listOfPair.at(j).paramName == av.paramName)
                                founded = true;
                        }
                        if (!founded)
                            listOfPair.append(av);
                    }
                }
                tmpStr += tmp;
            }
        } else {
            if (res[i].contains("<Date>"))
                res[i] = res[i].replace("<Date>",processFunctions("Date").toString());
            if (res[i].contains("<Time>"))
                res[i] = res[i].replace("<Time>",QTime::currentTime().toString());
            if (res[i].contains("<Page>"))
                res[i] = res[i].replace("<Page>",QString::number(curPage));
            if (res[i].contains("<TotalPages>"))
                res[i] = res[i].replace("<TotalPages>",QString::number(totalPage));
            if (res[i].contains("<LineNo>"))
                res[i] = res[i].replace("<LineNo>",QString::number(m_recNo+1));

            if (res[i].contains("<") && res[i].contains(">")) {
                QString formulaStr=res[i];
                QScriptEngine myEngine;

                QStringList tl = splitValue(formulaStr);
                for (int j = 1; j < tl.size(); ++j) {
                    if (tl.at(j).contains("[") &&
                        tl.at(j).contains("]") &&
                        !tl.at(j-1).toUpper().contains("SUM") &&
                        !tl.at(j-1).toUpper().contains("AVG") &&
                        !tl.at(j-1).toUpper().contains("COUNT")
                    )
                        formulaStr.replace(tl.at(j), sectionValue(tl.at(j)));
                }

                QScriptValue fun = myEngine.newFunction(funcAggregate);
                myEngine.globalObject().setProperty("Sum", fun);

                formulaStr = formulaStr.replace("Sum(","Sum(0,", Qt::CaseInsensitive);
                formulaStr = formulaStr.replace("Avg(","Sum(1,", Qt::CaseInsensitive);
                formulaStr = formulaStr.replace("Count(","Sum(2,", Qt::CaseInsensitive);
                formulaStr = formulaStr.replace("[","'[");
                formulaStr = formulaStr.replace("]","]'");
                formulaStr = formulaStr.replace("<","");
                formulaStr = formulaStr.replace(">","");

                QScriptValue result  = myEngine.evaluate(formulaStr);
                res[i] = result.toString();
            }

            tmpStr += res.at(i);
        }
    }

    return tmpStr;
}
Exemple #3
0
int main() {
  int i; // List indices
  
  FILE *fin = fopen("beads.in", "r");
  
  int num;
  fscanf(fin, "%d\n", &num);
  
  NODE *curr_node = appendNewNode(NULL);
  
  for (i = 0; i < num; i++) {
    char new_char;
    fscanf(fin, "%c", &new_char);
    
    COLOR new_color = charToColor(new_char);
    if (new_color == INVALID_COLOR) {
      printf("Encountered unexpected char: %c\n", new_char);
      exit(EXIT_FAILURE);
    }
    
    // Check if this is the first bead in the current node
    if (curr_node->color == INVALID_COLOR) {
      curr_node->color = new_color;
      curr_node->count++;
      continue;
    }
    
    // Check if we can add this bead to the current node
    if (curr_node->color == new_color) {
      curr_node->count++;
      continue;
    }
    
    // We need to start a new node
    curr_node = appendNewNode(curr_node);
    curr_node->count++;
    curr_node->color = new_color;
  }
  fclose(fin);

  // Now that we have a circular linked list of nodes, we check each possible
  // split point and pick the max
  int curr_max = 0;
  NODE *start = curr_node;
  do {
    int split = splitValue(curr_node);
    if (split > curr_max) {
      curr_max = split;
    }
    curr_node = curr_node->next;
  } while (start != curr_node);

  FILE *fout = fopen("beads.out", "w");
  fprintf(fout, "%d\n", curr_max);
  fclose(fout);

  // Clean up allocated memory...
  while (curr_node != NULL) {
    curr_node = freeNode(curr_node);
  }
}
Exemple #4
0
bool mhwd::readConfigFile(mhwd::Config *config, std::string configPath) {
    std::ifstream file(configPath.c_str(), std::ios::in);

    if (!file.is_open())
        return false;

    Vita::string line, key, value;
    std::vector<Vita::string> parts;

    while (!file.eof()) {
        getline(file, line);

        size_t pos = line.find_first_of('#');
        if (pos != std::string::npos)
            line.erase(pos);

        if (line.trim().empty())
            continue;

        parts = line.explode("=");
        key = parts.front().trim().toLower();
        value = parts.back().trim("\"").trim();


        // Read in extern file
        if (value.size() > 1 && value.substr(0, 1) == ">") {
            std::ifstream file(getRightConfigPath(value.substr(1), config->basePath).c_str(), std::ios::in);
            if (!file.is_open())
                return false;

            Vita::string line;
            value.clear();

            while (!file.eof()) {
                getline(file, line);

                size_t pos = line.find_first_of('#');
                if (pos != std::string::npos)
                    line.erase(pos);

                if (line.trim().empty())
                    continue;

                value += " " + line.trim();
            }

            file.close();

            value = value.trim();

            // remove all multiple spaces
            while (value.find("  ") != std::string::npos) {
                value = value.replace("  ", " ");
            }
        }


        if (key == "include") {
            readConfigFile(config, getRightConfigPath(value, config->basePath));
        }
        else if (key == "name") {
            config->name = value.toLower();
        }
        else if (key == "version") {
            config->version = value;
        }
        else if (key == "info") {
            config->info = value;
        }
        else if (key == "priority") {
            config->priority = value.convert<int>();
        }
        else if (key == "freedriver") {
            value = value.toLower();

            if (value == "false")
                config->freedriver = false;
            else if (value == "true")
                config->freedriver = true;
        }
        else if (key == "classids") {
            // Add new HardwareIDs group to vector if vector is not empty
            if (!config->hwdIDs.back().classIDs.empty()) {
                mhwd::Config::HardwareIDs hwdID;
                config->hwdIDs.push_back(hwdID);
            }

            config->hwdIDs.back().classIDs = splitValue(value);
        }
        else if (key == "vendorids") {
            // Add new HardwareIDs group to vector if vector is not empty
            if (!config->hwdIDs.back().vendorIDs.empty()) {
                mhwd::Config::HardwareIDs hwdID;
                config->hwdIDs.push_back(hwdID);
            }

            config->hwdIDs.back().vendorIDs = splitValue(value);
        }
        else if (key == "deviceids") {
            // Add new HardwareIDs group to vector if vector is not empty
            if (!config->hwdIDs.back().deviceIDs.empty()) {
                mhwd::Config::HardwareIDs hwdID;
                config->hwdIDs.push_back(hwdID);
            }

            config->hwdIDs.back().deviceIDs = splitValue(value);
        }
        else if (key == "blacklistedclassids") {
            config->hwdIDs.back().blacklistedClassIDs = splitValue(value);
        }
        else if (key == "blacklistedvendorids") {
            config->hwdIDs.back().blacklistedVendorIDs = splitValue(value);
        }
        else if (key == "blacklisteddeviceids") {
            config->hwdIDs.back().blacklistedDeviceIDs = splitValue(value);
        }
        else if (key == "mhwddepends") {
            config->dependencies = splitValue(value);
        }
        else if (key == "mhwdconflicts") {
            config->conflicts = splitValue(value);
        }
    }

    file.close();

    // Append * to all empty vectors
    for (std::vector<mhwd::Config::HardwareIDs>::iterator iterator = config->hwdIDs.begin(); iterator != config->hwdIDs.end(); iterator++) {
        if ((*iterator).classIDs.empty())
            (*iterator).classIDs.push_back("*");

        if ((*iterator).vendorIDs.empty())
            (*iterator).vendorIDs.push_back("*");

        if ((*iterator).deviceIDs.empty())
            (*iterator).deviceIDs.push_back("*");
    }

    if (config->name.empty())
        return false;

    return true;
}