int bibFormat( const XmElem *top, FILE *outfile ){ //prepare array of keys const char **keys = keys = malloc ( top->nsubs * sizeof(char*) ); assert (keys != NULL); BibData bibinfo; for (int i = 0; i < top->nsubs; i++){ if ( (*top->subelem)[i] != NULL ){ marc2bib( (*top->subelem)[i], bibinfo ); keys[i] = customCopy( bibinfo[AUTHOR] ); free(bibinfo[AUTHOR]); free(bibinfo[TITLE]); free(bibinfo[PUBINFO]); free(bibinfo[CALLNUM]); } } //sort records XmElem collection = *top; sortRecs( &collection, keys); //free keys for (int i = 0; i < top->nsubs; i++){ if (keys[i] != NULL){ free ((char*)keys[i]); } } if (keys != NULL){ free ( keys ); } //print in library format XmElem *temp = &collection; for (int i = 0; i < temp->nsubs; i++){ marc2bib( (*temp->subelem)[i], bibinfo ); fprintf(outfile, "\n%s %s %s %s", bibinfo[AUTHOR], bibinfo[CALLNUM], bibinfo[TITLE], bibinfo[PUBINFO]); if ( bibinfo[PUBINFO][ strlen( bibinfo[PUBINFO])-1 ] != '.'){ fprintf(outfile, "%c\n", '.'); }else{ fprintf(outfile, "\n"); } free(bibinfo[AUTHOR]); free(bibinfo[TITLE]); free(bibinfo[PUBINFO]); free(bibinfo[CALLNUM]); } return EXIT_SUCCESS; }
PyObject * Mx_marc2bib ( PyObject * self, PyObject * args ){ XmElem * collection; int recno = 0; PyArg_ParseTuple( args, "ki", (unsigned long * ) &collection, &recno ); BibData bdata; marc2bib( (* collection->subelem )[recno], bdata ); return Py_BuildValue ( "ssss", bdata[ TITLE ], bdata[ AUTHOR ], bdata[ PUBINFO ], bdata[ CALLNUM ] ); }
int selects( const XmElem *top, const enum SELECTOR sel, const char *pattern, FILE *outfile ){ //check for valid input pattern if ( (pattern[0] != 'a' && pattern[0] != 't' && pattern[0] != 'p') || pattern[1] != '='){ fprintf (stderr, "\nIncorrect string match pattern. Should be: <field>=<regex>\n"); return EXIT_FAILURE; } if ( printCollectionHeader(top, outfile) == 0 ){ return EXIT_FAILURE; } char const *reggie = &pattern[2]; //search each child for string reggie it it's specified tag BibData bibinfo; if (sel==KEEP){ for (int i = 0; i < top->nsubs; i++){ if ( (*top->subelem)[i] != NULL ){ marc2bib( (*top->subelem)[i], bibinfo ); } switch (pattern[0]){ case 'a':{ if ( match(bibinfo[AUTHOR], reggie) ){ if ( printElement( (*top->subelem)[i] , outfile, 1) == -1 ){ return EXIT_FAILURE; } } break; } case 't':{ if ( match(bibinfo[TITLE], reggie) ){ if ( printElement( (*top->subelem)[i] , outfile, 1) == -1 ){ return EXIT_FAILURE; } } break; } case 'p':{ if ( match(bibinfo[PUBINFO], reggie) ){ if ( printElement( (*top->subelem)[i] , outfile, 1) == -1 ){ return EXIT_FAILURE; } } break; } default:; } free(bibinfo[AUTHOR]); free(bibinfo[TITLE]); free(bibinfo[PUBINFO]); free(bibinfo[CALLNUM]); } }else if (sel == DISCARD){ for (int i = 0; i < top->nsubs; i++){ if ( (*top->subelem)[i] != NULL ){ marc2bib( (*top->subelem)[i], bibinfo ); } switch (pattern[0]){ case 'a':{ if ( match(bibinfo[AUTHOR], reggie) == 0 ){ if ( printElement( (*top->subelem)[i] , outfile, 1) == -1 ){ return EXIT_FAILURE; } } break; } case 't':{ if ( match(bibinfo[TITLE], reggie) == 0 ){ if ( printElement( (*top->subelem)[i] , outfile, 1) == -1 ){ return EXIT_FAILURE; } } break; } case 'p':{ if ( match(bibinfo[PUBINFO], reggie) ==0 ){ if ( printElement( (*top->subelem)[i] , outfile, 1) == -1 ){ return EXIT_FAILURE; } } break; } default:; } free(bibinfo[AUTHOR]); free(bibinfo[TITLE]); free(bibinfo[PUBINFO]); free(bibinfo[CALLNUM]); } } fprintf (outfile, "</marc:collection>\n"); return EXIT_SUCCESS; }
int review( const XmElem *top, FILE *outfile ){ if ( printCollectionHeader(top, outfile) == 0 ){ return EXIT_FAILURE; } FILE *input = fopen("/dev/tty", "r"); FILE *output = fopen("/dev/tty", "w"); if (input==NULL || output==NULL){ fprintf (stderr, "\nError, could not open /dev/tty\n"); return EXIT_FAILURE; } //Page 195, Begining Linux Programming 4th Ed. struct termios initial_settings, new_settings; tcgetattr(fileno(input) ,&initial_settings); new_settings = initial_settings; new_settings.c_lflag &= ~ICANON; new_settings.c_lflag &= ~ECHO; new_settings.c_cc[VMIN] = 1; new_settings.c_cc[VTIME] = 0; new_settings.c_lflag &= ~ISIG; if(tcsetattr(fileno(input), TCSANOW, &new_settings) != 0){ fprintf(stderr,"could not set attributes\n"); return EXIT_FAILURE; } BibData bibinfo; for (int i = 0; i < top->nsubs; i++){ if ( (*top->subelem)[i] != NULL && strcmp( (*top->subelem)[i]->tag, "record") == 0 ){ marc2bib( (*top->subelem)[i], bibinfo ); fprintf (output, "%d. %s %s %s %s", i+1, bibinfo[AUTHOR], bibinfo[TITLE], bibinfo[PUBINFO], bibinfo[CALLNUM]); if ( bibinfo[CALLNUM][ strlen( bibinfo[CALLNUM])-1 ] != '.'){ fprintf(output, "%c\n", '.'); }else{ fprintf(output, "\n"); } //get input and act on it char c = fgetc( input ); if ( c != ' ' && c != '\n' && c != 'd' && c != 'k' ){ fprintf (output, "\nInvalid input:"); fprintf (output, "\n< enter > : keep record"); fprintf (output, "\n< space > : skip record"); fprintf (output, "\n< k > : keep remaining records"); fprintf (output, "\n< d > : discard remaining records\n"); i --; //this will cause the last record to be displayed again }else if (c == ' '){ //skip record, therefor do nothing }else if (c == '\n'){ if ( printElement( (*top->subelem)[i] , outfile, 1) == -1 ){ return EXIT_FAILURE; } }else if (c == 'd'){ i = top->nsubs; //this will break the loop and 'discard' the rest of the records }else if (c == 'k'){ for (int g = i; g < top->nsubs; g++){ if ( printElement( (*top->subelem)[g] , outfile, 1) == -1 ){ return EXIT_FAILURE; } } i = top->nsubs; //break i forloop } free(bibinfo[AUTHOR]); free(bibinfo[TITLE]); free(bibinfo[PUBINFO]); free(bibinfo[CALLNUM]); } } tcsetattr(fileno(input), TCSANOW, &initial_settings); //Page 195, Begining Linux Programming 4th ed fclose (input); fclose (output); fprintf (outfile, "</marc:collection>\n"); return 0; }