Ejemplo n.º 1
0
string Logger::GetConfig(const string &conf_path, const string &conf_name)
{
    std::ifstream config_file(conf_path);
    std::string buf_line("");
    string conf_value("");
    int split_pos = std::string::npos;

    if (!config_file.is_open()) {
        return "";
    }

    while (config_file.good()) {
        std::getline(config_file, buf_line);
        TrimComments(buf_line);
        TrimSpaces(buf_line);

        if ((split_pos = buf_line.find_first_of('=')) != std::string::npos) {
            if (buf_line.substr(0, split_pos) == conf_name) {
                conf_value =  buf_line.substr(split_pos + 1, buf_line.length() - 1);
                break;
            }
        }
    }

    config_file.close();
    return conf_value;
}
Ejemplo n.º 2
0
/*
 * Connection handling. Implements 'get' and 'set'.
 */
void *handle_conn(void *void_param) {//int conn, struct hsearch_data *htab) {

    thdata *param = (thdata *)void_param;
    int conn = param->conn;
    struct hsearch_data *htab = param->htab;

    char *cmd, *key, *val, *msg, *line;
    int length;
    ENTRY entry, *result;
    struct Buf *buf;

    buf = new_buffer(conn);

    while (1) {
        line = buf_line(buf);

        if (strlen(line) == 0) {
            // Client has closed connection
            break;
        }

        cmd = strtok(line, " ");

        if (strcmp(cmd, "get") == 0) {
            key = strtok(NULL, " ");

            entry.key = key;
            hsearch_r(entry, FIND, &result, htab);
            if (result != NULL) {

                val = (char *) result->data;

                msg = malloc(strlen(key) + 13);
                sprintf(msg, "VALUE %s 0 %d\r\n", key, strlen(val));
                send(conn, msg, strlen(msg), 0);

                send(conn, val, strlen(val), 0);
                send(conn, "\r\n", 2, 0);
            }
            send(conn, "END\r\n", 5, 0);

        } else if (strcmp(cmd, "set") == 0) {
            key = strtok(NULL, " ");

            strtok(NULL, " ");
            strtok(NULL, " ");
            //exp = strtok(NULL, " ");
            //flags = strtok(NULL, " ");

            length = atoi(strtok(NULL, " "));
            val = buf_bytes(buf, length);

            entry.key = strdup(key);
            entry.data = strdup(val);
            hsearch_r(entry, ENTER, &result, htab);

            send(conn, "STORED\r\n", 8, 0);
        }
    }

    return NULL;
}