~glcompbench-dev/glcompbench/blur-update

« back to all changes in this revision

Viewing changes to src/libmatrix/util.cc

  • Committer: Jesse Barker
  • Date: 2012-01-27 22:26:02 UTC
  • mfrom: (74.1.5 libmatrix-util)
  • Revision ID: jesse.barker@linaro.org-20120127222602-osainr4na7opoiij
Merge of lp:~glcompbench-dev/glcompbench/libmatrix-util.

Updates glcompbench to reflect the latest lp:libmatrix, which, in and of itself,
conslidates code previously duplicated between glcompbench and glmark2 into
libmatrix.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// Copyright (c) 2010-2011 Linaro Limited
 
3
//
 
4
// All rights reserved. This program and the accompanying materials
 
5
// are made available under the terms of the MIT License which accompanies
 
6
// this distribution, and is available at
 
7
// http://www.opensource.org/licenses/mit-license.php
 
8
//
 
9
// Contributors:
 
10
//     Alexandros Frantzis <alexandros.frantzis@linaro.org>
 
11
//     Jesse Barker <jesse.barker@linaro.org>
 
12
//
 
13
#include <sstream>
 
14
#include <fstream>
 
15
#include <sys/time.h>
 
16
#ifdef ANDROID
 
17
#include <android/asset_manager.h>
 
18
#else
 
19
#include <dirent.h>
 
20
#endif
 
21
 
 
22
#include "log.h"
 
23
#include "util.h"
 
24
 
 
25
/**
 
26
 * Splits a string using a delimiter
 
27
 *
 
28
 * @param s the string to split
 
29
 * @param delim the delimitir to use
 
30
 * @param elems the string vector to populate
 
31
 */
 
32
void
 
33
Util::split(const std::string &s, char delim, std::vector<std::string> &elems)
 
34
{
 
35
    std::stringstream ss(s);
 
36
 
 
37
    std::string item;
 
38
    while(std::getline(ss, item, delim))
 
39
        elems.push_back(item);
 
40
}
 
41
 
 
42
uint64_t
 
43
Util::get_timestamp_us()
 
44
{
 
45
    struct timeval tv;
 
46
    gettimeofday(&tv, NULL);
 
47
    uint64_t now = static_cast<uint64_t>(tv.tv_sec) * 1000000 +
 
48
                   static_cast<double>(tv.tv_usec);
 
49
    return now;
 
50
}
 
51
 
 
52
std::string
 
53
Util::appname_from_path(const std::string& path)
 
54
{
 
55
    std::string::size_type slashPos = path.rfind("/");
 
56
    std::string::size_type startPos(0);
 
57
    if (slashPos != std::string::npos)
 
58
    {
 
59
        startPos = slashPos + 1;
 
60
    }
 
61
    return std::string(path, startPos, std::string::npos);
 
62
}
 
63
 
 
64
#ifndef ANDROID
 
65
 
 
66
std::istream *
 
67
Util::get_resource(const std::string &path)
 
68
{
 
69
    std::ifstream *ifs = new std::ifstream(path.c_str());
 
70
 
 
71
    return static_cast<std::istream *>(ifs);
 
72
}
 
73
 
 
74
void
 
75
Util::list_files(const std::string& dirName, std::vector<std::string>& fileVec)
 
76
{
 
77
    DIR* dir = opendir(dirName.c_str());
 
78
    if (!dir)
 
79
    {
 
80
        Log::error("Failed to open models directory '%s'\n", dirName.c_str());
 
81
        return;
 
82
    }
 
83
 
 
84
    struct dirent* entry = readdir(dir);
 
85
    while (entry)
 
86
    {
 
87
        std::string pathname(dirName + "/");
 
88
        pathname += std::string(entry->d_name);
 
89
        // Skip '.' and '..'
 
90
        if (entry->d_name[0] != '.')
 
91
        {
 
92
            fileVec.push_back(pathname);
 
93
        }
 
94
        entry = readdir(dir);
 
95
    }
 
96
    closedir(dir);
 
97
}
 
98
 
 
99
#else
 
100
 
 
101
AAssetManager *Util::android_asset_manager = 0;
 
102
 
 
103
void
 
104
Util::android_set_asset_manager(AAssetManager *asset_manager)
 
105
{
 
106
    Util::android_asset_manager = asset_manager;
 
107
}
 
108
 
 
109
AAssetManager *
 
110
Util::android_get_asset_manager()
 
111
{
 
112
    return Util::android_asset_manager;
 
113
}
 
114
 
 
115
std::istream *
 
116
Util::get_resource(const std::string &path)
 
117
{
 
118
    std::string path2(path);
 
119
    /* Remove leading '/' from path name, it confuses the AssetManager */
 
120
    if (path2.size() > 0 && path2[0] == '/')
 
121
        path2.erase(0, 1);
 
122
 
 
123
    std::stringstream *ss = new std::stringstream;
 
124
    AAsset *asset = AAssetManager_open(Util::android_asset_manager,
 
125
                                       path2.c_str(), AASSET_MODE_RANDOM);
 
126
    if (asset) {
 
127
        ss->write(reinterpret_cast<const char *>(AAsset_getBuffer(asset)),
 
128
                  AAsset_getLength(asset));
 
129
        Log::debug("Load asset %s\n", path2.c_str());
 
130
        AAsset_close(asset);
 
131
    }
 
132
    else {
 
133
        Log::error("Couldn't load asset %s\n", path2.c_str());
 
134
    }
 
135
 
 
136
    return static_cast<std::istream *>(ss);
 
137
}
 
138
 
 
139
void
 
140
Util::list_files(const std::string& dirName, std::vector<std::string>& fileVec)
 
141
{
 
142
    AAssetManager *mgr(Util::android_get_asset_manager());
 
143
    std::string dir_name(dirName);
 
144
 
 
145
    /* Remove leading '/' from path, it confuses the AssetManager */
 
146
    if (dir_name.size() > 0 && dir_name[0] == '/')
 
147
        dir_name.erase(0, 1);
 
148
 
 
149
    AAssetDir* dir = AAssetManager_openDir(mgr, dir_name.c_str());
 
150
    if (!dir)
 
151
    {
 
152
        Log::error("Failed to open models directory '%s'\n", dir_name.c_str());
 
153
        return;
 
154
    }
 
155
 
 
156
    const char *filename(0);
 
157
    while ((filename = AAssetDir_getNextFileName(dir)) != 0)
 
158
    {
 
159
        std::string pathname(dir_name + "/");
 
160
        pathname += std::string(filename);
 
161
        fileVec.push_back(pathname);
 
162
    }
 
163
    AAssetDir_close(dir);
 
164
}
 
165
#endif