Doxygen Source Code Documentation
Main Page Alphabetical List Data Structures File List Data Fields Globals Search
3dNotes.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "mrilib.h"
00015
00016 void Error_Exit(char *message) {
00017 fprintf (stderr, "\n\nError: %s\n", message);
00018 exit(1);
00019 }
00020
00021 void Show_Help(void) {
00022 fprintf(stderr,
00023 "Program: 3dNotes \n"
00024 "Author: T. Ross \n"
00025 "(c)1999 Medical College of Wisconsin \n"
00026 " \n"
00027 "3dNotes - a program to add, delete and show notes for AFNI datasets. \n"
00028 " \n"
00029 "----------------------------------------------------------------------- \n"
00030 " \n"
00031 "Usage: 3dNotes [-a \"string\"] [-h \"string\"][-d num] [-help] dataset \n"
00032 " \n"
00033 "Examples: \n"
00034 " \n"
00035 "3dNotes -a \"Subject sneezed in scanner, Aug 13 2004\" elvis+orig \n"
00036 "3dNotes -h \"Subject likes fried PB & banana sandwiches\" elvis+orig \n"
00037 "3dNotes -HH \"Subject has left the building\" elvis +orig \n"
00038 "3dNotes -d 2 -h \"Subject sick of PB'n'banana sandwiches\" elvis+orig \n"
00039 " \n"
00040 "----------------------------------------------------------------------- \n"
00041 " \n"
00042 "Explanation of Options:\n"
00043 "---------------------- \n"
00044 " dataset : AFNI compatible dataset [required].\n"
00045 " \n"
00046 " -a \"str\" : Add the string \"str\" to the list of notes.\n"
00047 " \n"
00048 " Note that you can use the standard C escape codes,\n"
00049 " \\n for newline \\t for tab, etc.\n"
00050 " \n"
00051 " -h \"str\" : Append the string \"str\" to the dataset's history. This\n"
00052 " can only appear once on the command line. As this is\n"
00053 " added to the history, it cannot easily be deleted. But,\n"
00054 " history is propagated to the children of this dataset.\n"
00055 " \n"
00056 " -HH \"str\" : Replace any existing history note with \"str\". This \n"
00057 " line cannot be used with '-h'.\n"
00058 " \n"
00059 " -d num : deletes note number num.\n"
00060 " \n"
00061 " -help : Displays this screen.\n"
00062 " \n"
00063 " \n"
00064 "The default action, with no options, is to display the notes for the\n"
00065 "dataset. If there are options, all deletions occur first and esentially\n"
00066 "simutaneously. Then, notes are added in the order listed on the command\n"
00067 "line. If you do something like -d 10 -d 10, it will delete both notes 10\n"
00068 "and 11. Don't do that.\n\n"
00069 );
00070 exit(0);
00071 }
00072
00073
00074 void Display_Notes(THD_3dim_dataset *dset) {
00075 ATR_int *notecount;
00076 int num_notes, i;
00077 char * chn , * chd ;
00078
00079 notecount = THD_find_int_atr(dset->dblk, "NOTES_COUNT");
00080 if (notecount == NULL)
00081 Error_Exit("There are no notes in the dataset");
00082 num_notes = notecount->in[0];
00083 for (i=1; i<= num_notes; i++) {
00084 chn = tross_Get_Note( dset , i ) ;
00085 if( chn == NULL )
00086 Error_Exit("Could not get the next note;"
00087 " is there a problem with the HEAD file?");
00088
00089 chd = tross_Get_Notedate(dset,i) ;
00090 if( chd == NULL ){
00091 printf("\n----- NOTE %d [no date] -----\n%s\n",i,chn) ;
00092 } else {
00093 printf("\n----- NOTE %d [%s] -----\n%s\n",i,chd,chn) ;
00094 free(chd) ;
00095 }
00096 free(chn) ;
00097 }
00098 }
00099
00100
00101 int main (int argc, char * argv[]) {
00102
00103 THD_3dim_dataset *dset=NULL;
00104 int narg = 1, i, curr_note=0, curr_del=0;
00105 char *notes[MAX_DSET_NOTES];
00106 char *history_note = NULL;
00107 int delnotes[MAX_DSET_NOTES], delindex, delnum;
00108 int HH=0 ;
00109
00110 if (argc == 1)
00111 Show_Help();
00112
00113 mainENTRY("3dNotes main"); machdep(); AFNI_logger("3dNotes",argc,argv);
00114
00115 for (i=0; i<MAX_DSET_NOTES; i++) {
00116 notes[i] = NULL;
00117 delnotes[i] = 0;
00118 }
00119
00120
00121
00122 while( narg < argc && argv[narg][0] == '-' ){
00123
00124 if( strncmp(argv[narg],"-help",5) == 0 ) {
00125 Show_Help();
00126 }
00127
00128 if( strncmp(argv[narg],"-a",2) == 0 ) {
00129 narg++;
00130 if (narg==argc)
00131 Error_Exit("-a must be followed by a string");
00132 notes[curr_note++] = argv[narg++];
00133 continue;
00134 }
00135
00136 if( strncmp(argv[narg],"-h",2) == 0 ) {
00137 narg++;
00138 if (narg==argc)
00139 Error_Exit("-h must be followed by a string");
00140 if( history_note != NULL )
00141 fprintf(stderr,
00142 "*** Warning: multiple -h options!\n") ;
00143 history_note = argv[narg++]; HH = 0 ;
00144 continue;
00145 }
00146
00147 if( strncmp(argv[narg],"-HH",3) == 0 ) {
00148 narg++;
00149 if (narg==argc)
00150 Error_Exit("-HH must be followed by a string");
00151 if( history_note != NULL )
00152 fprintf(stderr,
00153 "*** Warning: multiple -h options!\n") ;
00154 history_note = argv[narg++]; HH = 1 ;
00155 continue;
00156 }
00157
00158 if( strncmp(argv[narg],"-d",2) == 0 ) {
00159 narg++;
00160 if (narg==argc)
00161 Error_Exit("-d must be followed by a integer");
00162 delnotes[curr_del] = (int)atol(argv[narg++]);
00163 if (delnotes[curr_del++] < 1)
00164 Error_Exit("Cannot delete a note numbered < 1");
00165 continue;
00166 }
00167 }
00168
00169 if( narg >= argc )
00170 Error_Exit("No input dataset!?\n") ;
00171
00172 dset = THD_open_one_dataset( argv[narg] ) ;
00173 if( dset == NULL ) Error_Exit("Cannot open dataset") ;
00174 if( DSET_IS_MINC(dset) ) Error_Exit("Cannot use MINC dataset") ;
00175 if( DSET_IS_ANALYZE(dset) ) Error_Exit("Cannot use ANALYZE dataset") ;
00176 if( DSET_IS_1D(dset) ) Error_Exit("Cannot use .1D dataset") ;
00177 if( DSET_IS_3D(dset) ) Error_Exit("Cannot use .3D dataset") ;
00178 if( DSET_IS_CTFMRI(dset) ) Error_Exit("Cannot use CTF dataset") ;
00179 if( DSET_IS_CTFSAM(dset) ) Error_Exit("Cannot use CTF dataset") ;
00180 if( DSET_IS_NIFTI(dset) ) Error_Exit("Cannot use NIFTI dataset") ;
00181
00182
00183 do {
00184 delnum = 0;
00185
00186
00187 for(i=0; i<curr_del; i++)
00188 if (delnotes[i]>delnum) {
00189 delnum=delnotes[i];
00190 delindex = i;
00191 }
00192 if (delnum) {
00193 delnotes[delindex]=0;
00194 tross_Delete_Note(dset, delnum);
00195 }
00196 } while (delnum);
00197
00198
00199 tross_Dont_Encode_Slash( 1 ) ;
00200 for (i=0; i<curr_note; i++)
00201 tross_Add_Note(dset, notes[i]);
00202
00203
00204 if (history_note != NULL){
00205 if( HH == 0 )
00206 tross_Append_History( dset, history_note);
00207 else
00208 tross_Replace_History( dset, history_note);
00209 }
00210
00211
00212 if ((curr_note == 0) && (curr_del == 0) && (history_note == NULL))
00213 Display_Notes(dset);
00214 else {
00215 THD_write_3dim_dataset( NULL,NULL , dset , False ) ;
00216 THD_delete_3dim_dataset( dset , False ) ;
00217 }
00218
00219 return 0;
00220 }