678 return FALSE; |
678 return FALSE; |
679 } |
679 } |
680 } |
680 } |
681 |
681 |
682 |
682 |
683 Tokenizer *tokz_open(const char *fname) |
683 /* |
684 { |
684 * File open |
685 Tokenizer*tokz; |
685 */ |
686 FILE*file; |
686 |
|
687 static bool do_tokz_pushf(Tokenizer *tokz) |
|
688 { |
|
689 Tokenizer_FInfo *finfo; |
|
690 |
|
691 finfo=REALLOC_N(tokz->filestack, Tokenizer_FInfo, |
|
692 tokz->filestack_n, tokz->filestack_n+1); |
|
693 |
|
694 if(finfo==NULL) |
|
695 return FALSE; |
|
696 |
|
697 tokz->filestack=finfo; |
|
698 finfo=&(finfo[tokz->filestack_n++]); |
|
699 |
|
700 finfo->file=tokz->file; |
|
701 finfo->name=tokz->name; |
|
702 finfo->line=tokz->line; |
|
703 finfo->ungetc=tokz->ungetc; |
|
704 |
|
705 return TRUE; |
|
706 } |
|
707 |
|
708 |
|
709 bool tokz_pushf_file(Tokenizer *tokz, FILE *file) |
|
710 { |
|
711 if(file==NULL) |
|
712 return FALSE; |
|
713 |
|
714 if(tokz->file!=NULL){ |
|
715 if(!do_tokz_pushf(tokz)){ |
|
716 warn_err(); |
|
717 return FALSE; |
|
718 } |
|
719 } |
|
720 |
|
721 tokz->file=file; |
|
722 tokz->name=NULL; |
|
723 tokz->line=1; |
|
724 tokz->ungetc=-1; |
|
725 |
|
726 return TRUE; |
|
727 } |
|
728 |
|
729 |
|
730 bool tokz_pushf(Tokenizer *tokz, const char *fname) |
|
731 { |
|
732 FILE *file; |
|
733 char *fname_copy; |
687 |
734 |
688 file=fopen(fname, "r"); |
735 file=fopen(fname, "r"); |
689 |
736 |
690 if(file==NULL){ |
737 if(file==NULL){ |
691 warn_err_obj(fname); |
738 warn_err_obj(fname); |
692 return NULL; |
739 return FALSE; |
693 } |
740 } |
694 |
741 |
695 tokz=tokz_open_file(file); |
742 fname_copy=scopy(fname); |
696 |
743 |
697 if(tokz==NULL) |
744 if(fname_copy==NULL){ |
698 fclose(file); |
745 warn_err(); |
699 else |
746 goto err1; |
700 tokz->name=fname; |
747 } |
701 |
748 |
702 return tokz; |
749 if(!tokz_pushf_file(tokz, file)) |
703 } |
750 goto err2; |
704 |
751 |
705 |
752 tokz->name=fname_copy; |
706 Tokenizer *tokz_open_file(FILE *file) |
753 |
|
754 return TRUE; |
|
755 |
|
756 err2: |
|
757 free(fname_copy); |
|
758 err1: |
|
759 fclose(file); |
|
760 return FALSE; |
|
761 } |
|
762 |
|
763 |
|
764 |
|
765 static Tokenizer *tokz_create() |
707 { |
766 { |
708 Tokenizer*tokz; |
767 Tokenizer*tokz; |
709 |
768 |
710 tokz=ALLOC(Tokenizer); |
769 tokz=ALLOC(Tokenizer); |
711 |
770 |
712 if(tokz==NULL){ |
771 if(tokz==NULL){ |
713 warn_err(); |
772 warn_err(); |
714 return NULL; |
773 return NULL; |
715 } |
774 } |
716 |
775 |
717 tokz->file=file; |
776 tokz->file=NULL; |
718 tokz->name=NULL; |
777 tokz->name=NULL; |
719 tokz->line=1; |
778 tokz->line=1; |
720 tokz->ungetc=-1; |
779 tokz->ungetc=-1; |
721 tokz->flags=0; |
780 tokz->flags=0; |
722 tokz->optstack=NULL; |
781 tokz->optstack=NULL; |
723 tokz->nest_lvl=0; |
782 tokz->nest_lvl=0; |
|
783 tokz->filestack_n=0; |
|
784 tokz->filestack=NULL; |
724 |
785 |
725 return tokz; |
786 return tokz; |
726 } |
787 } |
727 |
788 |
728 |
789 |
729 void tokz_close(Tokenizer *tokz) |
790 Tokenizer *tokz_open(const char *fname) |
730 { |
791 { |
|
792 Tokenizer *tokz; |
|
793 |
|
794 tokz=tokz_create(); |
|
795 |
|
796 if(!tokz_pushf(tokz, fname)){ |
|
797 free(tokz); |
|
798 return NULL; |
|
799 } |
|
800 |
|
801 return tokz; |
|
802 } |
|
803 |
|
804 |
|
805 Tokenizer *tokz_open_file(FILE *file) |
|
806 { |
|
807 Tokenizer *tokz; |
|
808 |
|
809 tokz=tokz_create(); |
|
810 |
|
811 if(!tokz_pushf_file(tokz, file)){ |
|
812 free(tokz); |
|
813 return NULL; |
|
814 } |
|
815 |
|
816 return tokz; |
|
817 } |
|
818 |
|
819 |
|
820 /* |
|
821 * File close |
|
822 */ |
|
823 |
|
824 static bool do_tokz_popf(Tokenizer *tokz, bool shrink) |
|
825 { |
|
826 Tokenizer_FInfo *finfo; |
|
827 |
|
828 if(tokz->filestack_n<=0) |
|
829 return FALSE; |
|
830 |
731 if(tokz->file!=NULL) |
831 if(tokz->file!=NULL) |
732 fclose(tokz->file); |
832 fclose(tokz->file); |
|
833 if(tokz->name!=NULL) |
|
834 free(tokz->name); |
|
835 |
|
836 finfo=&(tokz->filestack[--tokz->filestack_n]); |
|
837 |
|
838 tokz->file=finfo->file; |
|
839 tokz->name=finfo->name; |
|
840 tokz->line=finfo->line; |
|
841 tokz->ungetc=finfo->ungetc; |
|
842 |
|
843 if(tokz->filestack_n==0){ |
|
844 free(tokz->filestack); |
|
845 tokz->filestack=NULL; |
|
846 }else if(shrink){ |
|
847 finfo=REALLOC_N(tokz->filestack, Tokenizer_FInfo, |
|
848 tokz->filestack_n+1, tokz->filestack_n); |
|
849 if(finfo==NULL) |
|
850 warn_err(); |
|
851 else |
|
852 tokz->filestack=finfo; |
|
853 } |
|
854 |
|
855 return TRUE; |
|
856 } |
|
857 |
|
858 |
|
859 bool tokz_popf(Tokenizer *tokz) |
|
860 { |
|
861 return do_tokz_popf(tokz, TRUE); |
|
862 } |
|
863 |
|
864 |
|
865 void tokz_close(Tokenizer *tokz) |
|
866 { |
|
867 while(tokz->filestack_n>0) |
|
868 do_tokz_popf(tokz, FALSE); |
|
869 |
|
870 if(tokz->file!=NULL) |
|
871 fclose(tokz->file); |
|
872 if(tokz->name!=NULL) |
|
873 free(tokz->name); |
733 |
874 |
734 free(tokz); |
875 free(tokz); |
735 } |
876 } |
|
877 |
736 |
878 |
737 |
879 |
738 /* */ |
880 /* */ |
739 |
881 |
740 |
882 |