Panda3D

pal_string_utils.cxx

00001 // Filename: pal_string_utils.cxx
00002 // Created by:  drose (30Nov00)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "pal_string_utils.h"
00016 
00017 #include "pnmFileType.h"
00018 #include "pnmFileTypeRegistry.h"
00019 
00020 
00021 // Extracts the first word of the string into param, and the remainder
00022 // of the line into value.
00023 void
00024 extract_param_value(const string &str, string &param, string &value) {
00025   size_t i = 0;
00026 
00027   // First, skip all whitespace at the beginning.
00028   while (i < str.length() && isspace(str[i])) {
00029     i++;
00030   }
00031 
00032   size_t start = i;
00033 
00034   // Now skip to the end of the whitespace.
00035   while (i < str.length() && !isspace(str[i])) {
00036     i++;
00037   }
00038 
00039   size_t end = i;
00040 
00041   param = str.substr(start, end - start);
00042 
00043   // Skip a little bit further to the start of the value.
00044   while (i < str.length() && isspace(str[i])) {
00045     i++;
00046   }
00047   value = trim_right(str.substr(i));
00048 }
00049 
00050 
00051 bool
00052 parse_image_type_request(const string &word, PNMFileType *&color_type,
00053                          PNMFileType *&alpha_type) {
00054   PNMFileTypeRegistry *registry = PNMFileTypeRegistry::get_global_ptr();
00055   color_type = (PNMFileType *)NULL;
00056   alpha_type = (PNMFileType *)NULL;
00057 
00058   string color_name = word;
00059   string alpha_name;
00060   size_t comma = word.find(',');
00061   if (comma != string::npos) {
00062     // If we have a comma in the image_type, it's two types: a color
00063     // type and an alpha type.
00064     color_name = word.substr(0, comma);
00065     alpha_name = word.substr(comma + 1);
00066   }
00067 
00068   if (!color_name.empty()) {
00069     color_type = registry->get_type_from_extension(color_name);
00070     if (color_type == (PNMFileType *)NULL) {
00071       nout << "Image file type '" << color_name << "' is unknown.\n";
00072       return false;
00073     }
00074   }
00075 
00076   if (!alpha_name.empty()) {
00077     alpha_type = registry->get_type_from_extension(alpha_name);
00078     if (alpha_type == (PNMFileType *)NULL) {
00079       nout << "Image file type '" << alpha_name << "' is unknown.\n";
00080       return false;
00081     }
00082   }
00083 
00084   return true;
00085 }
00086 
00087 
 All Classes Functions Variables Enumerations