Commit b11aa4eb authored by jcaceres's avatar jcaceres
Browse files

Folder rearangment

parents
/*
* Here is a sample of how to use AnyOption to
* parse comand line argumnets and an ptions file
*
* Create sample.txt as follows
*
* # sample options file
* # this is a comment
* zip
* size : 42
* title : This is a test title.
*
* Run the sample with any combination of the options
*
* a.out -c --zip -s 20 --name foo.jpg argtest1 argtest2
*/
#include "anyoption.h"
void example( int argc, char* argv[] );
int
main( int argc, char* argv[] )
{
example( argc, argv );
return 0 ;
}
void
example( int argc, char* argv[] )
{
/* 1. CREATE AN OBJECT */
AnyOption *opt = new AnyOption();
/* 2. SET PREFERENCES */
//opt->noPOSIX(); /* do not check for POSIX style character options */
//opt->setVerbose(); /* print warnings about unknown options */
//opt->autoUsagePrint(true); /* print usage for bad options */
/* 3. SET THE USAGE/HELP */
opt->addUsage( "" );
opt->addUsage( "Usage: " );
opt->addUsage( "" );
opt->addUsage( " -h --help Prints this help " );
opt->addUsage( " -s --size 42 Image Size " );
opt->addUsage( " -z --zip Compress Image " );
opt->addUsage( " -c convert Image " );
opt->addUsage( " --name image.jpg Image Name " );
opt->addUsage( "" );
/* 4. SET THE OPTION STRINGS/CHARACTERS */
/* by default all options will be checked on the command line and from option/resource file */
opt->setFlag( "help", 'h' ); /* a flag (takes no argument), supporting long and short form */
opt->setOption( "size", 's' ); /* an option (takes an argument), supporting long and short form */
opt->setOption( "name" ); /* an option (takes an argument), supporting only long form */
opt->setFlag( 'c' ); /* a flag (takes no argument), supporting only short form */
/* for options that will be checked only on the command and line not in option/resource file */
opt->setCommandFlag( "zip" , 'z'); /* a flag (takes no argument), supporting long and short form */
/* for options that will be checked only from the option/resource file */
opt->setFileOption( "title" ); /* an option (takes an argument), supporting only long form */
/* 5. PROCESS THE COMMANDLINE AND RESOURCE FILE */
/* read options from a option/resource file with ':' separated opttions or flags, one per line */
opt->processFile( "/home/user/.options" );
/* go through the command line and get the options */
opt->processCommandArgs( argc, argv );
if( ! opt->hasOptions()) { /* print usage if no options */
opt->printUsage();
delete opt;
return;
}
/* 6. GET THE VALUES */
if( opt->getFlag( "help" ) || opt->getFlag( 'h' ) )
opt->printUsage();
if( opt->getValue( 's' ) != NULL || opt->getValue( "size" ) != NULL )
cout << "size = " << opt->getValue( 's' ) << endl ;
if( opt->getValue( "name" ) != NULL )
cout << "name = " << opt->getValue( "name" ) << endl ;
if( opt->getValue( "title" ) != NULL )
cout << "title = " << opt->getValue( "title" ) << endl ;
if( opt->getFlag( 'c' ) )
cout << "c = flag set " << endl ;
if( opt->getFlag( 'z' ) || opt->getFlag( "zip" ) )
cout << "zip = flag set " << endl ;
cout << endl ;
/* 7. GET THE ACTUAL ARGUMENTS AFTER THE OPTIONS */
for( int i = 0 ; i < opt->getArgc() ; i++ ){
cout << "arg = " << opt->getArgv( i ) << endl ;
}
/* 8. DONE */
delete opt;
}
This diff is collapsed.
#!/bin/bash
#####################################
APP_NAME=jmess
######################################
## Created by Juan-Pablo Caceres
#Cleanup
rm -r obj
rm -r moc
rm $APP_NAME.pro
rm Makefile
rm $APP_NAME
echo "Makefile deleted"
#Create project (.pro) file
qmake-qt4 -project -o $APP_NAME.pro
echo "qmake-qt4 .pro built."
#Create Makefile
qmake-qt4 -makefile -unix -o Makefile \
"QMAKE_CXXFLAGS+=-Wno-deprecated -pg -g" \
"OBJECTS_DIR=obj" "MOC_DIR=moc" \
"QT+=xml" \
"unix:LIBS+=-Xlinker -ljack -lpthread -lrt" \
"unix:TARGET=$APP_NAME" \
"unix:target.path=/usr/bin" \
"unix:INSTALLS+=target" $APP_NAME.pro
echo "done"
#!/bin/bash
rm -r /user/j/jcaceres/Library/Web/jmess/*
doxygen jmess_doxygen
chmod -R o+rx /user/j/jcaceres/Library/Web/jmess
/*
JMess: A simple utility so save your jack-audio mess.
Copyright (C) 2007 Juan-Pablo Caceres.
This program 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 2 of the License, or
(at your option) any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* JMess.cpp
*/
#include "JMess.h"
//-------------------------------------------------------------------------------
/*! \brief Constructs a JMess object that has a jack client.
*
*/
//-------------------------------------------------------------------------------
JMess::JMess()
{
//Open a client connection to the JACK server. Starting a
//new server only to list its ports seems pointless, so we
//specify JackNoStartServer.
mClient = jack_client_open ("lsp", JackNoStartServer, &mStatus);
if (mClient == NULL) {
if (mStatus & JackServerFailed) {
cerr << "JACK server not running" << endl;
} else {
cerr << "jack_client_open() failed, "
<< "status = 0x%2.0x\n" << mStatus << endl;
}
exit(1);
}
}
//-------------------------------------------------------------------------------
/*! \brief Distructor closes the jmess jack audio client.
*
*/
//-------------------------------------------------------------------------------
JMess::~JMess()
{
if (jack_client_close(mClient))
cerr << "ERROR: Could not close the hidden jmess jack client." << endl;
}
//-------------------------------------------------------------------------------
/*! \brief Write an XML file with the name specified at xmlOutFile.
*
*/
//-------------------------------------------------------------------------------
void JMess::writeOutput(QString xmlOutFile)
{
QDomDocument jmess_xml; QDomElement root;
QDomElement connection; QDomElement output;
QDomElement input; QDomText output_name;
QDomText input_name;
QVector<QString> OutputInput(2);
this->setConnectedPorts();
root = jmess_xml.createElement("jmess");
for (QVector<QVector<QString> >::iterator it = mConnectedPorts.begin();
it != mConnectedPorts.end(); ++it) {
OutputInput = *it;
//cout << "Output ===> " <<qPrintable(OutputInput[0]) << endl;
//cout << "Input ===> " <<qPrintable(OutputInput[1]) << endl;
//Initialize XML elements
connection = jmess_xml.createElement("connection");
output = jmess_xml.createElement("output");
input = jmess_xml.createElement("input");
output_name = jmess_xml.createTextNode(OutputInput[0]);
input_name = jmess_xml.createTextNode(OutputInput[1]);
jmess_xml.appendChild(root); root.appendChild(connection);
connection.appendChild(output); connection.appendChild(input);
output.appendChild(output_name); input.appendChild(input_name);
}
//Write output file
QFile file(xmlOutFile);
string answer = "";
//Check for existing file first, and confirm before overwriting
if (file.exists()) {
while ((answer != "yes") && (answer != "no")) {
cout << "WARNING: The File " <<qPrintable(xmlOutFile)
<< " exists. Do you want to overwrite it? (yes/no): ";
cin >> answer;
}
}
else {
answer = "yes";
}
if (answer == "yes") {
if (!file.open(QIODevice::WriteOnly)) {
cerr << "Cannot open file for writing: "
<< qPrintable(file.errorString()) << endl;
exit(1);
}
QTextStream out(&file);
jmess_xml.save(out, Indent);
cout << qPrintable(xmlOutFile) << " written." << endl;
}
}
//-------------------------------------------------------------------------------
/*! \brief Set list of ouput ports that have connections.
*
*/
//-------------------------------------------------------------------------------
void JMess::setConnectedPorts()
{
mConnectedPorts.clear();
const char **ports, **connections; //vector of ports and connections
QVector<QString> OutputInput(2); //helper variable
//Get active output ports.
ports = jack_get_ports (mClient, NULL, NULL, JackPortIsOutput);
for (unsigned int out_i = 0; ports[out_i]; ++out_i) {
if ((connections = jack_port_get_all_connections
(mClient, jack_port_by_name(mClient, ports[out_i]))) != 0) {
for (unsigned int in_i = 0; connections[in_i]; ++in_i) {
OutputInput[0] = ports[out_i];
//cout << "Output ===> " <<qPrintable(OutputInput[0]) << endl;
OutputInput[1] = connections[in_i];
//cout << "Input ===> " << qPrintable(OutputInput[1]) << endl;
mConnectedPorts.append(OutputInput);
}
}
}
free(ports);
}
//-------------------------------------------------------------------------------
/*! \brief Disconnect all the clients.
*
*/
//-------------------------------------------------------------------------------
void JMess::disconnectAll()
{
QVector<QString> OutputInput(2);
this->setConnectedPorts();
for (QVector<QVector<QString> >::iterator it = mConnectedPorts.begin();
it != mConnectedPorts.end(); ++it) {
OutputInput = *it;
if (jack_disconnect(mClient, OutputInput[0].toAscii(), OutputInput[1].toAscii())) {
cerr << "WARNING: port: " << qPrintable(OutputInput[0])
<< "and port: " << qPrintable(OutputInput[1])
<< " could not be disconnected.\n";
}
}
}
//-------------------------------------------------------------------------------
/*! \brief Parse the XML input file.
*
* Returns 0 on success, or 1 if the file has an incorrect format or cannot
* read the file.
*/
//-------------------------------------------------------------------------------
int JMess::parseXML(QString xmlInFile)
{
mPortsToConnect.clear();
QString errorStr;
int errorLine;
int errorColumn;
QFile file(xmlInFile);
if (!file.open(QIODevice::ReadOnly)) {
cerr << "Cannot open file for reading: "
<< qPrintable(file.errorString()) << endl;
return 1;
}
QDomDocument doc;
if (!doc.setContent(&file, true, &errorStr, &errorLine,
&errorColumn)) {
cerr << "===================================================\n"
<< "Error parsing XML input file:\n"
<< "Parse error at line " << errorLine
<< ", column " << errorColumn << "\n"
<< qPrintable(errorStr) << "\n"
<< "===================================================\n";
return 1;
}
QDomElement jmess = doc.documentElement();
if (jmess.tagName() != "jmess") {
cerr << "Error: Root tag should be <jmess>: "
<< qPrintable(jmess.tagName()) << endl;
return 1;
}
QVector<QString> OutputInput(2);
//First check for <connection> tag
for(QDomNode n_cntn = jmess.firstChild();
!n_cntn.isNull(); n_cntn = n_cntn.nextSibling()) {
QDomElement cntn = n_cntn.toElement();
if (cntn.tagName() == "connection") {
//Now check for ouput & input tag
for(QDomNode n_sck = cntn.firstChild();
!n_sck.isNull(); n_sck = n_sck.nextSibling()) {
QDomElement sck = n_sck.toElement();
//cout << qPrintable(sck.tagName()) << endl;
//cout << qPrintable(sck.text()) << endl;
if (sck.tagName() == "output") {
OutputInput[0] = sck.text();
}
else if (sck.tagName() == "input") {
OutputInput[1] = sck.text();
}
}
mPortsToConnect.append(OutputInput);
}
}
return 0;
}
//-------------------------------------------------------------------------------
/*! \brief Connect ports specified in input XML file xmlInFile
*
*/
//-------------------------------------------------------------------------------
void JMess::connectPorts(QString xmlInFile)
{
QVector<QString> OutputInput(2);
if ( !(this->parseXML(xmlInFile)) ) {
for (QVector<QVector<QString> >::iterator it = mPortsToConnect.begin();
it != mPortsToConnect.end(); ++it) {
OutputInput = *it;
if (jack_connect(mClient, OutputInput[0].toAscii(), OutputInput[1].toAscii())) {
//Display a warining only if the error is not because the ports are already
//connected, in case the program doesn't display anyting.
if (EEXIST !=
jack_connect(mClient, OutputInput[0].toAscii(), OutputInput[1].toAscii())) {
cerr << "WARNING: port: " << qPrintable(OutputInput[0])
<< "and port: " << qPrintable(OutputInput[1])
<< " could not be connected.\n";
}
}
}
}
}
/*
JMess: A simple utility so save your jack-audio mess.
Copyright (C) 2007 Juan-Pablo Caceres.
This program 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 2 of the License, or
(at your option) any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* JMess.h
*/
#ifndef __JMESS_H
#define __JMESS_H
#include <iostream>
#include <string>
#include <errno.h>
#include <QIODevice>
#include <QString>
#include <QVector>
#include <QtXml>
#include <QXmlSimpleReader>
#include <QXmlInputSource>
#include <QXmlContentHandler>
#include <jack/jack.h>
using namespace std;
const int Indent = 2;
//-------------------------------------------------------------------------------
/*! \brief Class to save and load all jack client connections.
*
* Saves an XML file with all the current jack connections. This same file can
* be loaded to connect evrything again. The XML file can also be edited.
*
* Has also an option to disconnect all the clients.
*/
//-------------------------------------------------------------------------------
class JMess {
public:
JMess();
virtual ~JMess();
void disconnectAll();
void writeOutput(QString xmlOutFile);
void connectPorts(QString xmlInFile);
private:
void setConnectedPorts();
int parseXML(QString xmlInFile);
jack_client_t *mClient; //Class client
jack_status_t mStatus; //Class client status
//Vectors of Connected Ports and Ports to connects
//This are a matrix (Nx2) of string like this:
//OuputPort1 InputPort1
// ...
//OuputPortN InputPortN
QVector<QVector<QString> > mConnectedPorts;
QVector<QVector<QString> > mPortsToConnect;
};
#endif
This diff is collapsed.
#ifndef _ANYOPTION_H
#define _ANYOPTION_H
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>
#define COMMON_OPT 1
#define COMMAND_OPT 2
#define FILE_OPT 3
#define COMMON_FLAG 4
#define COMMAND_FLAG 5
#define FILE_FLAG 6
#define COMMAND_OPTION_TYPE 1
#define COMMAND_FLAG_TYPE 2
#define FILE_OPTION_TYPE 3
#define FILE_FLAG_TYPE 4
#define UNKNOWN_TYPE 5
#define DEFAULT_MAXOPTS 10
#define MAX_LONG_PREFIX_LENGTH 2
#define DEFAULT_MAXUSAGE 3
#define DEFAULT_MAXHELP 10
#define TRUE_FLAG "true"
using namespace std;
class AnyOption
{
public: /* the public interface */
AnyOption();
AnyOption(int maxoptions );
AnyOption(int maxoptions , int maxcharoptions);
~AnyOption();
/*
* following set methods specifies the
* special characters and delimiters
* if not set traditional defaults will be used
*/
void setCommandPrefixChar( char _prefix ); /* '-' in "-w" */
void setCommandLongPrefix( char *_prefix ); /* '--' in "--width" */
void setFileCommentChar( char _comment ); /* '#' in shellscripts */
void setFileDelimiterChar( char _delimiter );/* ':' in "width : 100" */
/*
* provide the input for the options
* like argv[] for commndline and the
* option file name to use;
*/
void useCommandArgs( int _argc, char **_argv );
void useFiileName( const char *_filename );
/*
* turn off the POSIX style options
* this means anything starting with a '-' or "--"
* will be considered a valid option
* which alo means you cannot add a bunch of
* POIX options chars together like "-lr" for "-l -r"
*
*/
void noPOSIX();
/*
* prints warning verbose if you set anything wrong
*/
void setVerbose();
/*
* there are two types of options
*
* Option - has an associated value ( -w 100 )
* Flag - no value, just a boolean flag ( -nogui )
*
* the options can be either a string ( GNU style )
* or a character ( traditional POSIX style )
* or both ( --width, -w )
*
* the options can be common to the commandline and
* the optionfile, or can belong only to either of
* commandline and optionfile
*
* following set methods, handle all the aboove
* cases of options.
*/
/* options comman to command line and option file */
void setOption( const char *opt_string );
void setOption( char opt_char );
void setOption( const char *opt_string , char opt_char );
void setFlag( const char *opt_string );
void setFlag( char opt_char );
void setFlag( const char *opt_string , char opt_char );
/* options read from commandline only */
void setCommandOption( const char *opt_string );
void setCommandOption( char opt_char );
void setCommandOption( const char *opt_string , char opt_char );
void setCommandFlag( const char *opt_string );
void setCommandFlag( char opt_char );
void setCommandFlag( const char *opt_string , char opt_char );
/* options read from an option file only */
void setFileOption( const char *opt_string );
void setFileOption( char opt_char );
void setFileOption( const char *opt_string , char opt_char );
void setFileFlag( const char *opt_string );
void setFileFlag( char opt_char );
void setFileFlag( const char *opt_string , char opt_char );
/*
* process the options, registerd using
* useCommandArgs() and useFileName();
*/
void processOptions();
void processCommandArgs();
void processCommandArgs( int max_args );