Panda3D

Loading...
Searching...
No Matches
pal_string_utils.cxx
Go to the documentation of this file.
1/**
2 * PANDA 3D SOFTWARE
3 * Copyright (c) Carnegie Mellon University. All rights reserved.
4 *
5 * All use of this software is subject to the terms of the revised BSD
6 * license. You should have received a copy of this license along
7 * with this source code in a file named "LICENSE."
8 *
9 * @file pal_string_utils.cxx
10 * @author drose
11 * @date 2000-11-30
12 */
13
14#include "pal_string_utils.h"
15
16#include "pnmFileType.h"
17#include "pnmFileTypeRegistry.h"
18
19 using std::string;
20
21
22// Extracts the first word of the string into param, and the remainder of the
23// line into value.
24 void
25extract_param_value(const string &str, string &param, string &value) {
26 size_t i = 0;
27
28 // First, skip all whitespace at the beginning.
29 while (i < str.length() && isspace(str[i])) {
30 i++;
31 }
32
33 size_t start = i;
34
35 // Now skip to the end of the whitespace.
36 while (i < str.length() && !isspace(str[i])) {
37 i++;
38 }
39
40 size_t end = i;
41
42 param = str.substr(start, end - start);
43
44 // Skip a little bit further to the start of the value.
45 while (i < str.length() && isspace(str[i])) {
46 i++;
47 }
48 value = trim_right(str.substr(i));
49}
50
51
52 bool
53parse_image_type_request(const string &word, PNMFileType *&color_type,
54 PNMFileType *&alpha_type) {
55 PNMFileTypeRegistry *registry = PNMFileTypeRegistry::get_global_ptr();
56 color_type = nullptr;
57 alpha_type = nullptr;
58
59 string color_name = word;
60 string alpha_name;
61 size_t comma = word.find(',');
62 if (comma != string::npos) {
63 // If we have a comma in the image_type, it's two types: a color type and
64 // an alpha type.
65 color_name = word.substr(0, comma);
66 alpha_name = word.substr(comma + 1);
67 }
68
69 if (!color_name.empty()) {
70 color_type = registry->get_type_from_extension(color_name);
71 if (color_type == nullptr) {
72 nout << "Image file type '" << color_name << "' is unknown.\n";
73 return false;
74 }
75 }
76
77 if (!alpha_name.empty()) {
78 alpha_type = registry->get_type_from_extension(alpha_name);
79 if (alpha_type == nullptr) {
80 nout << "Image file type '" << alpha_name << "' is unknown.\n";
81 return false;
82 }
83 }
84
85 return true;
86}
PNMFileTypeRegistry
This class maintains the set of all known PNMFileTypes in the universe.
Definition pnmFileTypeRegistry.h:28
PNMFileTypeRegistry::get_global_ptr
static PNMFileTypeRegistry * get_global_ptr()
Returns a pointer to the global PNMFileTypeRegistry object.
Definition pnmFileTypeRegistry.cxx:277
PNMFileTypeRegistry::get_type_from_extension
PNMFileType * get_type_from_extension(const std::string &filename) const
Tries to determine what the PNMFileType is likely to be for a particular image file based on its exte...
Definition pnmFileTypeRegistry.cxx:147
PNMFileType
This is the base class of a family of classes that represent particular image file types that PNMImag...
Definition pnmFileType.h:32
pal_string_utils.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pnmFileTypeRegistry.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pnmFileType.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
trim_right
string trim_right(const string &str)
Returns a new string representing the contents of the given string with the trailing whitespace remov...
Definition string_utils.cxx:251