about summary refs log blame commit diff stats
path: root/src/resource.c
blob: 1598c7b280be58debb51ae8d72d18161f926b882 (plain) (tree)
1
2
3
4


             
                                                            















                                                                       











                                                                                








                     
                                                                              
                                                  

                         

                                                               
                                      
                 




                                              



                        





























                                                                

                                         
                   

                               

                       
 
/*
 * resource.c
 *
 * Copyright (C) 2012 - 2015 James Booth <boothj5@gmail.com>
 *
 * This file is part of Profanity.
 *
 * Profanity is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Profanity is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Profanity.  If not, see <http://www.gnu.org/licenses/>.
 *
 * In addition, as a special exception, the copyright holders give permission to
 * link the code of portions of this program with the OpenSSL library under
 * certain conditions as described in each individual source file, and
 * distribute linked combinations including the two.
 *
 * You must obey the GNU General Public License in all respects for all of the
 * code used other than OpenSSL. If you modify file(s) with this exception, you
 * may extend this exception to your version of the file(s), but you are not
 * obligated to do so. If you do not wish to do so, delete this exception
 * statement from your version. If you delete this exception statement from all
 * source files in the program, then also delete it here.
 *
 */

#include <assert.h>
#include <stdlib.h>
#include <string.h>

#include <common.h>
#include <resource.h>

Resource * resource_new(const char * const name, resource_presence_t presence,
    const char * const status, const int priority)
{
    assert(name != NULL);
    Resource *new_resource = malloc(sizeof(struct resource_t));
    new_resource->name = strdup(name);
    new_resource->presence = presence;
    if (status) {
        new_resource->status = strdup(status);
    } else {
        new_resource->status = NULL;
    }
    new_resource->priority = priority;

    return new_resource;
}

int
resource_compare_availability(Resource *first, Resource *second)
{
    if (first->priority > second->priority) {
        return -1;
    } else if (first->priority < second->priority) {
        return 1;
    } else { // priorities equal
        if (first->presence == RESOURCE_CHAT) {
            return -1;
        } else if (second->presence == RESOURCE_CHAT) {
            return 1;
        } else if (first->presence == RESOURCE_ONLINE) {
            return -1;
        } else if (second->presence == RESOURCE_ONLINE) {
            return 1;
        } else if (first->presence == RESOURCE_AWAY) {
            return -1;
        } else if (second->presence == RESOURCE_AWAY) {
            return 1;
        } else if (first->presence == RESOURCE_XA) {
            return -1;
        } else if (second->presence == RESOURCE_XA) {
            return 1;
        } else {
            return -1;
        }
    }
}

void resource_destroy(Resource *resource)
{
    if (resource) {
        free(resource->name);
        free(resource->status);
        free(resource);
    }
}
span> wd 1)) (kv-value kv-pair))) files)))) ; >> (wordcounts (append (file->linelist file1) ; (file->linelist file2) ; (file->linelist file3))) (define (mostfreq files) (accumulate (lambda (new old) (cond ((> (kv-value new) (kv-value (car old))) (list new)) ((= (kv-value new) (kv-value (car old))) (cons new old)) ; In case of tie, remember both. (else old))) (list (make-kv-pair 'foo 0)) ; Starting value. (groupreduce + 0 (sort-into-buckets (flatmap (lambda (kv-pair) (map (lambda (wd) (make-kv-pair wd 1)) (kv-value kv-pair))) files))))) ; >> (mostfreq (append (file->linelist file1) ; (file->linelist file2) ; (file->linelist file3))) ;; Example 3: Searching for a pattern. ; >> (match? '(* i * her *) '(i saw her standing there)) ; >> (match? '(* i * her *) '(and i love her)) ; >> (match? '(* i * her *) '(ps i love you)) (define (grep pattern files) (groupreduce cons '() (sort-into-buckets (flatmap (lambda (kv-pair) (if (match? pattern (kv-value kv-pair)) (list kv-pair) '())) files)))) ; >> (grep '(* i * her *) (append (file->linelist file1) ; (file->linelist file2) ; (file->linelist file3))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Don't read this part, just take it on faith. (define (sort-into-buckets alist) (group (sort-by-key alist))) (define (group alist) (define (help in complete current) (cond ((null? in) (reverse (cons (reverse current) complete))) ((null? current) (help (cdr in) complete (cons (car in) current))) ((equal? (kv-key (car in)) (kv-key (car current))) (help (cdr in) complete (cons (car in) current))) (else (help in (cons current complete) '())))) (if (null? alist) '() (help alist '() '()))) (define (sort-by-key alist) (define (keyinsert pair alist) (cond ((null? alist) (list pair)) ((bef? (kv-key pair) (kv-key (car alist))) (cons pair alist)) ((equal? (kv-key pair) (kv-key (car alist))) (cons pair alist)) (else (cons (car alist) (keyinsert pair (cdr alist)))))) (if (null? alist) '() (keyinsert (car alist) (sort-by-key (cdr alist))))) (define (bef? a b) ; word or sentence version of BEFORE? (cond ((word? a) (bef? (se a) b)) ((word? b) (bef? a (se b))) ((empty? a) (not (empty? b))) ((empty? b) #f) ((before? (first a) (first b)) #t) ((equal? (first a) (first b)) (bef? (bf a) (bf b))) (else #f))) (define (match? pattern text) (cond ((null? pattern) (null? text)) ((null? text) (and (equal? (car pattern) '*) (match? (cdr pattern) text))) ((equal? (car pattern) '*) (or (match? (cdr pattern) text) (match? pattern (cdr text)))) ((equal? (car pattern) (car text)) (match? (cdr pattern) (cdr text))) (else #f)))