LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-18-2012, 12:51 PM   #1
mreff555
Member
 
Registered: Sep 2011
Location: Philly
Distribution: Gentoo
Posts: 450

Rep: Reputation: Disabled
Whats the most elegant way of comparing different data types


I'm fairly new to c++ or programming in general.
I am trying to avoid a bunch of conditional statements and use the most elegant method to solve my problem

I have a function that will currently determines a file extension and stores it in a character array called ext.

What I would like it to do, is output a number symbolic of a list of accepted extensions eg:
0= if null or not supported
1=tif
2=jpg

Is there a way to do this with enum or typedef, or some other good way of doing this.
Also, what is the best way to make it ignore case.

Thanks.

Code:
int validExt(){
  int len= strlen(chararg);
  int dot=len-1;
  for(int i=0;i <=( len-1); i++){
    if(chararg[i]=='.') dot=i;
    }
  int extlen= (len-1) - dot;
  char ext[extlen+1];
  strcpy (ext,&chararg[dot+1]);
  cout<<ext<<endl;

  ///extnum????

return extnum;
}
 
Old 08-18-2012, 01:04 PM   #2
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,141

Rep: Reputation: 638Reputation: 638Reputation: 638Reputation: 638Reputation: 638Reputation: 638
i think the most elegent way would be for your program to read/parse the /usr/share/misc/magic file (you would essentially be recreating the file command).

that way if someone mistakenly names something like watch-this-video.mkv.exe.jpg you will be able to determine the real file type by examining the header of the actual file (not just the file name suffix).
 
Old 08-18-2012, 02:05 PM   #3
mreff555
Member
 
Registered: Sep 2011
Location: Philly
Distribution: Gentoo
Posts: 450

Original Poster
Rep: Reputation: Disabled
Excellent idea, however I need this to work across multiple platforms, including windows 7
In the future I might pursue some method of reading the header but for now I'll settle for something
that is 90% robust.
So is there a way I can evaluate a character array and set up some kind of enumeration to turn char* values into numbers?
If not I can just use conditionals.
 
Old 08-18-2012, 02:26 PM   #4
mreff555
Member
 
Registered: Sep 2011
Location: Philly
Distribution: Gentoo
Posts: 450

Original Poster
Rep: Reputation: Disabled
Excellent idea, however I need this to work across multiple platforms, including windows 7
In the future I might pursue some method of reading the header but for now I'll settle for something
that is 90% robust.
So is there a way I can evaluate a character array and set up some kind of enumeration to turn char* values into numbers?
If not I can just use conditionals.
 
Old 08-18-2012, 02:56 PM   #5
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
I'd say enum is what you want:
http://en.cppreference.com/w/cpp/language/enum
 
Old 08-18-2012, 03:17 PM   #6
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,141

Rep: Reputation: 638Reputation: 638Reputation: 638Reputation: 638Reputation: 638Reputation: 638
in c you can reverse the array and use strncmp to see if the first 3 chars equal "gpj" or "fit" and return the value that corresponds to the non/match.
 
Old 08-18-2012, 03:29 PM   #7
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,523

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
Quote:
Originally Posted by mreff555 View Post
I'm fairly new to c++ or programming in general.
It would appear that you have previous experience writing code in C. Consider using C++ strings, not char arrays for strings. Also, avoid global variables.

Quote:
Originally Posted by mreff555 View Post
I am trying to avoid a bunch of conditional statements and use the most elegant method to solve my problem

I have a function that will currently determines a file extension and stores it in a character array called ext.

What I would like it to do, is output a number symbolic of a list of accepted extensions eg:
0= if null or not supported
1=tif
2=jpg

Is there a way to do this with enum or typedef, or some other good way of doing this.
Also, what is the best way to make it ignore case.

Thanks.
You could consider storing various file types, and an identifying number, within a STL vector.

Here's an example program:
Code:
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
#include <ctype.h>

class FileUtil
{
public:
    FileUtil(const std::string& extDataFile)
    {
        // These extensions should come from a flat-file; not be hard-coded as shown below.
        m_knownExtensions.push_back("jpg");
        m_knownExtensions.push_back("cpp");
        m_knownExtensions.push_back("c");
        m_knownExtensions.push_back("mov");
    }

    bool knownExt(const std::string& filename)
    {
        // search for the dot
        const size_t dot = filename.rfind('.');

        if (dot == std::string::npos)
        {
            return false;
        }

        // extract the filename extension
        std::string ext = filename.substr(dot + 1);

        // convert extension to all lowercase letters
        std::transform(ext.begin(), ext.end(), ext.begin(), tolower);

        // search for extension within our list of known extensions; return true if found.
        for (std::vector<std::string>::const_iterator it = m_knownExtensions.begin();
             it != m_knownExtensions.end(); ++it)
        {
            if (ext == *it)
                return true;
        }

        // if here, we could not locate the filename's extension.
        return false;
    }

    void displayKnownExtensions() const
    {
        for (size_t i = 0; i < m_knownExtensions.size(); ++i)
        {
            std::cout << i << " -- " << m_knownExtensions[i] << std::endl;
        }
    }

private:
    std::vector<std::string> m_knownExtensions;
};

int main()
{
    FileUtil fn("MyExtDataFile.dat");    // see note in constructor

    fn.displayKnownExtensions();

    std::cout << "JPEG is" << (fn.knownExt("File.jpeg") ? "" : " NOT") << " a known extension."
              << std::endl
              << "JPG  is" << (fn.knownExt("File.jpg" ) ? "" : " NOT") << " a known extension."
              << std::endl;
}

Last edited by dwhitney67; 08-18-2012 at 10:45 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
comparing data in array in FORTRAN vjramana Programming 4 10-14-2011 05:29 PM
adding different data types khodeir Programming 6 06-10-2011 06:29 PM
PHP: Problems comparing data from a DB with data from http request eantoranz Programming 3 08-14-2008 08:55 PM
LXer: On data models, data types and dangerous liaisons LXer Syndicated Linux News 0 07-22-2006 11:33 PM
Prolog data types..(?) sachitha Programming 2 04-08-2006 01:30 PM


All times are GMT -5. The time now is 09:04 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration